Laravel 9 Mail - How to send email in laravel 9 using custom mail template

Laravel 9 Mail - How to send email in laravel 9 using custom mail template

In this tutorial we will learn how to send an email to a user. Also we will send static and dynamic values using a custom mail template. If you want t to send an email your customer, user, business partner from your software then you are in the right place. We are going to learn this topic how to send mail in laravel 9.

 

Sending email has not complicated in laravel. Laravel provides a simple and clean API to do this.  Laravel provide several drivers for sending email via SMTP, Mailgun, Postmark, Amazon SES, and sendmai.

 

Today we will send mail via SMTP servers. In laravel app, each type of email sent from our application is represented as a "mailable" class, and these classes are stored in the app/Mail directory. Don't worry we will see everything step by step, for that we have to following the bellow steps.

 

We do following steps for sending email using custom template with variables and their values.

 

1. Setup a project

2. Generating mailable class

3. Changes in mailable class

4.  Create mail template

5.  Setup smtp in .env file

6. Send mail

 

 

 

Step-1. Setup a project

 

Open your terminal and run the bellow command to setup a project

composer create-project laravel/laravel email-send

 

 

Step-2. Generating mailable class

 

We know that every type of email sent from laravel app is represented as a "mailable" class, and these classes are stored in the app/Mail directory. The Mail directory will be generate inside app when you create your first mailable class using the artisan command make:mail, and surely we will create our mail class for send an email. 

Let's create a mail class

php artisan make:mail BasicMail

 

See BasicMail.php class is generate inside app\Mail directory

 

 

3. Changes in mailable class

 

Once the mailable class has created, open it so that we can see it's content. So the newly created BasicMail.blade.php file looks like bellow.

 

App\Mail\BasicMail.php

<?php

namespace App\Mail;

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

class BasicMail 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('view.name');
    }
}

 

Now initialize some empty string variable and assign value to those variables from __construct() method, next use them inside build() method so that we can send dynamic values in our custom template. Now our mail class looks likes 

App\Mail\BasicMail.php

<?php

namespace App\Mail;

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

class BasicMail extends Mailable
{
    use Queueable, SerializesModels;

    public $title='';
    public $info='';
    public $messages='';

    public function __construct($title='', $info='', $messages='')
    {
        $this->title = $title;
        $this->info = $info;
        $this->messages = $messages;
    }

    public function build()
    {
        $title = $this->title;
        $info = $this->info;
        $messages = $this->messages;
        return $this->view('emails.basic_mail',compact('title','info','messages'))->subject($title);
    }
}

 

 

Step-4.  Create mail template

 

Now we will create a directory emails inside resources\views and a  basic_mail.blade.php file inside resources\views\emails to design mail template.

 

resources\views\emails\basic_mail.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{ $title }}</title>
</head>
<body>

<p>{{ $messages }}</p>
<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <p>{{ $info }}</p>
        </div>
    </div>
</div>

</body>
</html>

 

 

5.  Setup smtp in .env file

 

Open .env file and setup smtp for sending email. I'm using mailtrap for testing email is comming or not. If you have not any mailtrap account just create an account and you will get the test credentials. Setup your credentials, don't use my credentials because it's not work for you, use your own.

Note: You will get the MAIL_USERNAME and MAIL_PASSWORD  from your mailtrap account.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=8ha73add6aad97
MAIL_PASSWORD=c98cbf3b906393
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=test@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Change only MAIL_USERNAME=8ha73add6aad97 and MAIL_PASSWORD=c98cbf3b906393. It's work perfectly.

 

Step-6. Send mail

 

Create a route and write your logic. You can write logic in controller or directly in web.php inside the route. For testing purpose i'm writting the code logic in web.php.

<?php

use App\Mail\BasicMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;


Route::post('/send-mail',function(){
    $title = 'Laravel 9 Email sending Tutorial By Web Journey';
    $messages = 'Hi, there welcome to web jouurney';
    $info = ('channel: Web Journey');
    
    Mail::to('example@gmail.com')->send(new BasicMail($title, $messages, $info));
});

 

Now start the artisan server and hit the url, you will see the magic how email is sending.

php artisan serve

 

I hope this will help you for sending an email using laravel. Thanks a lot

 

 

 

If you like what you are reading, please think about buying us a coffee as a token of appreciation.

Buy Me A Coffee

We appreciate your support and are committed to providing you useful and informative content.

We are thankful for your ongoing support 

Tags

  • Share This:

Comments - 1

Mehedi Hasan Nayem

Facing some problems. Your created Mail and my created mail look different. Like you have only a constructor and a function. But in my case, my newly created mail class contains a constructor, envelope, content, and attachments function. How to work with this? Note - I'm using Laravel version 9.91

Oct 28, 2022

WebJourney

Ok, no problem. You can create a function manually and like me in your Mail . It also works. Just create the function like me. Thanks.

Dec 17, 2022