sending mail in Laravel 5.4 using Mailgun get error code " 401 UNAUTHORIZED` response: Forbidden "
Asked Answered
L

11

14

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?

Lascar answered 14/4, 2017 at 16:32 Comment(2)
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. :pHypnosis
If you are not using the United States as your region, that may be the problem.Satinet
W
14

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.

Whisk answered 17/11, 2020 at 15:6 Comment(1)
Thank you! Would spend several more hours if it were not for your post.Narra
S
11

If you are not using the United States Mailgun region, you can 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'),
],

Alternatively, one can define the MAILGUN_ENDPOINT in the .env file:

MAILGUN_ENDPOINT="api.eu.mailgun.net"
Satinet answered 25/2, 2021 at 21:13 Comment(2)
This helped me regarding the European region. Thank you. It should be stated that you can also just add MAILGUN_ENDPOINT='api.eu.mailgun.net' to your .env file.Loutish
thanks, that helped me too, I'm using node.js but had the similar issue :DMajesty
L
10

Frankly it was quite an ordeal, I made the sandbox work 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.
Lei answered 21/12, 2017 at 10:39 Comment(2)
I did not have to do step 1 but that's probably because I'm not using a sandbox in my case.Stellular
Using the private API key is what did it for me. Using Laravel 6.9.0, but just set MAIL_DRIVER to mailgun, and added the MAILGUN_DOMAIN and MAILGUN_SECRET lines to my .env file and it worked.Inae
D
4

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

Duester answered 16/6, 2018 at 10:51 Comment(1)
To me this answer helped, somehow my Mailgun's private key was different. Thank you.Vasileior
C
2

Ok, I just want to summarize what I had to do to make Mailgun work in Laravel 8.

1. Install guzzle

composer require guzzlehttp/guzzle

2. EU or US?

I'm from EU. The Laravel Doc for Mailgun says to configure mailgun for usage in EU. This is WRONG if you're using the sandbox because it is located in the US apparently. I have a small US flag next to my sandbox url and it took me ages to figure this out.

If you want to use the sandbox put this into \config\services.php:

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

If you want to use a domain that is registered for EU you will need to make this change:

'endpoint' => env('MAILGUN_ENDPOINT', 'api.eu.mailgun.net'),

If you put the wrong region, your log should throw 401 UNAUTHORIZED response

3. Authorize Recipients

If you're using the sandbox you need to authorize recipients. Go to your mailgun account and to your sandbox domain and authorize recipients in the email field. If you try to send an email to non-authorized recipients your log should throw an 403 FORBIDDEN response

4. Default Mailer

If you want mailgun to be your default mailer you can change this in your \config\mail.php:

'default' => env('MAIL_MAILER', 'mailgun'),

5. Credentials

These credentials made it work for me, hopefully they will make it work for you too. Put them in .env:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org
MAILGUN_SECRET=2dfXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_ENCRYPTION=tls

MAILGUN_SECRET is your private api key which can be found when go to your mailgun account and click on your profile logo -> api keys.

MAILGUN_DOMAIN is your (I believe currently used) domain and can be accessed in your mailgun account either through the dashboard or through sending->domains in the navpanel. The only not messy reference field for your domain seems to be in sending->overview-> select SMTP and copy the end part of the Username field (without postmaster@)

I hope this saves time for someone because it took forever to get it to work.

Caespitose answered 4/11, 2021 at 12:7 Comment(0)
C
1

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

Chamberlain answered 29/5, 2020 at 5:13 Comment(0)
F
1

I had the same issue. Check your DNS settings in the Mailgun Sending domain checklist. I found I could not get an EU MG setting to work so I suggest you use a US MG server

Fistula answered 20/8, 2021 at 0:35 Comment(0)
C
0

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.

Cataclysm answered 14/4, 2017 at 17:14 Comment(3)
Hello, my friend Fastmover. I have no domain. I'm just testing to send mail in my tutorial exercise. So, can you suggest me other way please. By the way thanks for your answer. :)Lascar
Might have to contact mailgun's support about the sandbox account. I'm not sure there.Cataclysm
@WhiteNuzzle you can create sandbox account free of charge. And if you want to test with that account you have to first invite recipients (Authorized Recipients). one more thing first time when you receive the mail most of the time it will be appear in spam/junk boxSweven
B
0

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.

Beneath answered 7/3, 2018 at 15:41 Comment(0)
J
0

In case if you use API base mailgun, check if the domain exists. I removed my testing domain and after it tried to send a new email message from that domain. I got 401 error.

Jovia answered 2/11, 2021 at 8:57 Comment(0)
E
0

I had the same problem in Symfony. Yes, it's not Laravel but they're close.

In .env file I had

MAILER_DSN=mailgun://KEY:DOMAIN@default

The error disappeared after I changed it to

MAILER_DSN=mailgun://KEY:DOMAIN@default?region=eu

Ejector answered 29/3, 2022 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.