Laravel is an extraordinarily popular PHP framework for building powerful web applications. With a thriving community, Laravel enables pretty much any concept you can think of - including email. In this post we'll show you how to setup a basic Laravel application with Composer and enable email functionality. We will use two methods to send and read email in our app: SMTP connections and an email API provider.

See also our Mailable and Notification Laravel guide

Concept

To demonstrate email sending we will create a Laravel webapp with a view that provides a button that will send an email from one address to another. We will then use a second button to display the sent email. This will give us a good example of how to read and compose emails in PHP.

Getting starting

Let's get up and running by creating a new Laravel project. First verify you have PHP installed by opening a terminal and running

PHP command not found?

If you see that command is not found then you need to install PHP on your machine. On a Mac this can be done with Homebrew.

Install Composer package manager

The next step is to install the PHP package manager composer.

Download the composer installation script from the downloads page:

Then use PHP to run the setup script:

This will create a local file called . We can install the executable globally by moving it to the user path.

Create new Laravel app

Now we can use composer to create a new Laravel application:

This command will create a new directory called with a directory structure like so:

Verify the app works

We can check that our new app works correctly by running the generated unit tests with the command:

Run the webserver

We can also run the webserver and view the webapp in a browser using :

This will start a server on by default.

Creating a dummy view

Now that we have an application let us modify the default view page served by the index to include two buttons, one for sending one for receiving emails. Views are found in the directory. Open the file to view the current route setup:

Notice the use of a template view. This refers to the welcome view inside the views directory:

We can replace this route with our own called :

Then create a new template in the views directory called .

Setting up the mail server

Now we have a basic application we can configure email sending using either a or settings. We can then use the mail server with either Laravel Mailable or Notifications to send emails. Later we'll show you how to receive email too!

Create a mail server account

You need access to an SMTP server for the next steps. Luckily MailSlurp provides free mail servers than can be integrated into any Laravel app! Just sign up for a free account and copy the API Key and SMTP details from the dashboard.

Create a .env file

If you plan to use a single mail server you can configure a file in your project root like this:

Configuring mail.php

If you want to provision inboxes dynamically use the MailSlurp PHP library instead. We can configure the mail settings in like this:

Now run this command to load the config changes:

Sending email

To send email in Laravel we have a number of options: direct SMTP with PHPMailer, Notifications, and Mailable interface. Let's cover those now.

Sending directly with SMTP

To directly send an email with can use the SMTP mailserver details and PHPMailer to send an email:

Sending with Laravel Mailable

First create a new object using artisan:

Then define our views for the mail:

We wrote for the content view, we also define a blade template :

This template becomes the email body when sending.

Sending with Laravel notifications

Laravel also supports multi-channel notifications (meaning we could also send SMS for example). Let us define a new Notification:

Inside the class we define our content:

Note that we haven't specified a view because laravel add notification styles for us. This is a big difference in Notifications and Mailables.

Receive email

To receive email in Laravel or PHP we can use the MailSlurp SDK to fetch emails from our mail server over HTTP/S or configure a webhook that will POST emails to our server.

Polling for emails

Using webhooks

Webhooks allow you to handle emails on your server without requesting them directly. You create webhook in MailSlurp that is triggered for any new inboxes. This webhook the sends the email to your server via HTTP/S POST.