Email template not using themed version - cakephp

I am using CakePHP 1.3 and the built in email features as described in the documentation. I have the html version of the template located in app/views/elements/email/html/reservation.ctp and its working as expected.
$this->Email->template = 'reservation'; // no '.ctp'
I also have a theme setup and most of the themed files are correctly overriding the default files. My problem is the themed email template is not being used when called from the themed site, its still using the email template file in the default path.
The default is at: app/views/elements/email/html/reservation.ctp
The theme is at: app/views/themed/myTheme/elements/email/html/reservation.ctp
Should the email template assignment automatically work with themes without the need for hard coding a path or is there another solution? Anyone else have this issue?

in cakephp when you want to create email template. Lets suppose we want to create an Html email. and email config is configured.
Views[File Structure]:
1) your content email with variables should be located in View/Emails/html [reservation.ctp]
2) your template should be located in View/Layouts/Emails/html [default.ctp OR any new template you have made]
controllers:
Note: some people think when you write an action(in controller) you have to write a view for it. In this case (for sending email) is completely wrong. only if you want to show the result which email sent successfully or not then is fine.
lets say ReserveController ;) and sendReservationEmail
function sendReservationEmail( $to, $from,$subject ,$template, $variables=array()){
$Email = new CakeEmail();
$Email->config('smtp')
->viewVars($variables)
->emailFormat('html')
->template($template['page'], $template['layout']) //'reservation', 'default'
->from($from) //'me#example.com' => 'My Site'
->to($to) //'you#example.com'
->subject($subject) //'Resevation'
->send();
}
Views (View/Emails/html/reservation.ctp):
Dear $this->viewVars['name'];
Welcome to our restaurant .....

Related

Error Image Upload and Get, Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException

I am uploading user profile image which is uploading and moved to storage/app/upload/images folder but when I am trying to display that image, below given error occurs.
Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException
Here is my controller function for displaying
public function userProfile() {
$image = Image::all();
return view('frontend.layouts.Profile',compact('image'));
}
My view in which I am displaying image
#foreach($image as $images)
<img style="width:210px ; height: 230px " src="/storage/app/upload/images/{{$images->image}}" >
#endforeach
Please Upload your image in Public directory and then try to access that, it will work fine
There are three ways of making an image available to a user:
1. As a public asset
Here the image is made available to everyone. For instance your website logo, or landing page image would be accessed by all. So there is a url to the image that is easily accessed by all. These sort of files would go straight to public/img/ folder.
2. As a protected image available only if specific url is requested
Here user specific images would be accessed by specific people. Think of your members' personal photos that you want to make available only to the member herself or to some specific person. In this case you would store the images in storage/app/public and make a symlink using the artisan command php artisan storage:link You can read more on this here. Assuming that you store your files using random names using str_random() you would then generate urls to your image using the asset() helper like: echo asset('storage/X3jf5j5b2j3n.jpg'); Given that the file names are random, it would be hard to access this image by everyone excepting those who have the url generated using the asset() helper.
3. As a protected image made available using Intervention library
In this case you would first check if user is logged in and then dynamically load the image using Intervention via another protected route. So in your web routes you would first have the user authorization using auth middleware:
Route::group(['middleware' => 'auth'], function () {
Route::get('user', 'UserController#userProfile');
Route::get('images/{image}', 'UserController#serveImage'); // this route serves the image only if user is logged in
});
Then once your have installed Intervention library using composer, our UserController would look like:
use Intervention;
class UserController extends Controller
{
public function userProfile()
{
$images = Image::all();
return view('frontend.layouts.Profile', compact('images'));
}
public function serveImage($image)
{
$filename = storage_path('app/images/'.$image);
return Intervention::make($filename)->response();
}
}
You can see that the image is now being served from the storage folder and not public folder. So this method serveImage() is called only when the route defined earlier for it is authorized. Intervention then works its magic to read the image and send it as a http response.
Your view would change one tad bit to accommodate the new route end point that we defined called images. I assume here that you are storing the filename of the image in db by a field named filename:
#foreach($images as $image)
<img style="width:210px ; height: 230px " src="{{ url('/images/'.$image->filename) }}" >
#endforeach
Note: Do bear in mind that the preferred way to serve images is by using method 2 since it is much faster. You can use method 3 sparingly if you really don't want anyone to even stumble upon the files using the urls.

How to create a link that download a file in Symfony

I did some research and I found this post:
How to create a link to download generated documents in symfony2?
I tried the solution, but it show my pdf in the browser, but what I want is that when someone click the link, it directly download the file. Is ther a way to do that with Symfony?
Kévin Duguay
Set up an action.
This uses annotation for the route. YOu can of course use yml or xml or whatever you are currently using
/**
* #Route("/download", name="download_file")
**/
public function downloadFileAction(){
$response = new BinaryFileResponse('path/to/pdf.pdf');
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,'pdf.pdf');
return $response;
}
Twig template:
Download file

CakePHP Upload Plugin: Programmatic File Retrieval without a Form

I use CakePHP-Upload plugin, now need to use the upload without form, following this example: Programmatic File Retrieval without a Form
All my upload are stored in the associated model, called the Attachment.
So when I save the article, at the same time save the images in Attachmen model.
Plugin documentation suggests something like this:
<?php
$this->Article->set(array('file' => $image_path)); // ?????
$this->Article->save();
?>
I have a larger collection of images on the server, previously uploaded via the Joomla CMS, now I have a migration of data to a custom CMS built on CakePHP framework. I need to take all these pictures and upload again, through this plugin.
How to properly use this plugin for my needs, I try to put the path to a local picture, but uploading is not working ???
EDIT
$image_path = WWW_ROOT . 'img' . DS . 'attachment' . DS . '59'.DS.'hpim4799.jpg';
debug($image_path);
'C:\xampp\htdocs\portal\webroot\img\attachment\59\hpim4799.jpg'
Do not save the record and not create new images.
Note, uploading through HTML form works well, I use windows 7 and xampp server.
EDIT 2 / Solution
Image path must be valid url like 'http://localhost/portal/img/attachment/59/hpim4799.jpg'
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
Thank you.
Can you tell me what the content of the variable $image_url is? The doucmentation says you can add a file via the form, or you can do it programmatically. If you do that, use the $image_url as path to the image. If you got an associated model like Attachment you shoud use:
<?php
$this->Article->set(array('Attachment.file' => $image_path)); // path + file to file
$this->Article->save();
?>
After hours of searching for solutions and detailed inspection of the Behavior code I finally found it.
Because Behavior uses php FILTER_VALIDATE_URL Filter, path must be a valid URL. Here is an example:
<?php
$image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
// return http://localhost/portal/img/attachment/59/hpim4799.jpg
$this->Article->Attachment->set(array('file' => $image_path));
$this->Article->Attachment->save();
?>

How do I test an HTML Email Template on CakePHP?

To confirm a purchase I must send an e-mail to the customer with her shopping cart details. I'd like to test the HTML template to use the appropiate CSS, and of course to check the data that arrives to it.
What code should I use instead of setting the Email parameters, to watch how the template will render on the Email?
I'm all new on CakePHP, so your help will be very much appreciated.
Thanks in advance.
~José
This is CakePHP 1.3, but I have a feeling it may work well with 2.0 as well. While there may be other ways to do it, I do by creating a test action in any controller, then return a render call of the email template. Check this out:
function email_test()
{
$this->layout = 'email/html/default';
$user = $this->User->findById(1);
$this->set('name', $user['User']['firstname']);
$this->set('email_heading', 'Welcome to My App');
return $this->render('/elements/email/html/welcome');
}
This action will now render out your email in the browser.
Use the debug transport for testing.
If you want to make it more comfortable write your own transport that creates a new html file in for example APP/tmp/email/.html See the debug transport class as a reference, it's dead easy to do this http://api20.cakephp.org/view_source/debug-transport#l-34
See also the book -> http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#using-transports
Here is the simple method I use to show html/plain text content of email in browser that I want to send using cakephp email class i.e. App::uses('CakeEmail', 'Network/Email');.
Just do an exit at the end of email template file or (email layout file) and then try to send email. it will render the email content.
HTH.
$this->Email->to = $user_id_array[0]['User']['email'];
$this->Email->subject = $arrTemplate[0]['EmailTemplate']['subject'];
$this->Email->replyTo = 'Admin<admin#indianic.com>';
$this->Email->from = 'Admin<admin#indianic.com>';
$this->Email->sendAs = 'html';
$this->Email->template = '/elements/email/html/warning_message';
$this->set('email_message',$arrTemplate[0]['EmailTemplate']['body']);
$this->Email->send();
in this way you can set template for email
If you want to print your email template on localhost for testing then you can use this code.
$this->Email->send();
print_r($this->Email->htmlMessage);

Can i create a url like google mail in cake php?

Hi The master CakePHP! I want to ask something.
I think the probem is so simple, Is it possible to generate a url in cakephp application like this? (This is google mail like url - http://mail.google.com/a/domain.com)
http://www.domain.com/a/[something]/[member]/[controller]/[action]
Where [something] is a dynamic variable, [member] is a prefix, [controller] and [action] is usual cakephp url element.
so I want to shift the usual CakePHP url to the right, or insert /a/something/ in the initial url.
In addition, [something] should be read from the controller action
thanks, your help would be very valuable to me and will really help me
QUESTION v2:
I have tried to add this routing into routes.php
Router::connect('/:code/:something/:controller/:action',
array(),
array('code'=>'a','pass'=>array('something'))
);
when i try to access a http://domain.local/a/pte/users/view, it's work, but it display an error when i try to access http://domain.local/a/pte/users
QUESTION v3:
So the simple question is:
How do I have a url like google (example above) without breaking the
default CakePHP routing?
QUESTION v4: (Thank's for #Rui)
I tried to create this routing:
Router::connect('/:code/:something/:prefix/:controller/*',
array(),
array('code'=>'a','pass'=>array('something'))
);
It's ok when i try to access
http://domain.local/a/pte/member/users (The result is cake render member_index view, It's a great progress :-) ),
but there are several issue:
when i try to access http://domain.local/a/pte/users (without prefix member, i decided to display index function) it's failed (display an error AController did not defined)
When i create a link
echo $html->link('test link',array('code'=>'a','something'=>'pte','member'=>true,'controller'=>'users','action'=>'another_view'));
it generate a link like this
http://domain.local/member/users/another_view/code:a/something:pte
Please try in /app/config/routes.php:
<?php
Router::connect('/a/:something/:member/:controller/:action/*');
?>
And the action should be:
<?php
public function action($something = null, $member = null) {
/* action code */
}
?>
Hope it helps.

Resources