Laravel Multipart Registration and Reminder Emails

Laravel 4 comes with a password reminder and registration interface. The interface sends out reminder emails with unique tokens, so that the application can confirm the user trying to recover their password does have access to the email address they claim is theirs.

Using the interface is easy. To recover a password, an anonymous user is asked to supply their email address. This is then passed into the reminder:


return Password::remind($credentials, function($message, $user){
$message
->subject('Password reset for your account - action needed now');
});

Lots of things happen behind that. It will return (redirect) you to the page you just came from, with either a success or failure, tested using Session::has(‘success’) and Session::has(‘error’). If there is an error you can display the reason


{{{ trans(Session::get('reason')) }}}

If successful you can tell the user to go check their emails. If neither, then you just display the password recovery form asking the user for their email address.

There is some setup to get all this working – the tokens need to be stored in a table. Users also need to be set up, either locally or remote users modelled through the IlluminateAuthUserInterface and IlluminateAuthRemindersRemindableInterface interfaces. This is all documented in a number of places, which I won’t repeat here. See here and here for starters.

Now, the email that is sent to the user comes from a template. The sample view supplied by Laravel out the box is in views/emails/auth/reminder.blade.php You can modify this template to suite your product, but the problem is, it is just a HTML email. A HTML-only email is ranked fairly highly as SPAM, and that can result in a lot of your users never receiving their reminder email with the token link in.

What you need is a text part for the email; it needs to be a multipart email with both text/plan and text/html parts to it. Doing this is surprisingly easy, and also does not seem to be well known.

You start by duplicating your HTML email – call it reminder-plain.blade.php and keep it in the same directory as the original HTML email. Edit reminder-plain.blade.php and strip out all the HTML tags. It should just be plain text. Headers can be underlined with dashes, bullet points replaced with asterisks, and links replaced with the plain URL surrounded by [square brackets].

Now we need to tell the Laravel reminder broker that it needs to use both templates. In app/config/auth.php there is a setting for the reminder email that looks like this:


'reminder' => array(

'email' => 'emails.auth.reminder',

'table' => 'password_reminders',

'expire' => 60,

),

The ‘auth.reminder.email’ setting points to the HTML view. What you can do, is provide BOTH views here as an array:


'reminder' => array(

'email' => array('emails.auth.reminder', 'emails.auth.reminder-plain'),

'table' => 'password_reminders',

'expire' => 60,

),

The HTML view comes first and the plain text second. The password reminder broker will use both templates as to create the body of the password reset email that it sends out.

And that is a as simple as it gets – create a second plain text template (I’m using template and view interchangeably here) then add it to the settings as an array.

3 Responses to Laravel Multipart Registration and Reminder Emails

  1. Daniel 2014-06-19 at 23:24 #

    Here is a script that works with Laravel that does email authentication to create a user account: http://signup.edenwired.com/

    • Jason Judge 2014-06-25 at 15:14 #

      I’ve not tried it, but it may be useful to someone. Thanks for the link.

  2. Khaled 2014-09-09 at 06:56 #

    Thank you! I’ve been looking for this all day!

Leave a Reply