How to send mail in Laravel using Mailtrap
There are many ways to send mail in Laravel. Using Mailtrap in Laravel gives a quick and easy way for the developers to test mail in Laravel. In this topic, we will develop a Laravel 10 application to send mail in Laravel using Mailtrap. It will have a simple form to send mail and the controller will get details from the submitted form to send mail using Laravel mail method. We will use Mailtrap for receiving mail in the Mailtrap inbox.
During developmemt you need to test you code for email, so this will help you verify Laravel send mail using Mailtrap for your project.
Step1 - Create a Laravel project
Let us create a project in Laravel 10. Make sure you have Composer and PHP installed in your system. I have given the project name as lara_mail.
This will create a folder named "lara_mail" under the folder where you run the above command.
Step 2 - MySQL Database and Migration
.env file for database details. I am using MySQL database named 'lara_mail', so I updated the file as below:
We will create a custom table called 'mail_logs' to store details of emails sent. Let us create a migration for this table. Go to your project folder and run the below command for migration from VS code terminal.
Above command will create the migration file name
<yyyy_mm_dd_xxxxxx>_create_mail_logs_table.php under database/migrations folder and a Model named "MailLog" under app/Models folder.
Migration scripts for mail_logs
Let us now run the Laravel migration to create new table as well as the default tables for Laravel. Run below command from project folder to create the tables.
After running migration, below tables are created.
MailLog model is already created, let us update it as to add $fillable columns.
Step 3 - Setup Mailtrap account
For emails, you can create a Mailtrap account and add the settings in your .env file. If you do not have an account in Mailtrap, you can go to https://mailtrap.io/ and signup there. Once you complete the signup you can go to "My Inbox", there you will get your credentials. Get the Host, Port, Username and Password and update your .env file with those values as shown below:
Step 4 - Write Controller code
We will create a controller named "MailController" with a method to get details from a submitted form and send mail using Mailtrap.
Let us run below command from the terminal to create the controller:
We will have a form to enter the email id to whom the message is to be sent, subject and a mail body. Once the user submits the form using the Send button, the controller method will process them and send the mail. The form will look like below:
There are two methods in the controller, one for the form,
sendMail(), and other to send mail after the form is submitted,
postSendMail() method, I am taking the values from the form, validating them and saving the details in "mail_logs" table. After that mail is sent using "mail_template" which is a view with the mail_body as the data to the view. Note that "to" and "subject" parameters are also used from the form.
There are two routes as given below:
Step 5 - Laravel blade views
We have one view for the form to send mail. Also, I am using a loader when the form is submitted. So when the form is submitted loader is called using jQuery. Below are the views along with the layout:
Form to send mail.
I am using some styles and the stylesheet is given below:
Step 6 - Test the application
From the project root, start the php development server:
From browser start
localhost:8000. Send an email using the form and check if you get the mail in your mailtrap inbox. Also, check that a row is inserted in the mail_logs table with details of the email.
Below is a test email I received when submiited the form:
Download code from github.