In this tutorial you will learn about the Laravel 8 Send Email Example and its application with practical example.
In this tutorial we will learn how to send email in laravel 8 using mailable and mailtrap. We will use Laravel 8 builtin mailable markdown class to send emails. In order to send email using mailtrap we will implement laravel 8 builtin mailable markdown class. In this post I’ll show you how to integrate mailtrap to send email in laravel 8 application.
Laravel 8 provides builtin mailable markdown class to send emails. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails. You are required to configure driver details on the .env file.
Laravel 8 Send Email Example
In this step by step tutorial you will understand how to send emails in Laravel 8 with the help of Mailtrap. We will explain to you how to use Laravel 8 builtin mailable markdown class to send emails using Mailtrap.
Install Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel --prefer-dist laravel-markdown-demo |
Adding Mail Configuration in ENV
In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.
1 2 3 4 5 6 7 8 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD= MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=null MAIL_FROM_NAME="${APP_NAME}" |
If we are sending email from localhost using Gmail SMTP, then it requires you to turn on the “Allow less secure apps” on?. Go to the link provided:
1 |
https://www.google.com/settings/security/lesssecureapps |
Now, You need to turn on the option “Allow less secure apps”
Define Markdown with Mailable Class
The mailable class in Laravel used for sending emails. Now, lets create a new laravel mailable class using following artisan command:
1 |
php artisan make:mail OfferMail --markdown=emails.offerSendMail |
Once the above command executed, it will create a new laravel mailable class OfferMail.php in app/Mail/ directory. Open the OfferMail.php file and put the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class OfferMail extends Mailable { use Queueable, SerializesModels; public $offer; /** * Create a new message instance. * * @return void */ public function __construct($offer) { $this->offer = $offer; } /** * Build the message. * * @return $this */ public function build() { return $this->markdown('emails.offerSendMail') ->with('offer', $this->offer); } } |
Create and Organize Controller
Now, we will create a controller that will handle the logic to send email. Let’s create MailController Controller using following command:
1 |
php artisan make:controller MailController |
Once the above command executed, it will create a controller file MailController.php in app/Http/Controllers/ directory. Open the MailController.php file and put the following code in it.
Controllers/MailController.php
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan config:clear |
1 |
php artisan serve |
Now, open the following URL in browser to see the output –