2
\$\begingroup\$

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->_updateResetHash();
    $user = $this->_grabUser();

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

    $message = "Hello, " . ucfirst($user["first_name"]) . ", <br /><br />";
    $message .= "We received a request to reset your password. <br /><br />";
    $message .= "<a href=\"" . $resetLink . "\">Please click here to reset your password</a><br />";
    $message .= "<h4>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.</h4>";

    // If email is sent
    if (!Mail::send("Password reset link", $message, ["no-reply@.com" => "No-Reply@"], $this->_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.

\$\endgroup\$
4
  • \$\begingroup\$ Are you using a template engine in your project already, that could handle this as well? \$\endgroup\$ Apr 2, 2017 at 16:12
  • \$\begingroup\$ Your function is called try? \$\endgroup\$ Apr 2, 2017 at 16:14
  • \$\begingroup\$ @insertusernamehere twig. \$\endgroup\$
    – Joe Scotto
    Apr 2, 2017 at 16:14
  • 1
    \$\begingroup\$ @insertusernamehere 'ResetPassword::try();' \$\endgroup\$
    – Joe Scotto
    Apr 2, 2017 at 16:14

1 Answer 1

1
\$\begingroup\$

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->renderView(
    'resources/views/emails/password_reset.html.twig', [
    'first_name' => $user["first_name"],
    'reset_link' => $resetLink,
]);

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

Hello {{ first_name }},

<a href="{{ reset_link }}">Please click here to reset your password</a>

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.