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);
Related
I use html2pdf, which is based on TCPDF, in CakePhp to render Views in PDF.
However, sometimes the generation hangs, I mean the browser freezes and never receives data.
There is a way to debug such a behavior? In apache logs I do not see any kind of error...
$this->set(compact('quotation','company','user'));
$view = new View(null, false);
$view->set(compact('quotation','company','user'));
$view->viewPath = 'Quotations';
$view->layout = 'preventivo';
if ($quotation['Quotation']['quotation_type'] == SERVICE)
{
$content = $view->render('print_s_template');
$this->set(compact('content'));
$this->response->type('pdf');
$this->render('print');
the print.ctp has
App::import('Vendor', 'HTML2PDF', array('file' => 'html2pdf'.DS.'html2pdf.class.php'));
$html2pdf = new HTML2PDF('P','A4','it');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
and the html is in print_s_template.ctp.
I found a solution myself. The problem is that I forgot to pass some variables to the View $view. And I suppose cake throw an error which, next, html2pdf cannot "render".
So: double check that all the variables in the view do exist!
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 .....
In CakePhp 2.0, using CakeEmail new Component seems to not output flash message:
In my controller I put:
$email = new CakeEmail(array('log'=>true));
$email->transport('Debug');
and in my view
echo $this->Session->flash('email');
But nothing is printed out.
Has that function (flash) been removed in 2.0?
none of the cake email libs or components or transport classes touch the session or write any such flash content. they never did as far is I know.
but they return the email content as array for the DebugTransport.
so you would want to fetch the returned array and log it away:
$res = $this->Email->send();
$this->Session->setFlash($res ? 'Email sent' : 'Email not sent');
or sth like that.
Of course there is flash function in cakephp 2.0 for details check it here: http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
If you want to get ur flash message in your view you have to first set it in your Controller action.
//controller
$this->Session->setFlash('email');
//view
echo $this->Session->flash();
// The above will output.
<div id="flashMessage" class="message">
'email'.
</div>
In Cake 2.x the debug transport doesn't set the email content in session. Just check the return value, $contents = $email->send();. $contents will contain the headers and message so use them as required.
$response = $Email->send();
$response['headers']; // headers as string
$response['message']; // message body with attachments
$this->Session->setFlash($response['headers'].$response['message']);
Make sure you have the following in your layout file.
echo $this->Session->flash();
My order table includes customer filed , When add new order, after choose the customer name from dropdown list, I want the same page to display the information of customer, for example, address, phone number from customer table
How to do?
Thanks in advance...
Could you give me an example or related links? I don't know anything about AJAX, have no idea where to start.
Maybe this link: http://www.w3schools.com/ajax/ajax_database.asp, but it uses php not cakephp...
the code in example is : xmlhttp.open("GET","getuser.php?q="+str,true);
I don't know how to pass customer_id(q=str) to orderscontroller/getuser function.need help
You can use JsHelper to achieve the same you mentioned. Kindly try at your end, and provide some raw code so that peers can help you to do that.
Your code should be like this
function showCustomer(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("divtoDisplayInfo").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("divtoDisplayInfo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/yoursite/controllername/action/str,true); //if you use redirect and windows environment
xmlhttp.send();
}
you should expect the view of that action so do the normal cake processing, get data in the controller, render view, if there is an error set the actions autorender to false.
but i will just advice you to download Jquery add to your cakephp framework this simple line
$("divToDisplayInfo's_ID").load("Controllername/action")
solves your problem.
Quick question about CI.
I have a view with a form, several text input fields and a file upload.
I want to be able to take the input from the text fields, save it to the DB, and then upload the image.
I've achieved this by having the upload code in a controller, and if the upload is successful, a call to my Model is made to update the database.
Is this "best practice", or indeed an acceptable way of doing it? Or should the File Upload go in the Model. Does it matter?
Essentially my code is:
function edit_category()
{
$config['upload_path'] = 'images/category/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '300';
$config['max_height'] = '300';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$this->session->set_flashdata('status', $this->upload->display_errors());
redirect('admin/category/edit/'.$this->input->post('catID'), 'location');
}
else /*no errors, upload is successful..*/
{
$fInfo = $this->upload->data();
//$this->_createThumbnail($fInfo['file_name']);
//process form POST data.
$data = array(
'catName' => $this->input->post('catName'),
'catDesc' => $this->input->post('catDesc'),
'catImage' => $fInfo['file_name']
);
/* update the database */
$category = $this->category_model->edit_category($data, $this->input->post('catID'));
I would put this in a model because I like to keep my controllers as slim as possible. I think of the controller as the link between the views and the back-room processing, not the processing itself.
I'm not sure if this is "best practise" or not. It will certainly work the way you're doing it too. CodeIgniter allows you to be quite flexible in how you apply mvc theory.
Use your models to interact with data whether it's a database interaction, an api call, or a file upload and download. Use your controller to run the show and make calls to that data. Do your best to keep them all separate in case the method for interacting with that data ever changes. Most of the time we think of the model as a database function, but it really should be ANY data no matter how it's retrieved.
I came out with this same dilemma, should I put the file upload functionality in controller or model.
After few trial and error I decided to put it under model for reusable purposes as calling controller from another controller is against the MVC concept.