7
votes

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 a 401 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?

7
Its a very very late. But You are using wrong key MAILGUN_SECRET= Must be Secret Key not public key. I was facing same problem and figured it out. :pHusnain
If you are not using the United States as your region, that may be the problem.Darren Murphy

7 Answers

8
votes

Frankly it was quite an ordeal, I made the sandbox worked as following

  1. Added authorized recipient - Apparently sandbox can't send email to anyone, you need to add them as authorized recipient.
  2. Use proper credentials in .env File added 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 here
  3. Put MAIL_DRIVER=mailgun MAIL_HOST=smtp.mailgun.org MAIL_PORT=587 MAIL_USERNAME=postmaster@sandboxcc*****************.mailgun.org MAIL_PASSWORD=****************** MAIL_ENCRYPTION=tls
  4. !!!MOST IMPORTANT!!!! RESTART YOUR SERVER to load the new .env file.
6
votes

Ok found it, In the file "vendor\laravel\framework\src\Illuminate\Mail\Transport\MailgunTransport.php", the endpoint used is US one.

As said in documentation, https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions, you have an endpoint for US and EU.

If you are european, you must use "api.eu.mailgun.net" or you get a 401.

Just change the endpoint: Laravel is powerful and they think about that. You can add an 'endpoint' key to the config/services.php/mailgun entry.

4
votes

Make sure to check your credentials for the mailgun, make sure that those are correct.

Dont copy the Public Validation Key. please copy the Private API Key

2
votes

If you are not using the United States Mailgun region, you may define your region's endpoint in the services configuration file:

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),
],
1
votes

Default Mail Driver: Mailgun

MAILGUN_DOMAIN=*your-domain*.mailgun.org

MAILGUN_SECRET=pubkey-*your-public-key*

Default Mail Driver: SMTP

MAIL_USERNAME=postmaster@*your-domain*.mailgun.org

MAIL_PASSWORD=*your-password*

Don't forget to php artisan config:clear

0
votes

Personally I never got the sandbox account to work. I just did this a little over a month ago. Sandbox never worked, but the live account I created did. Try switching to the live account and let me know if that works for you.

0
votes

Just go to the config folder. and made some changes..

'mailgun' => [
    'domain' => env('your_domainxxxxxxx.mailgun.org'),
    'secret' => env('key-xxxxxxxx_private_API_keyxxxx'),
],

error will be resolved.