PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Related Articles

Other Uses For Smarty

Sending Plaintext And HTML Emails

There are many situations where PHP is used to send emails. Probably the most common cases are for member signup type applications, where an email is sent to a user when they sign up on your site. Typically these kinds of sites also have a ‘fetch password’ type email, which sends a password reminder or reset link to a user.

Smarty is suited perfectly to managing the templates used in these emails. Frequently I see in people’s code a quickly rushed email template hard coded into their PHP code. This is really hard to manage and clutters up the PHP code.

To implement this in Smarty, firstly, you create your email template. The way I do is to code the email subject as the first line of the template. This comes down to personal preference, but doing so allows you to dynamically place content easily in the subject line.

Listing 1 registration-email.tpl
{$user.firstname}, your XYZ Site registration
Dear {$user.firstname},
 
Thank you for signing up at XYZ Site. Your login details are below.
 
    Username: {$user.username}
    Password: {$user.password}
 
Sincerely,
 
Mr. Fake Person
XYZ Site Administrator

Remember here that the first line is the subject line. For the example below, let’s assume the above template is stored in a file called registration-email.tpl.

Now it’s just a matter of running this template through Smarty, and then sending the output through the PHP mail() function.

Listing 2 listing-2.php
<?php
    define('SITE_FROM_EMAIL', 'webmaster@example.com');
 
    function sendRegistrationEmail($user)
    {
        require_once('Smarty.class.php');
 
        $smarty = new Smarty();
        //$smarty->template_dir = [path to templates]
        //$smarty->compile_dir = [path to template compile dir]
 
        // assign the user's details to the template
        $smarty->assign('user', $user);
 
        // fetch the email body
        $body = $smarty->fetch('registration-email.tpl');
 
        // the subject is on the first line, so parse that out
        $lines = explode("\n", $body);
        $subject = trim(array_shift($lines));
        $body = join("\n", $lines);
 
        // finally, send the email
        return mail($user['email'], $subject, $body, 'From: ' . SITE_FROM_EMAIL);
    }
?>

You will still need to manually customize Smarty’s template_dir and compile_dir as you would normally with Smarty. Note here that the fetch() method returns the template out as a string, rather than outputting is directly as the display() method does.

Now, to make use of this function, you would use something like:

Listing 3 listing-3.php
<?php
    $user = array('firstname' => 'Joe',
                  'lastname'  => 'Bloggs',
                  'email'     => 'bloggs@example.com',
                  'username'  => 'jbloggs',
                  'password'  => 'jb123');
 
    if (sendRegistrationEmail($user))
        echo 'Success';
    else
        echo 'Failure';
?>

The same principles apply for sending HTML emails also. The biggest difference is that you’d probably want to use something like PEAR’s Mail_Mime class to deal with sending HTML emails.

In this case, you’d create a template that consisted of HTML code like you would normally with Smarty, then set the output from this as the HTML body.

In This Article