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

Scotalia's avatar

Problem setting up Mailgun SMTP & API on Laravel 8

As the title says, I am pulling my hair out trying to set up Mailgun, both the SMTP and API mailer. I am using Laravel v8 and am getting this error when I have Mailgun's SMTP as my mailer:

Swift_TransportException Connection could not be established with host :stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known

I can see from my recent guzzle install that "Package swiftmailer/swiftmailer is abandoned" and that I should use "symfony/mailer" instead, but not really sure if this is the problem and where to change this to test.

When i try to use the Mailgun API, i get the error that my domain is not validated and I am not allowed to send. I have checked and double/triple/quadruple checked my API key, domain name, and that my DNS records have 1. been edited, and 2. been verified through my Mailgun dashboard. I have put a ticket in with Mailgun to see if I missed anything, but am still waiting to hear back from them.

Assuming I can't/don't want to upgrade to Laravel 10 at this time, has anyone run into this issue and were you able to resolve it and send through Mailgun?

0 likes
2 replies
LaryAI's avatar
Level 58

It sounds like you have done all the necessary steps to set up Mailgun, so it's likely that the issue is related to the version of Laravel you are using.

Laravel 8 uses SwiftMailer for sending emails, and as you mentioned, it is now abandoned. You can try to use the Symfony Mailer package instead, as it is the recommended replacement for SwiftMailer. To do this, you will need to update your config/mail.php file.

First, you will need to add the Symfony Mailer package to your composer.json file:

"require": {
    ...
    "symfony/mailer": "^5.1"
},

Then, you will need to update your config/mail.php file to use the Symfony Mailer package instead of SwiftMailer. You can do this by changing the driver option to mailer:

'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'timeout' => null,
    ],

    'mailer' => [
        'transport' => 'mailer',
        'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'timeout' => null,
    ],
],

Finally, you will need to update your .env file to use the mailer driver instead of smtp:

MAIL_DRIVER=mailer

Once you have done this, you should be able to send emails using Mailgun.

Scotalia's avatar

@LaryAI Thanks for the quick AI response. However, when I follow these directions, I now get this error:

InvalidArgumentException Unsupported mail transport [mailer]

Please or to participate in this conversation.