0

I recently upgraded my Laravel from 5.7.29 to 8.51. I have been using the Mailgun API to send emails for years. The old version of the site is still able to send via mailgun, but the new version keeps returning this message:

Client error: `POST https://api.mailgun.net/v3/mg.clstracking.com/messages.mime` resulted in a `401 Unauthorized` response: Forbidden

I found similar posts that indicate that kind of response if you're working in the EU and fail to change the MAILGUN_ENDPOINT in the services config. The server and site are all in the USA. I have verified I have the correct settings in my .env and that those are passed into my services.php config file and cached using artisan config:cache. I verified this by looking through the /bootstrap/cache/config.php and everything is there - I even checked this against another site with a different domain that is working the same way. I even tried hard-coding my domain and secret into the services.php.

If I change

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io

with my username and password, I get no error, but the email isn't sent and checking the logs on mailgun, there is no record. I'm at a loss what else to try.

In env I have:

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=mg.mydomain.com
MAILGUN_SECRET=key-##########################

In config/services.php:

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

In config/mail.php:

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

'mailers' => [
  'mailgun' => [
    'transport' => 'mailgun',
  ],
],
Dylan Glockler
  • 1,115
  • 1
  • 20
  • 40

1 Answers1

0

I use this config in my projects which are in production with gmail

MAIL_MAILER='sendmail'
MAIL_HOST='sendmail.googlemail.com'
MAIL_PORT=587
MAIL_USERNAME='email@gmail.com'
MAIL_PASSWORD="*********"
MAIL_ENCRYPTION='tls'
MAIL_FROM_ADDRESS='email@gmail.com'
MAIL_FROM_NAME="${APP_NAME}"

Maybe helps you.

gerSu
  • 69
  • 8
  • 1
    Thanks gerSu! I do appreciate that - I found a post with that option late at night after trying everything and have implemented that for now until I can resolve the issue. It's actually working pretty well as a backup, though I prefer the reliability of the transactional server. Note to others: to use googlemail, you have to config for 'less secure app access' https://medium.com/@agavitalis/how-to-send-an-email-in-laravel-using-gmail-smtp-server-53d962f01a0c – Dylan Glockler Nov 09 '21 at 14:04