How to Configure Mailgun for Laravel: 5 Easy Methods

Abu Sayed
Information Technology Hub
3 min readJul 13, 2024

Discover 5 efficient ways to set up Mailgun for sending emails in your Laravel project. Boost your app’s email capabilities with our step-by-step guide.

5 Effective Ways to Configure Mailgun for Laravel Email Sending

Email functionality is crucial for modern web applications, and Laravel, combined with Mailgun, offers a powerful solution for handling email services. In this article, we’ll explore five different methods to configure Mailgun for sending emails in your Laravel project. Whether you’re a beginner or an experienced developer, these approaches will help you implement robust email capabilities in your application.

1. Using Laravel’s Built-in Mailgun Driver

Laravel comes with built-in support for Mailgun, making it the easiest method to get started.

Steps:

  • Install the Mailgun PHP SDK:
composer require symfony/mailgun-mailer symfony/http-client
  • Update your .env file with Mailgun credentials:
MAIL_MAILER=mailgun
MAILGUN_DOMAIN=your-domain.com
MAILGUN_SECRET=your-mailgun-secret
  • In config/services.php, add:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
  • You’re ready to send emails using Laravel’s Mail facade!

2. Configuring Mailgun as SMTP

For applications requiring SMTP configuration, Mailgun can be set up accordingly.

Steps:

  • Update your .env file:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=your-smtp-username
MAIL_PASSWORD=your-smtp-password
MAIL_ENCRYPTION=tls
  • Use Laravel’s Mail facade to send emails as usual.

3. Implementing Mailgun API Directly

For more control over email sending, you can use Mailgun’s API directly.

Steps:

  • Install Mailgun PHP SDK:
composer require mailgun/mailgun-php php-http/guzzle6-adapter php-http/message
  • Create a service class:
use Mailgun\Mailgun;
class MailgunService
{
protected $mailgun;
public function __construct()
{
$this->mailgun = Mailgun::create(config('services.mailgun.secret'));
}
public function sendEmail($to, $subject, $content)
{
$this->mailgun->messages()->send(config('services.mailgun.domain'), [
'from' => 'Your Name <your-email@example.com>',
'to' => $to,
'subject' => $subject,
'text' => $content
]);
}
}
  • Inject and use this service in your controllers or jobs.

4. Using Laravel Mailgun Package

Third-party packages can simplify Mailgun integration even further.

Steps:

  • Install the package:
composer require spatie/laravel-mailgun-mailer
  • Publish the config file:
php artisan vendor:publish --provider="Spatie\MailgunMailer\MailgunMailerServiceProvider" --tag="config"
  • Configure the config/mailgun-mailer.php file with your Mailgun credentials.
  • Use the package’s helpers to send emails easily.

5. Implementing Mailgun Webhooks

For advanced email tracking and processing, set up Mailgun webhooks.

Steps:

  • Create a webhook endpoint in your Laravel application.
  • Set up a route for the webhook:
Route::post('mailgun/webhook', 'MailgunWebhookController@handle');
  • Implement the controller to process webhook data:
class MailgunWebhookController extends Controller
{
public function handle(Request $request)
{
// Verify webhook signature
// Process the webhook data
// Update your database or trigger events
}
}
  • Configure the webhook URL in your Mailgun dashboard.

Conclusion

Configuring Mailgun for your Laravel project doesn’t have to be complicated. Whether you prefer the simplicity of Laravel’s built-in driver or the flexibility of direct API integration, there’s a method that suits your needs. By implementing one of these five approaches, you’ll enhance your application’s email capabilities and ensure reliable message delivery.

Remember to always secure your API keys and follow best practices for email sending to maintain a good sender reputation. Happy coding!

--

--

Abu Sayed
Information Technology Hub

Bangladeshi Full Stack Web Dev, Sys Admin & DevOps Engineer. Skills: Data analysis, SQL, Kubernetes. Python, PHP & Laravel. Me on: bd.linkedin.com/in/imabusayed