Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Sending emails in Laravel

Sunday, October 18th, 2015 by Servage

emailThe ability to send emails from within your web application is essential to almost any website or system. This rather trivial task is sometimes complicated by relatively unreliable libraries and unstable sending methods. If you have been around PHP for a while, you will probably remember the old days using the native “mail” function in PHP with Sendmail. Fortunately a lot has happened over the recent years, and great options for easy email sending (and receiving) have been well adopted in most frameworks.

Laravel includes an email sending API built upon the common and very popular Swift Mailer library. It provides all the basic sending capabilities and is wrapped into a nice object oriented structure, making it easy to extend and use. Furthermore it allows you to use a variety of email sending drivers, such as SMTP or other services. Like any other configurable service in Laravel, this allows for easy integration and switch between different solutions, which also makes local and live production use easy to handle. You could develop locally and send via a local mail server, while in production you use a different service, all nicely controlled via the environment configuration files.

Email sending example in a Laravel controller

You can get started sending emails via the Mail facade which exposes the API. Emails are handled like other views, so you could easily organize them in a separate folder (eg “views/emails”). Consider the example below, which is sending a welcome email to a new user.

Mail::send('emails.welcome', ['user' => $user], function ($m) use ($user)
{
    $m->to($user->email, $user->name)->subject('Welcome!');
});

It is pretty straightforward how you use the Mail facade, choose the desired template from the views folder, add your user variable into the sending function scope, and then generate and send the email. Inside your template you can access the passed variable, so printing the user’s name is pretty easy like in other views.

<?php echo $user->name; ?>

HTML and plaintext content

It is generally advisable to include both a nice HTML and a plaintext version of your content. This is to ensure maximum compatibility amongst multiple devices. One could argue that the plaintext version is obsolete due to the rise of smartphones and other compatible devices, but the overhead of including this is relatively small, and therefore still recommended. Nothing is more annoying that getting a malformed HTML email, so you can ensure less frustration by doing so.

Attachment handling

You can include attachments with your emails, both as regular attachments and as inline-attachments placed at a specific point in your email content.

Queueing email sending

If you are used to sending emails with Sendmail or SMTP, you may be doing it by issuing an email sending command, waiting for the mail server completion, and then continuing your script (eg. showing a website). It is advised to breakout your sending from the actual generation of an email. I.e. when you “send” an email, you actually just queue it, and then another service is (almost immediately) sending the actual message. This compartmentalization enables faster performance for scripts and websites, independently from mail server load.

Sending emails in Laravel, 3.5 out of 5 based on 6 ratings
Categories: Guides & Tutorials

Keywords: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.