Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Melodia's avatar

Mailgun gives 401 unathorized response

I was using my gmail account to send emails while working on localhost, but after uploading to the server the emails weren't going and t would give me an error in the page, so I assume it has to do with google security and decided to use mailgun, which am currently testing on local environment.

I created a new email with my domain [email protected] for the mailgun account.

In the .env I have the following:

MAIL_DRIVER=mailgun MAILGUN_DOMAIN=sandbox16261edfb1b94ea960061b8ce6e.mailgun.org MAILGUN_SECRET=g333c6d0-46a333e32

Did the setup in mail.php

'driver' => env('MAIL_DRIVER', 'mailgun'),

'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', 'Admin'),
],

I also edited the services.php

'mailgun' => [
    'domain' => 'sandbox16ddd1edfb1dd4e9ab89addd061b8ce6e.mailgun.org',
    'secret' => 'bddddd0-46a0de32',
],

I also added guzzle to my composer dependencies:

"require": {
    "php": "^7.1.3",
    "barryvdh/laravel-dompdf": "^0.8.2",
    "billowapp/payfast": "^0.1.29",
    "brian2694/laravel-toastr": "^5.5",
    "fideloper/proxy": "^4.0",
    "laravel/framework": "5.6.*",
    "laravel/tinker": "^1.0",
    "laravelcollective/html": "^5.4.0",
    "maddhatter/laravel-fullcalendar": "^1.3",
    "srmklive/paypal": "~1.0",
    "stripe/stripe-php": "4.*",
    "guzzlehttp/guzzle": "~5.3|~6.0"
},

When I try to send a email I see the following error:

Client error: `POST https://api.mailgun.net/v3/sandbox16261edfb1b94e9ab89a960061b8ce6e.mailgun.org/messages.mime` resulted in a `401 UNAUTHORIZED` response:\n

Forbidden\n

Is there anything else that I need to do?

0 likes
17 replies
Danlog's avatar

Did you check the .env file that is in the server? Usually when you use forge or other services like that, the .env file needs to be configured a part.

Melodia's avatar

Right now I am trying this on localhost. Its best to test on localhost first because first I will still have to do some work offline and then push the changes to github.

Snapey's avatar

make sure your config is not cached

easiest to go into tinker and type config('mail') and check the response has all the settings you expect

1 like
Melodia's avatar

@Snapey I managed to make it work with smtp using gmail account for now because I have to make a test on live environment, but I see that when a user registers, the system is back logging him in.

I then noticed that its probably because the vendor folder was not uploaded to the server. If it is not recommended to upload the vendor folder, how exctly can work around this issue?

Snapey's avatar
Snapey
Best Answer
Level 122

You cannot do ANYTHING without the vendor folder.

What is recommended is that you have a hosting provider that provides console access and then you run composer install to bring in all the packages

1 like
Melodia's avatar

I am using forge. I ran the composer install and it now works.

calmsz's avatar

To me it was that I was using an incorrect value for MAILGUN_SECRET env variable, at first I was using a key that I received through email but the required value was the api secret key that is way longer. Api secret key can be found in: Settings -> API Keys -> Private API key At mailgun.org

1 like
Wesstep1315's avatar

This was one step that I needed to follow that wasn't obvious at first. Thank you very much!

vainway 's avatar

i used this method but its not working am using localhost, i have all the requirements but doesn't work

i changed my mailgun to gmail account but steal giving me that error

please help me am stuck

junaidnawaz's avatar

Try after whitelisting your servers IP:

Mailgun Dashboard > Settings > API Keys > API Security IP Whitelist

Once added, it may take a few minutes to become active, after which you should be able to send emails from your laravel app

2 likes
Wesstep1315's avatar

This was one step that I needed to follow that wasn't obvious at first. Thank you!

dlievano's avatar

I have the same problem I have a Job to send the email after creating a user, it is working well in my local environment but on my production environment is not working, my credentials are the same in both environments my production environment was working but since last Monday it stopped sending emails.

GuzzleHttp\Exception\ClientException: Client error: POST https://api.mailgun.net/v3//messages.mime resulted in a 401 UNAUTHORIZED response: Forbidden

services.php

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

.env

MAIL_MAILER=mailgun [email protected] MAIL_PASSWORD=password MAILGUN_DOMAIN=domain.com MAILGUN_SECRET=key-a MAIL_FROM_NAME="Name" [email protected]

Blackpig's avatar

I know that this fairly but I lost a fair bit of time on this before finding the solution. If your domain is set up on Mailgiun's EU server then you need to specify the EU endpoint - otherwise your credentials aren't valid

MAILGUN_ENDPOINT=api.eu.mailgun.net

HTH

3 likes
alkhachatryan's avatar

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.

alissonsabino's avatar

I had the same problem, and I was NOT able to solve it, I found a temporary workaround until I found a solution: In the approval environment MAIL_DRIVER=mailgun works normally, in production I left the driver set to MAIL_DRIVER=SMTP

jery123's avatar

Dealing with a 401 Error in Mailgun Integration? Check Your API Region!

If you’re integrating Mailgun with your application and encountering a 401 Unauthorized error, one common cause could be a mismatch in the API region. Mailgun has different endpoints for its US and EU regions, and using the wrong one can prevent your authentication from working correctly.

Quick Fix: Identify Your Mailgun Region:

1 - For US-based accounts, use api.mailgun.net.

2 - For EU-based accounts, use api.eu.mailgun.net.

Update Your Configuration:

Ensure your configuration points to the correct endpoint based on your region. For example, in a Laravel application, your .env file should look something like this:

MAILGUN_API_KEY=your-mailgun-api-key

MAILGUN_DOMAIN=yourdomain.com

MAILGUN_ENDPOINT=api.eu.mailgun.net # For EU region; use api.mailgun.net for US region

Now in your config/services.php this is the array key for 'mailgun':

Please or to participate in this conversation.