Use the built in Silverstripe Email functionality to take advantage of Silverstripe's templating with your email.
With Silverstripe you can create a template for your email and populate it with whatever data you like.
Within your send function in your controller you can set the template with setTemplate
and push data into the email template with populateTemplate
:
$email = new Email($from, $to, $subject);
$email->setTemplate('EmailTemplate');
$email->populateTemplate(array(
'CartItems' => $cartItems,
'PageTitle' => $this->Title
));
$email->send();
Note that $cartItems
has to be a ArrayList
or DataList
to be able to use <% loop %>
in template. If $cartItems
is an array, read how to convert it to ArrayList
here.
Put your template into themes/mytheme/templates/Email/EmailTemplate.ss
EmailTemplate.ss
<!DOCTYPE HTML>
<html>
<head>
...
</head>
<body>
...
<% if $CartItems %>
<% loop $CartItems %>
...
<% end_loop %>
<% end_if %>
...
</body>
</html>