How To Send Email In Laravel 9 Using Mailgun

Websolutionstuff | Jul-29-2022 | Categories : Laravel

In this article, how to send email in laravel 9 using mailgun. we will learn laravel 9 to send emails using mailgun. Mailgun is an email delivery service that provides developers an easy API to send out transactional emails from their website. It's powerful APIs that enable you to send, receive, and track email effortlessly.

So, let's see, send mail using mailgun in laravel 9 and how to send email using mailgun in laravel 9.

Step 1: Create a Mailgun Account

Step 2: Setup .env file Configuration

Step 3: Create Mailable Class

Step 4: Create Blade File

Step 5: Create Route

 

Step 1: Create a Mailgun Account

In this step, we will create a mailgun account. So, open mailgun and create an account.

After successful registration, you will get redirected to Mailgun's dashboard page. Then, click on the "Overview" link from the left sidebar under the section labeled "Sending." On this page, you will see two ways to send emails, i.e., API and SMTP.

Click on the SMTP box and get the SMTP details.

how_to_send_email_in_laravel_9_using_mailgun_smtp_details

If you want to test the emailing feature, you can add a recipient email to the "Authorized Recipients" section available on the "Overview" page in the right sidebar.

Note: The email feature is restricted to authorized recipients for free accounts.

 

 

Step 2: Setup .env file Configuration

Now, we will set up mail configuration in the .env file.

MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@xxxxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org
MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=Your mailgun domain name
MAILGUN_SECRET=Your mailgun API Key

 

Step 3: Create Mailable Class

In this step, we will create a mailable class using the laravel artisan command.

php artisan make:mail TestMail

This command will create a new file app/Mail/TestEmail.php.

<? php

namespace App\Mail;
   
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
  
class TestMail extends Mailable{

    use Queueable, SerializesModels;

    public $user;

    public function __construct($user){

        $this->user = $user;

    }

    public function build(){

        return $this->subject('This Is Mailgun Testing Mail')
                    ->view('emails.test');
    }
}

 

 

Step 4: Create Blade File

Let's create a file under app/resources/views/emails/test.blade.php and add this code.

<!DOCTYPE html>
<html>
<head>
    <title>How To Send Email In Laravel 9 Using Mailgun - Websolutionstuff</title>
</head>
<body>
    <h5>{{ $user['name'] }}</h5>
    <p>{{ $user['info'] }}</p>
    <p>Thank you</p>
</body>
</html> 

 

Step 5: Create Route

In this step, we will create a route for sending emails.

routes/web.php

Route::get('send-mail', function () {

    $user = [
        'name' => 'Websolutionstuff',
        'info' => 'This is mailgun example in laravel 9'
    ];

    \Mail::to('[email protected]')->send(new \App\Mail\TestMail($user));

    dd("Successfully send mail..!!");

});

 


You might also like:

Recommended Post
Featured Post
How To Install VueJs In Laravel
How To Install VueJs In Larave...

In this article, we will see how to install vue js in the laravel framework. if you are not aware of you are freshe...

Read More

Jul-12-2020

Getting Started with Laravel in CodeLobster IDE
Getting Started with Laravel i...

Laravel is probably one of the most promoted PHP frameworks at the moment. It has a huge team and an excellent knowledge...

Read More

Jan-16-2022

How To Check Image Blur Or Not Using Python
How To Check Image Blur Or Not...

In this article, we will see how to check image blur or not using python. For blur image check we are using the OpenCV p...

Read More

May-13-2022

Carbon Add Days To Date In Laravel
Carbon Add Days To Date In Lar...

In this article, we will see carbon add days to date in laravel. Carbon provides the addDay() and addDays() functions to...

Read More

Dec-03-2020