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

ixudra's avatar

Sending emails with Mailgun

I'm trying to use Mailgun to send emails to my users in my app. Unfortunately, I can't get this to work. I added a custom domain and I'm waiting for that to be verified. In the meantime, I'd like to continue working on my app on my development machine. I can't get this to work and it's getting really frustrating.

First, I tried sending it using the mailgun driver:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox-------------------.mailgun.org
MAILGUN_SECRET=key-----------------------

Unfortunately, I get the following error:

Client error: `POST https://api.mailgun.net/v3/sandbox-----------------------.mailgun.org/messages.mime` 
resulted in a `400 BAD REQUEST` response: { 
"message": "Sandbox subdomains are for test purposes only. 
Please add your own domain or add the address to authoriz (truncated...)

So instead, I tried using smtp:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=25
MAIL_USERNAME=postmaster@sandbox------------------------.mailgun.org
MAIL_PASSWORD=somePassword
MAIL_ENCRYPTION=null

which also thrown an exception:

Expected response code 250 but got code "554", 
with message "554 Sandbox subdomains are for 
test purposes only. Please add your own domain or 
add the address to authorized recipients in domain 
settings. " 

Which begs the question: what are these sandbox domains for? How can and should they be used? I'm at a loss here..

And how are you supposed to send emails in a development setting using Mailgun?

0 likes
12 replies
EmilMoe's avatar

They are for test, not for API implementation. Add your domain and verify it, takes literally 1 minute.

ixudra's avatar

Well I did add the domain, but it takes 24-48h for it to verify... As a matter of fact, my domain just verified and I was able to send emails after that.

So how and when would I use the sandbox? I'm still trying to make sense of this..

ixudra's avatar

From mailgun.com:

Once you've added the appropriate DNS records for verification, it can take 
24-48 hours for them to propagate.

I'm not making this stuff up...

1 like
EmilMoe's avatar

Yes I think this applies for both CNAME and MX, but not TXT which is for sending. :-)

ixudra's avatar

Well, I have to add two TXT but also one CNAME for the link tracking, so I guess I just have to wait it out either way..

EmilMoe's avatar

You can send mails for tests, the CNAME is for tracking only, TXT is for sending, so I think you can proceed your testing but without tracking.

ixudra's avatar

Sadly, no.. The domain needs to be verified before you can send emails... And the domain will not be verified until the DNS changes have been propagated :-(

abdul8002's avatar

In case anyone else is having similar issues, I think it's because you need to add authorized recipients and emails can only be sent to them in sandbox.

Otherwise the email address you registered with is authorized by default.

Any other emails will not work and you will end up with that error message.

4 likes

Please or to participate in this conversation.