CakePHP comes with an default email library that already supports SMTP mail protocol. In this article we will come to know that how we can send an email in HTML or text both.
We have to define the layout of text and HTML emails In app/views/layouts/email
In app/views/layouts/email/text/default.ctp add:
<!--?php echo $content_for_layout; ?-->
and in app/views/layouts/email/html/default.ctp add:
<!--?php echo $content_for_layout; ?-->
After calling the layout we can create the template for the emails. Right now we have added 2 files named- registration.ctp under email-text folder and email-html folder
app/
views/
elements/
email/
text/
registration.ctp
html/
registration.ctp
after creating a template file you can add the following code in the file app/views/elements/email/text/registration.ctp
Hi <!--?php echo $name ?-->, Thank you for registering with us.
also in app/views/layouts/email/html/default.ctp add:
Dear <!--?php echo $name ?-->, Thank you for registering with us.
After creating the template and adding the text code in it , we can define the email component in our controller
<!--?php var $components = array('Email'); ?-->
After defining the email component in the controller we can add the below code in the controller to send an email
<?php $this->Email->smtpOptions = array( 'port'=>'587', 'timeout'=>'30', 'host' => 'smtp.localhost.net', 'username'=>'abcd', 'password'=>'efgh', 'client' => 'domainname.com' ); $this->Email->delivery = 'smtp'; $this->Email->from = 'Your Name '; $this->Email->to = 'Recipient Name '; $this->set('name', 'Recipient Name'); $this->Email->subject = 'This is a subject'; $this->Email->template = 'registration'; $this->Email->sendAs = 'both'; $this->Email->send(); ?>
0 Comment(s)