Skip to Content search facebook instagram pinterest twitter youtube

Sending email

SproutCMS has an inbuilt system for sending rich-text (i.e. HTML) emails using Views, similar to the way that controller methods and widgets work.

Here's an example:

src/modules/Members/Helpers/Members.php

function sendWelcomeEmail($member_id)
{
    $mem = Pdb::get('members', $member_id);
 
    $view = new View('modules/Members/email/welcome');
    $view->set($mem);
 
    $mail = new Email();
    $mail->Subject = 'Hello there, new member!';
    $mail->addAddress($mem['email'], "{$mem['first_name']} {$mem['last_name']}");
    $mail->SkinnedHTML($view);
    $mail->send();
}

The view could look something like this:

src/modules/Members/views/email/welcome.php

<?php
use Sprout\Helpers\Enc;
?>
 
<p>Hi <?= Enc::html($first_name); ?>,</p>
 
<p>We like you, so please use this promo code for free stuff: <?= Enc::html($promo_code); ?></p>
 
<p>Cheers!</p>

Easy as pie, right?

But this is only the content of the message. The main skin is specified as the third template, which defaults to 'skin/email'.

Generally you only need a single email skin per site, but you can use multiple if you ever want to.