How to send mail using mailgun in laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hi All,
Now, We see a article of laravel mailgun integration. We can see laravel send mail using mailgun.Now i’m going to show you about laravel send mail mailgun setup. In this article, i will implement a laravel mailgun tutorial. Let’s see bellow example of laravel mailgun integration.

We can easily setup mailgun for email send in laravel 6, laravel 7 and laravel 8 application.

Now in this post, i will give you step by step instructions for send email in laravel application by using mailgun. Now you can create blade file design and also with dynamic information for mail layout. So now see step by step guides for sending email by using laravel as your requirement.

Step 1: Add Configuration

First we need to create account at mailgun if you don’t have any account. So first of all click bellow link to create account:

Mailgun Site

After successfully creating account we will get mail configuration as mail host, mail port, mail username, mail passwor.

Domain Page: Sending->Domain

In Domain Detail page -> Click to SMTP

Get SMTP Details

Add Receiver Email for Testing:

add details from there bellow:

.env

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox659a0eea69cc4..
MAIL_PASSWORD=73c29..
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Read Also : How to Base64 Image Upload in Laravel?

Step 2: Create Mail

Now in this step we will create mail class MyTestMails for email sending. Here i will write code for which view will call and object of user. So let’s run bellow command.

php artisan make:mail MyTestMail

app/Mail/MyTestMails.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $details;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from codingspoint.com')
                    ->view('emails.myTestMails');
    }
}

Step 3: Create Blade View

In this step, i will create blade view file and write email that i want to send. Now i just write some dummy text. create bellow files on “emails” folder.

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Codingspoint.com</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>Thank you</p>
</body>
</html>

Read Also : Laravel 8 Sanctum API Authentication

Step 4: Add Route

Now at the last i will create “MyTestMails” for sending our test email. so let’s create a web route for testing send email.

routes/web.php

Route::get('send-mail', function () {
   
    $details = [
        'title' => 'Mail from codingspoint.com',
        'body' => 'This is for testing email using smtp'
    ];
   
    \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMails($details));
   
    dd("Email is Sent.");
});

Now you can run and check example.

Run Project:

php artisan serve
localhost:8000/send-mail

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →

Leave a Reply

Your email address will not be published. Required fields are marked *

10 − one =