I’m working on a Laravel 5.4 application and trying to integrate Mailgun for email delivery. However, I keep getting a 401 Forbidden error whenever I attempt to send an email.
The error message I’m receiving is:
RequestException.php line 111: Client error: POST https://api.mailgun.net/v3/sandbox123abc456def789ghi012jkl345mno.mailgun.org/messages.mime resulted in a 401 UNAUTHORIZED response: Forbidden
Here’s my current setup:
.env configuration:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox123abc456def789ghi012jkl345mno.mailgun.org
MAILGUN_SECRET=key-987f654e**********
mail.php settings:
'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.php configuration:
'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 are excited to have you on board!'
];
Mail::send('emails.welcome', $emailData, function($mail){
$mail->to('[email protected]', 'Test User')->subject('Welcome Message');
});
});
I have guzzlehttp/guzzle version 6.2 installed as well. When I visit the test route, the 401 error occurs. I’ve double checked my Mailgun credentials and domain settings but can’t seem to resolve this issue. Has anyone encountered this problem before?