Email Template use to send customized email. We create all email templates in Admin Panel from below location:
Login Admin > Preference > Email Template
Each email template has a Name, Subject and Body. In side body we can pass user define variable in curly braked like {VariableName} for example:{Message} {Name} {Email} etc.
All these variable value passed from email function.
Send Email
We use a Helper to send email. There are three phase to send email.
1.Prepare Parameter: Parameter is a list of values that we set in email template. Each text in curly braked is consider as a Variable. All values for variable passed in a Associative Array. See below example:
$params = Array(
'FirstName' => 'Jhon',
'LastName' => 'Regan',
'Email' => 'jhon@example.com'
);
App::Helper('EmailTemplate')->setParameters($params);
2.Set Address: In this step we set To and From of the email address. If we do not define these values then by default Admin Name and Email set. See below example:
App::Helper('EmailTemplate')->setFrom('jhon@example.com','Jhon Regan');
App::Helper('EmailTemplate')->setTo('info@example.com','Site Admin');
3.Send email: After completing above steps we call below function to send email:
App::Helper('EmailTemplate')->prepare('TemplateName',true);
For example:
App::Helper('EmailTemplate')->prepare('ContactUs',true);
See below examples:
// Example 1
$params = Array(
'FirstName' => 'Jhon',
'LastName' => 'Regan',
'Email' => 'jhon@example.com'
);
App::Helper('EmailTemplate')->setParameters($params);
App::Helper('EmailTemplate')->setFrom('jhon@example.com','Jhon Regan');
App::Helper('EmailTemplate')->setTo('info@example.com','Site Admin');
App::Helper('EmailTemplate')->prepare('ContactUs',true);
/* Also we can write it like */
App::Helper('EmailTemplate')
->setParameters(
Array(
'FirstName' => 'Jhon',
'LastName' => 'Regan',
'Email' => 'jhon@example.com'
)
)
->setFrom('jhon@example.com','Jhon Regan')
->setTo('info@example.com','Site Admin')
->prepare('ContactUs',true);