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

patgilmour's avatar

Mailgun API not sending, but SMTP does send

Hi,

I'm able to send emails using SMTP with Mailgun but when I try to use the (recommended) API, emails don't send.

I tested with the Mailgun curl API command and mails send and get through so it's not the key, I think:

curl -s --user 'api:MY_MAILGUN_PRIVATE_API_KEY' \
    https://api.mailgun.net/v3/mg.opie.app/messages \
    -F from='Excited User <[email protected]>' \
    -F [email protected] \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomeness!'

In Laravel 6.4, I've installed: "guzzlehttp/guzzle": "^6.4"

In my .env: MAIL_DRIVER=mailgun (other MAIL params are empty)

In config/mail.php: 'driver' => env('MAIL_DRIVER', 'mailgun'), (though I think .env should override this)

In config/services.php:

'mailgun' => [
        'domain' => env('mg.opie.app'),
        'secret' => env('MY_MAILGUN_PRIVATE_API_KEY'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],

When I run this in php artisan tinker I get 'null' back and the mail does not send:

Mail::send('welcome', [], function($message) {$message->to('[email protected]')->subject('Testing Mail');});

When I configure Mailgun to use SMTP instead and run the same command the mail does get through.

Any thoughts what I should try/change?

Thanks!

0 likes
3 replies
matth's avatar
matth
Best Answer
Level 31

The way you have it setup it is looking for keys of mg.opie.app and MY_MAILGUN_PRIVATE_API_KEY in your .env file. You'll want to set these back to default and then set the values in your .env file.

So set the config back to:

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

And then in your .env file use:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=mg.opie.app
MAILGUN_SECRET=MY_MAILGUN_PRIVATE_API_KEY

If you did want to hardcode them into your config file you could do it this way:

    'mailgun' => [
        'domain' => 'mg.opie.app',
        'secret' => 'MY_MAILGUN_PRIVATE_API_KEY',
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],

or this way if you wanted to hardcode them with the ability to still override them in your .env file.

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN', 'mg.opie.app'),
        'secret' => env('MAILGUN_SECRET', 'MY_MAILGUN_PRIVATE_API_KEY'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],
1 like
patgilmour's avatar

Pointy hat and seat in the corner for me ;-)

Thanks Matth!

matth's avatar

Haha, no problem. I've done the exact same thing before with my config files.

Please or to participate in this conversation.