Sunday, June 22, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Sending email in Laravel with Mailgun

May 17, 2024
in Front-Tech
Reading Time: 6 mins read
0 0
A A
0
Share on FacebookShare on Twitter



As a web developer, you will often need to send emails from your application. In fact, if you’ve been a developer for more than a few months, you’ve probably already had to do this. You might want to send emails for a variety of reasons, such as sending a welcome email to new users, sending a password reset email, or sending a notification to a user.

Laravel makes it easy to send these types of emails to single recipients, thanks to some handy features such as mailables and notifications. However, there might be times when you need to send emails to many recipients at once. For example, you might want to send a newsletter to the 5,000 users of your application. In these cases, you may need to consider using “bulk sending” (or sometimes referred to as “batch sending”) rather than sending individual emails.

In this article, we’re going to look at how to send emails from a Laravel application using Mailgun, a popular email service provider. We’ll then cover how to bulk send emails using Mailgun, and how to test that the emails are sent correctly. By the end of the article, you should feel confident in sending both single and bulk emails from your Laravel application using Mailgun.

What is Mailgun?
Mailgun is a popular email service provider that allows you to send, receive, and track emails from your Laravel application. They provide the ability to send both single and bulk emails, and also provide analytics on the emails you send. This means you can track statistics such as open rates, click rates, and bounce rates. To send an email from your application, you send an API request to Mailgun with the details of the email (such as the recipient, subject, and body), and Mailgun will then send the email for you.

This is a great way to send emails from your application as it means you don’t need to worry about managing your own email server, and you can be confident that the emails will be delivered reliably. Mailgun is used by over 150,000 businesses, including Lyft, Microsoft, Wikipedia, and American Express. So you can be confident that it’s a reliable service. I have used Mailgun for many years myself, and I’ve always found it to be reliable, easy to use, and well-documented.

Alternatives to Mailgun
In this article, we’re going to be discussing how to send single and bulk emails using Mailgun. However, there are other providers that you might want to consider depending on things such as your application’s requirements, your budget, and your personal preferences. Some popular alternatives to Mailgun include:

  • Postmark
  • SendGrid
  • Amazon SES
  • Mailtrap
  • Mailcoach
  • Resend

If you feel confident managing your own email server, you may even want to consider using your own server to send emails. However, if you do choose to do this, you should be aware of the potential pitfalls, such as deliverability issues and the added complexity of managing your own server. For most Laravel developers, using an external service, such as Mailgun, is a great choice because it takes care of all the complexities for you.

Setting up Mailgun
Before we take a look at any code, we’ll first need to set up Mailgun and prepare our Laravel application.

Creating your Mailgun domain
To get started, you’ll need to sign up for a Mailgun account at https://signup.mailgun.com/new/signup. Once you’ve signed up, on your Mailgun dashboard you’ll want to head to the “Sending” dropdown in the left-hand navigation menu and then click on “Domains”.

You’ll then want to press the “Add New Domain” button on the right of the screen. Here’s where you’ll want to define the domain you’ll be sending emails from. For example, let’s say your application’s domain is my-awesome-app.com, you might want to fill in the form with the following details:

  • Domain name: mail.my-awesome-app.com
  • Domain region: EU
  • IP assignment option: Shared IP
  • DKIM key length: 2048 bits

After you’ve submitted the form, you’ll be presented with some DNS record information. You’ll need to go to your DNS provider and add these records. This will allow Mailgun to send emails on behalf of your domain. Don’t worry if this part sounds complicated. I know when I was new to web development, this part confused me a little. Thankfully, Mailgun has some handy guides and videos to help you through this part of the process. After you’ve added the records to your DNS provider, you’ll need to press the “Verify DNS settings” button in the Mailgun dashboard. This will check that the records have been added correctly.

After this, you’ll then want to head to the Sending dropdown in the left-hand navigation menu and then click on the “Domain settings” option. From here, you’ll want to select the “Sending API keys” tab in the middle of the screen. You can then select the “Add sending key” button to create a new API key. You can call this whatever you’d like, but I’d recommend calling it something that makes it clear what it’s for. For example, you might want to call it mail.my-awesome-app.com. It’s important that you keep this key safe as you’ll only see it once and you need it for your Laravel application. This sending key is the API key that your Laravel application will use to send emails via Mailgun.

Preparing your Laravel application
We’re nearly ready to start sending emails from our app. But first, we need to prepare our Laravel project to use Mailgun. You’ll need to install the Symfony Mailgun Mailer transport package via Composer. You can do this by running the following command in your project’s root directory:
composer require symfony/mailgun-mailer symfony/http-client

You’ll then need to add the following configuration to the mailers field in your config/mail.php file:

'mailgun' => [
'transport' => 'mailgun',
],

Your config/mail.php file should look something like this:

return [
// ...
'mailers' => [
// ...
'mailgun' => [
'transport' => 'mailgun',
],
],
// ...
];

You’ll then need to add the following configuration to your config/services.php file:

'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],

This will result in your config/services.php file looking something like this:

return [
// ...
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
// ...
];

By adding these config fields, we’re instructing Laravel to read the MAILGUN_DOMAIN and MAILGUN_SECRET environment variables from our .env file. So let’s get these added to our .env file now. You’ll want to add the following to your .env file:

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=your-mailgun-domain
MAILGUN_SECRET=your-mailgun-secret

As you can see, we’ve set the MAIL_MAILER environment variable to mailgun. This tells Laravel to use the Mailgun mailer as the default mailer for our application. We’ve then set the MAILGUN_DOMAIN and MAILGUN_SECRET environment variables to the domain and secret that we created in the Mailgun dashboard. That’s it! We should now be ready to start sending emails from our Laravel application using Mailgun.

Sending a single email
In your Laravel applications, there are generally two different approaches to sending single emails: notifications and mailables. The Laravel documentation does a great job of explaining these two approaches, so we won’t spend too much time on them here. But I’d highly recommend reading the following documentation to see how to use these two approaches:

Let’s quickly cover how to send a single email using each of these approaches.

Sending a single email using mailables
Using mailables is a great way to send single emails from your Laravel application. Mailables allow you to define a class that represents an email, and then use this class to send the email to the recipient. They also provide you with a lot of flexibility, such as the ability to define the email’s subject, HTML and plain text content, and the ability to attach files to the email.

In comparison to notifications, I feel like mailables are sometimes the better choice when you need to send a more complex email that may require a lot of dynamic content or customization.

Let’s say our web application allows users to download PDF reports of their data. We might want to send an email to the user when the report is ready to download. We’ll take a look at a simple example of how to do this using mailables.

First, we’ll…



Source link

Tags: emailLaravelMailgunsending
Previous Post

Memecoins Outpace Wider Crypto Market

Next Post

Kandi Technologies Group, Inc. (KNDI) Q1 2024 Earnings Call Transcript

Related Posts

The essential principles of a good homepage
Front-Tech

The essential principles of a good homepage

June 7, 2024
How to measure and improve user retention
Front-Tech

How to measure and improve user retention

June 6, 2024
Push Animation on Grid Items
Front-Tech

Push Animation on Grid Items

June 5, 2024
How to build a Rails API with rate limiting
Front-Tech

How to build a Rails API with rate limiting

June 4, 2024
Introduction to the B.I.A.S. framework
Front-Tech

Introduction to the B.I.A.S. framework

June 3, 2024
Blue Ridge Ruby is exactly what we need
Front-Tech

Blue Ridge Ruby is exactly what we need

June 3, 2024
Next Post
Kandi Technologies Group, Inc. (KNDI) Q1 2024 Earnings Call Transcript

Kandi Technologies Group, Inc. (KNDI) Q1 2024 Earnings Call Transcript

11 Best Pastry Chefs Schools In 2024

11 Best Pastry Chefs Schools In 2024

I Built a Reusable Dashboard for Read the Docs Traffic Analytics Using Vizro-AI | by Jo Stichbury | May, 2024

I Built a Reusable Dashboard for Read the Docs Traffic Analytics Using Vizro-AI | by Jo Stichbury | May, 2024

Leave a Reply Cancel reply

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

  • Trending
  • Comments
  • Latest
23 Plagiarism Facts and Statistics to Analyze Latest Trends

23 Plagiarism Facts and Statistics to Analyze Latest Trends

June 4, 2024
Managing PDFs in Node.js with pdf-lib

Managing PDFs in Node.js with pdf-lib

November 16, 2023
How ‘Chain of Thought’ Makes Transformers Smarter

How ‘Chain of Thought’ Makes Transformers Smarter

May 13, 2024
The Importance of Choosing a Reliable Affiliate Network and Why Olavivo is Your Ideal Partner

The Importance of Choosing a Reliable Affiliate Network and Why Olavivo is Your Ideal Partner

October 30, 2023
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
Best headless UI libraries in React Native

Best headless UI libraries in React Native

September 28, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In