There are plenty of ways or services to send E-mail in the Laravel application. The mail function is essential for these tasks, to register or notify the users, so it’s native in Laravel and provides a wide range of capabilities like integration for sending mail through multiple drivers like SMTP, Mailtrap, Mailgun, and Amazon SES.
In addition, it provides options for queueing mails, markdown support as well and plain text or HTML messages.
What is Mailtrap and Why to Use it?
Mailtrap is a test mail server solution that allows testing email notifications without sending them to the real users of your application.
Mailtrap is a service for the safe testing of emails sent from the development and staging environments. Mailtrap catches your emails in a virtual inbox so that you can test and optimize your email campaigns before sending them to real users.
For your better understanding, we have divided our application into some parts :
- Create a new Application
- Database Configuration
- Mail Configuration
- Create Mail
- Create Blade View
- Add Route
- Running Application
Note: If you already have a Laravel application then you can skip the first and second steps.
Mailtrap Account
First, you need to create an account on Mailtrap if you don’t have one. You can use this link Mailtrap.
After creating an account you will get mail credentials which you can use for testing.
Create New Application
First, open the terminal and create a new Laravel application using the below command :
composer create-project --prefer-dist laravel/laravel MailtrapExample
Database Configuration
Let’s configure a database for our application. If you don’t have a database then create a new one. After creating the database open the .env file from the root directory of your project. if the .env file is missing from the project then copy content from the .env.example and create a file. .env file defines many common environment variables.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=**********
DB_USERNAME=*********
DB_PASSWORD=*********
Mail Configuration
First of all, let’s configure our mail, for that, you have to configure the mail driver, mail host, mail port, and Mailtrap credentials like the below example:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=mailtrap_username
MAIL_PASSWORD=mailtrap_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=test@mailtrap.com
MAIL_FROM_NAME="${APP_NAME}"
Create Mail Class
In Laravel, each type of mail your application sends is called a Mailable class. All Mailable classes are stored in the app/Mail directory in your project. There is no need to worry if it’s not present in your application, it’s automatically created while creating a mailable class.
Let’s create a mailable class that will handle sending mail to users. For that enter the below command into your terminal :
php artisan make:mail WelcomeMail
This command will create app/Mail/WelcomeMail.php and resources/views/mail/welcome-mail.blade.php files. The mailable class will handle the logic for mail while the view will be sent through mail to the user. Here you can also send plain text.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this->from(env('MAIL_FROM_ADDRESS'), 'Mailtrap')
->subject('Welcome To Codewolfy')
->markdown('mail.welcome-mail') //View to send
->with([
'name' => 'Groot',
]);
}
}
Here, in the build method, you can define parameters for mail like from method handle, from address, subject, view, and custom parameters used in the view. In this example we have passed static values, you can use dynamic values for each parameter using __construct() like the subject or from address.
Create Blade View
The resources\views\mail\welcome-mail.blade.php has already been created with the Mailable class. Let’s make some changes to the view.
@component('mail::message')
# Welcome {{$name}}
This mail is sent using Mailtrap.
Thanks,
{{ config('app.name') }}
@endcomponent
Create Route
The last thing we need to add is to create a route that will send mail to the user. The send-mail route passes the email address as a route parameter for testing it with different mail addresses. For this example, we are not creating a controller to handle this route.
<?php
use Illuminate\Support\Facades\Route;
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
Route::get('/', function () {
return view('welcome');
});
Route::get('/send-mail/{email}', function ($email) {
Mail::to($email)->send(new WelcomeMail());
return 'A message has been sent to Mailtrap!';
});
In the above, we have defined a route that gets the user to enter email and send mail using a mailable class and display message. You can use try and catch for error handling.
Running Application
php artisan serve
Open the below URL in your browser:
http://127.0.0.1:8000/send-mail/test@test.com
After the success message visit your Mailtrap inbox and it will show new mail. You can change the mail address into the URL as per your requirement or use it from a database.