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