Spread the love

Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. In this tutorial i will show you to send Mail in Laravel Through Sendmail and SMTP. Laravel provides multiple drivers or services to send mail from different different providers.

Laravel gives wide range of options to send emails. We can queue an email for send on specific time using the queue drivers.

Following are the supported methods of Laravel mail

  1. Amazon SES
  2. MailGun
  3. Sendmail
  4. SparkPost
  5. SMTP

In this example we will learn to use sendmail and SMTP driver of laravel.

Laravel Mailing supports blade views based templating system so you can easily configure the mail body layout same as other pages.

It also gives a easy to implements mail with attachment feature so let’s start with example

Step 1: Create a laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2 : Configure the mail configuration

We have two options to configure the mail from .env file or config/mail.php file and the recommended way to configure it is from .enc file

So for SendMail

MAIL_DRIVER=sendmail 
MAIL_HOST=localhost
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

And for SMTP

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail@gmail.com
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_gmail@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3 : Generate Mailable Class

In laravel 8 or 9 every mail is implements mailable class thus first we need to create a mail class as follow

php artisan make:mail TestMail

This will create a class in following location App\Mail\TestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('test');
    }
}

Update the code as below

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;
    private $user;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user=$user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('info@readerstacks.com')
               ->subject('Mail from readerstacks.com')
               ->view('emails.test',["user"=>$this->user,"title"=>"Register"]);
    }
}

Step 4 : Create View for Mail

Create a simple view for mail body in resources/views/emails/test.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Readerstacks.com</title>
</head>
<body>
    <h1>Hello {{ $user['name'] }}</h1>
    
    <p>{{ $user['body'] }}</p>
    <p>Thanks to visit us.</p>
    
</body>
</html>

Step 5 : Create Route and Send Mail

Now, create a route and call Mail methods in inline function as below

<?php
use Illuminate\Support\Facades\Route;


Route::get('/send-mail',function(){
 $user = [
        'name' => 'Readerstacks',
        'body' => 'This is simple mail from Readerstacks'
    ];
   
    \Mail::to('receiver_email@domain.com')->send(new \App\Mail\TestMail($user));
   
});

Also Read : How to Share URL on social media using package in Laravel 9/8/7/6/5 ?

Leave a Reply