Laravel 5.4 Mailgun integration throws 401 Forbidden error when attempting to send emails

I’m working on a Laravel 5.4 application and trying to integrate Mailgun for email delivery. Everything seems configured properly but I keep getting a 401 Forbidden error.

The exact error message is:

ClientException in RequestException.php line 111:
Client error: POST https://api.mailgun.net/v3/sandbox9a7b2c4f5e1234567890abcdef123456.mailgun.org/messages.mime
resulted in a 401 UNAUTHORIZED response: Forbidden

My environment configuration (.env):

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox9a7b2c4f5e1234567890abcdef123456.mailgun.org
MAILGUN_SECRET=pubkey-2468a**********

Mail configuration (config/mail.php):

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'John Doe'),
],

Services configuration (config/services.php):

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],

Test route for sending email:

Route::get('/test-email', function () {
    $emailData = [
        'subject' => 'Welcome to our platform',
        'message' => 'Thank you for joining us. We hope you enjoy our services'
    ];

    Mail::send('mail.welcome', $emailData, function($mail){
        $mail->to('[email protected]', 'Test User')->subject('Welcome Message');
    });
});

I have guzzlehttp/guzzle version 6.2 installed. When I visit the test route, the 401 error occurs. What could be causing this authentication issue?

Check your Mailgun region endpoint - that’s probably it. If you’re in EU, use api.eu.mailgun.net instead of api.mailgun.net. Same thing bit me and threw a 401 error even though my credentials were right.

401 errors usually indicate incorrect API credentials, but there’s another aspect to consider. It’s important to ensure that your MAILGUN_DOMAIN matches exactly with what is displayed in your Mailgun dashboard. I’ve encountered cases where users incorrectly configured the sandbox domain or used a custom domain that hasn’t been verified yet. Additionally, check if your account might be suspended; I faced this issue when I unwittingly hit my free tier limit. A good initial step is to test your credentials with a curl command to determine if the problem lies within the Laravel configuration or if it’s a Mailgun authentication issue. If you are confident that the API key is correct, remember to clear your config cache and restart your server to apply any changes.

Your MAILGUN_SECRET is the problem. You’re using a public key (starts with ‘pubkey-’) but Mailgun needs the private API key to send emails. The private key starts with ‘key-’ followed by your secret. Go to your Mailgun dashboard → API Keys section and grab the Private API key, not the Public validation key. I made this exact mistake when setting up Mailgun and wasted hours debugging it. After you fix it, run php artisan config:clear so Laravel picks up the new value.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.