There are three important steps for sending emails with Laravel Mailgun which are
- Create mailgun account
- Add DNS records
- Laravel Mailgun Api setup
Now let’s explore how to create a Mailgun account.
Create mailgun account:
- Step 1: Go to Mailgun Website Open a web browser and go to the Mailgun website at https://www.mailgun.com/.
- Step 2: Click on the ” Get Started for Free ” button on the Mailgun homepage to sign up for a free Mailgun account.
- Step 3: Provide Account Information Fill in the required information in the signup form, including your name, email address, password, and company name. Click on “Sign Up” to proceed.
- Step 4: Verify Email Address Mailgun will send a verification email to the email address you provided during signup. Open your email inbox and click on the verification link in the email to verify your email address.
- Step 5: Complete Account Setup After email verification, you’ll be prompted to complete your account setup. Provide the requested information, including your company name, website URL, and other details. Click on “Continue” to proceed.
- Step 6: Add Payment Information (if applicable) If you choose a paid plan or require additional features, you may be prompted to provide payment information during the account setup process. Follow the prompts and provide the necessary payment details if applicable.
- Step 7: Access Mailgun Dashboard Once your account setup is complete, you’ll be redirected to the Mailgun dashboard. Now first you need to add your domain. And it is recommended that you add it as a subdomain in Mailgun.
You have done your first step for sending an email with Laravel Mailgun.
Add DNS records:
After creating an account you need to add Mailgun DNS records to your hosting environment follow these steps.
- Step 1: Log in to Your Hosting Control Panel Access your hosting control panel provided by your hosting provider. This could be cPanel, Plesk, or a custom control panel, depending on your hosting environment.
- Step 2: Navigate to DNS Zone Management Find the DNS management section or zone editor in your hosting control panel. This is where you can manage your DNS records.
- Go to the mailgun dashboard. Click on Sending -> Domain Settings -> DNS records in this section you can see three types of records which are Sending records, Receiving records, and Tracking records. Copy these values and paste add-on hosting as TXT records, MX, and CNAME.
- (Important First it is a better approach if you use your subdomain for email transactions, Second when you are adding TXT, MX, and CNAME records don’t copy your domain name like email.mg.abc.com so just copy the email. mg.)
- After adding these records to your hosting go back to your Mailgun dashboard and go to the same section where you copied the values. In the top right corner you can see verify DNS Settings click on it. You can see the Verified label in front of every value. If there is any unverified please check values hosting and verify it again.
Laravel Mailgun api setup:
The last step for Laravel Mailgun integration is to do some code in your application following these steps.
- Install the Required Package via Composer
composer require symfony/mailgun-mailer symfony/http-client
- Check config/mail.php default value is set to Mailgun or not
'default' => env('MAIL_MAILER', 'mailgun'),
- Check config/service.php file containing
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'scheme' => 'https',
],
- Now you need to create a mailable class by using a such command
php artisan make:mail TestMail
- Go to app/Mail/TestMail.php and add this code
public function envelope()
{
return new Envelope(
from: new Address('info@yourdomain.com', 'Your company name '),
subject: 'Test Email',
);
}
// For view file
public function content()
{
return new Content(
view: 'emails. test',
);
}
- Now create an email directory in the views folder and also create a test.blade.php file in this directory.
<!DOCTYPE html>
<html>
<head>
<title>A Meaningful Page Title</title>
</head>
<body>
I am Test Email.
</body>
</html>
- Go to any controller from where you can want to send an email to add this code
use App\Mail\ApplicationSuccess;
use Illuminate\Support\Facades\Mail;
class HomeController extends Controller{
public function mail(){
$abc = Mail::to('abdul@gmail.com')->send(new TestMail());
}
}
Conclusion:
In conclusion, sending emails with Laravel Mailgun involves three important steps: creating a Mailgun account, adding DNS records to your hosting environment, and setting up Laravel Mailgun API. This includes signing up for a free Mailgun account, verifying your email address, completing account setup, adding Mailgun DNS records to your hosting control panel, installing required packages via Composer, checking and updating Laravel configuration files, creating a mailable class, creating a view for the email content, and sending the email from a controller in your Laravel application.
Read more about Laravel Debugbar
You need to follow some steps create mailgun account, Add DNS Record and add Mailgun API in laravel.