'Mailgun API failing silently in Laravel 8.54
I've been trying for a full day to understand why my attempt to send mail with the Mailgun API fails silently in my Laravel 8 project. I know there are other questions here with a similar complaint, but so far none of the answers to these other questions have helped.
One potential clue is that when I comment out the mailgun domain and secret in my .env file, I get no errors. But I can't determine why.
I am using the free sandbox domain with the US location.
Here are my env variables:
MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=https://api.mailgun.net/v3/....mailgun.org
MAILGUN_SECRET=5...9
services.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
];
mail.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Mailer
|--------------------------------------------------------------------------
|
| This option controls the default mailer that is used to send any email
| messages sent by your application. Alternative mailers may be setup
| and used as needed; however, this mailer will be used by default.
|
*/
'default' => env('MAIL_MAILER', 'mailgun'),
/*
|--------------------------------------------------------------------------
| Mailer Configurations
|--------------------------------------------------------------------------
|
| Here you may configure all of the mailers used by your application plus
| their respective settings. Several examples have been configured for
| you and you are free to add your own as your application requires.
|
| Laravel supports a variety of mail "transport" drivers to be used while
| sending an e-mail. You will specify which one you are using for your
| mailers below. You are free to add additional mailers as required.
|
| Supported: "smtp", "sendmail", "mailgun", "ses",
| "postmark", "log", "array"
|
*/
...
ExampleMail.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ExampleMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->from('[email protected]')
->markdown('emails.example');
}
}
And in my controller, I am doing the following:
Mail::to('[my hard-coded email]')->send(new ExampleMail());
Can anyone help me spot the problem? I have guzzle installed: "guzzlehttp/guzzle": "^7.4", so this shouldn't be the issue and even if it was, there would be at least some kind of error.
Help would be greatly appreciated. I am still pretty new to Laravel so I'm currently in over my head.
Solution 1:[1]
I did some digging and finally realized the problem. It's a major newbie mistake. Looking at vendor/laravel/src/Illuminate/Mail/Transport/MailgunTransport.php I realized that MailgunTransport::send doesn't require the whole API base url, which is what Mailgun provides you. It only needs the part of the url that contains sandbox[number].mailgun.org. I had put the entire API url in my .env file. So if there are any other newbies out there struggling to understand why your emails aren't sending, this might be your issue. Making this change fixed my problem.
Solution 2:[2]
I've just had the same problem... you can also press Ctrl+p and then type MailgunTransport.php and press Enter, it will open the mailgun transport file or just navigate vendor>laravel>framework>src>illumate>Mail>Transport and open the MailgunTransport.php file.
On line 80 comment out this // throw new Swift_TransportException('Request to Mailgun API failed.', $e->getCode(), $e); and replace it with dd($e) and then run your script again. This will display the exact problem that you have and it'll be much easier to fix this way instead of vague error messages such as Request to Mailgun API failed.
DON'T FORGET TO UNCOMMENT LINE 80 AFTER YOU'VE FIXED WHATEVER ISSUE YOU HAD
Solution 3:[3]
I had this problem too.
Swift_TransportException
Request to Mailgun API failed.
http://localhost:3000/form-process
In my case it was seemingly because the MAILGUN_ENDPOINT was incorrectly set in my .env file.
Laravel had set this by default to:
MAILGUN_ENDPOINT=api.eu.mailgun.net
Changing it to
MAILGUN_ENDPOINT=api.mailgun.net
solved the problem for me. Hopefully this helps someone. Don't forget to run php artisan config:cache to reload your .env.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Cook |
| Solution 2 | |
| Solution 3 | Inigo |
