I'm trying to send mail in Laravel 5.4 project with Mailgun. I think I set the configuration correctly. But, I got this error message such as
ClientException in RequestException.php line 111: Client error:
POST https://api.mailgun.net/v3/sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org/messages.mime
>resulted in a401 UNAUTHORIZED
response: Forbidden
Here is my configuration:
in .env file
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org
MAILGUN_SECRET=pubkey-1767e**********
in mail.php file
'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', 'Richi Htoo'),
],
in services.php file
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
and I wrote mail sending code in default route such as
Route::get('/', function () {
//return view('welcome');
$data = [
'title' => 'Hi student I hope you like the course',
'content' => 'This laravel course was created with a lot of love and dedication for you'
];
Mail::send('emails.test', $data, function($message){
$message->to('[email protected]', 'White Nuzzle')->subject('Hello student how are you?');
});
});
And I also installed Laravel Package "guzzlehttp/guzzle" version 6.2 to send mail.
But when I call that default home route, I got an error message as I mention above.
I can't find any solution for my error in any where including this forum "stackoverflow.com".
Can anyone help me please?
Frankly it was quite an ordeal, I made the sandbox worked as following
MAILGUN_DOMAIN
and MAILGUN_SECRET
too as services.php uses them. Remember MAILGUN_SECRET
is the private key and starts with key-
, don't use public key hereMAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandboxcc*****************.mailgun.org
MAIL_PASSWORD=******************
MAIL_ENCRYPTION=tls