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
Laravel 9 Livewire File Upload Example
Laravel 9 Livewire File Upload...

In this article, we will see the laravel 9 livewire file upload example. Here, we will learn how to upload files us...

Read More

Dec-02-2022

jQuery Show and Hide Effects Example
jQuery Show and Hide Effects E...

Hello friends, in this tutorial, we will see jQuery show and hide effects example. jQuery show method and jQuery...

Read More

Jan-21-2022

Laravel 8 Toastr Notifications Example
Laravel 8 Toastr Notifications...

Today, I will show you Laravel 8 Toastr Notifications Example. There are many types of notifications availa...

Read More

Oct-19-2020

Laravel 8 Datatables Filter with Dropdown
Laravel 8 Datatables Filter wi...

In this example we will see laravel 8 datatables filter with dropdown, Here we will add datatables custom...

Read More

Jun-16-2021