Videos
Check out our tutorial video series.
Learn how to set up email functionality in Laravel with Composer. From creating a basic webapp to sending and reading emails through SMTP and APIs.
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
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.
Let's get up and running by creating a new Laravel project. First verify you have PHP installed by opening a terminal and running
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.
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.
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:
We can check that our new app works correctly by running the generated unit tests with the command:
We can also run the webserver and view the webapp in a browser using :
This will start a server on by default.
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 .
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!
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.
If you plan to use a single mail server you can configure a file in your project root like this:
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:
To send email in Laravel we have a number of options: direct SMTP with PHPMailer, Notifications, and Mailable interface. Let's cover those now.
To directly send an email with can use the SMTP mailserver details and PHPMailer to send an email:
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.
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.
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.
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.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.