Writing the Template
Perhaps the most distinct feature of Mail Core separate from PhpMailer
is the ability to write email templates for consistent emails being sent out. If you use some of the FW apps, I am sure you have received an email from Status saying you have been clocked in or out by another user, or an email from Tally saying your points are ready to be cashed in etc.
Email templates are saved in the content/email-templates
folder, and the template function pulls the template file from this folder based on the file name it is given. While there is a Handlebars Renderer, all of our current email templates are written using the php renderer. And the email is written using vanilla html with a few php injections.
Common Php Injections:
Variables are written in the template using the format: <?=$varName?>
The php renderer will replace this variable with its counterpart in the data array passed into the template function. Be careful, the template will throw an error if you try to use a variable that has not been defined in the data array.
If or foreach statements can be written in one of two ways: - Use "block format" where you have the ability to code in regular html still:
<?php if(conditional):?>
//Vanilla HTML
<?php endif?>
<?php foreach($array as $item):?>
//Vanilla HTML <?=$item?>
<?php endforeach?>
Or just do the whole thing in php:
<?php
if (conditional) {
echo "{html statement}";
}
?>
<?php
foreach($array as $item) {
echo "{html statement} $item";
}
?>
The former way is more prevalent within the apps.
At the bottom of your template, you should include the code:
<small><em>This message was sent by the Benner Informatics Automated Email System.</em></small>
This is like the "Informatics Signature" if you will that lets people know that it was sent out through our automatic php mailer.
Full Integrated Example
This file is located at content/email-templates/car-totaled.php
in our pretend app (note: if you desire a newline, you must type <br>
directly, leaving a newline in the file will not render correctly).
Hello <?=$name?>,<br>
This is your courtesy notice that this testing email has been sent to you because of your <?=$color?>
car. It has been totaled by <?=$sender?>.<br><br>
<?php if ($note):?>
"<?=$note?>" <br>
--<?=$sender?>
<?php endif?><br><br>
<small><em>This message was sent by the Benner Informatics Automated Email System.</em></small>