I’m working on a Laravel 5.4 application and trying to configure Mailgun for email delivery. Everything seems to be set up properly but I keep getting a 401 Forbidden error when trying to send emails.
The error message I receive is:
ClientException in RequestException.php line 111:
Client error: POST https://api.mailgun.net/v3/sandbox123abc456def789.mailgun.org/messages.mime
resulted in a 401 UNAUTHORIZED response: Forbidden
My environment configuration (.env):
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox123abc456def789.mailgun.org
MAILGUN_SECRET=key-987654321abcdef
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. The error occurs whenever I visit the test route. Has anyone encountered this authorization issue before?