Composing an HTML email body in PHP swiftmailer The following code is what I'm using to send a password reset email. The specific part I'm referring to is how I'm setting $message. Would there be a cleaner way to do this or is this about as clean as you can get?

/**
 * Attempt to send reset link
 * @return bool True if success, false if not
 */
public function try() {
    // Update users email hash
    $this-&gt;_updateResetHash();
    $user = $this-&gt;_grabUser();

    $resetLink = $_SERVER["HTTP_ORIGIN"] . "/reset/" . $this-&gt;_hash . "/" . $user['uid'];

    $message = "Hello, " . ucfirst($user["first_name"]) . ", &lt;br /&gt;&lt;br /&gt;";
    $message .= "We received a request to reset your password. &lt;br /&gt;&lt;br /&gt;";
    $message .= "&lt;a href=\"" . $resetLink . "\"&gt;Please click here to reset your password&lt;/a&gt;&lt;br /&gt;";
    $message .= "&lt;h4&gt;If you did not make this request you do not need to worry. The only way your password can be reset is through the email that is on the account. Unless someone has taken control of you email, your account is safe and no one except you can gain access.&lt;/h4&gt;";

    // If email is sent
    if (!Mail::send("Password reset link", $message, ["no-reply@.com" =&gt; "No-Reply@"], $this-&gt;_email, 'text/html')) {
        return false;
    }

    return true;
}


Also, I'm not too sure how or if it exists but I heard somewhere that Twig / Slim, which I'm using, have a way of using templates for swiftmailer.
As you're using the Twig template engine already, I would recommend to use it for your email-templates as well. It will have all the advantages like:


easy to maintain
easy to localize
template and its content is separated from your logic
all templates are in one place


Symfony has a manual, how to use Twig together with Swiftmailer. This will work also without the Symfony framework.

In your case you can simply store the rendered template in $message and pass it on to Mail::send():

$message = $twig-&gt;renderView(
    'resources/views/emails/password_reset.html.twig', [
    'first_name' =&gt; $user["first_name"],
    'reset_link' =&gt; $resetLink,
]);


In your template you have all the advantages and simplicity of Twig:

Hello {{ first_name }},

&lt;a href="{{ reset_link }}"&gt;Please click here to reset your password&lt;/a&gt;




You could take it one step further and include the subject as well in the template:

{% if true == get_subject %}
    {% spaceless %}
        Password reset link
    {% endspaceless %}
{% else %}
    Hello {{ first_name }}
{% endif %}


You need spaceless in this case for the subject, so that Twig will remove all whitespaces, tabs etc. before and after the subject.