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

gmehtaster's avatar

Sending Mail using Mailgun

Hi,

I am trying to follow steps provided by Laravel Documentation to send emails. Here's what I have done so far

Installed Guzzle Set Driver option in config/mail.php to mailgun Next in config/services.php, I added the following code

'mailgun' => [
'domain' => 'your-mailgun-domain',
'secret' => 'your-mailgun-key',
],

I am using mailgun sandbox so used that credentials. I am also not sure what is mailgun domain. In my mailgun account, there is one domain name thats like sandbox54...mailgun.com There's another which is smtp login that's postmaster@sandbox54d5c9ed96434d689f971a0a3574efd3.mailgun.org. Which one should I be using?

However, if I try to do forgot password, I am not getting any emails. Any help on how I can resolve this?

0 likes
14 replies
fraserk's avatar

Here's my setting for mail gun.

in my .env file. mail settings

MAIL_DRIVER= mailgun
MAILGUN_DOMAIN = sandbox........mailgun.org
MAIL_HOST = smtp.mailgun.org
MAIL_PORT =587
MAIL_USERNAME = postmaster@sandbox..........mailgun.org
MAIL_PASSWORD = 0maigunpassword
MAILGUN_SECRET = key-...........
MAIL_FROM = ticket@sandbox..........mailgun.org
MAIL_ENCRYPTION=tls

in my services.php

  'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],
3 likes
gmehtaster's avatar

What about config/mail.php? Did you make any changes to that file?

fraserk's avatar

Yes. I just call the defined variables.

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

   
gmehtaster's avatar

@premsaurav I am trying to use SMTP for now.

I know there is a different library for sending through API but haven't tried that yet.

Also, I am wondering if there is anyway to send password reset email through API.

d3xt3r's avatar

@gmehtaster Laravel will use what ever mechanism you specify. For smtp you will have to specify

    MAIL_DRIVER=smtp // and then the username,password , host, port and encryption

If you want to use mailgun's api, the setting will be like

    MAIL_DRIVER=mailgun
    MAILGUN_DOMAIN=your-mailgun-domain
    MAILGUN_SECRET=*******
    // no other details required

Edit: I don't think you will require any additional package or library

4 likes
gmehtaster's avatar

@premsaurav

Thanks I was able to get it working on my server. Had to print env variables to see what was being used. Turned out that mail driver was set to smtp. So had to restart the app. Worked after that.

For some reason my local environment is not printing the env variables so its not working in my local environment yet.

gnovaro's avatar

Quick Note: If you are having an problems sending emails you may want to add the Guzzle package to your composer.json. The mailgun maildriver uses a package called Guzzle HTTP to send and recieve HTTP requests from your app. This may already be added in your composer.json, but if it is not you can add it by including "guzzlehttp/guzzle": "~5.3|~6.0" in the require section of your composer.json and then run composer update.

1 like
SomeT's avatar

This post has helped me a bit, so basically if you set MAIL_DRIVER=smtp it will not use API key just password, if you set MAIL_DRIVER=mailgun you have to specify again your API key in .env as well as services.php? Is that correct? If not then please can you give me an explanation someone, for me only setting it to smtp works, not tried it by setting API key in .env yet.

Helmchen's avatar

If you wanna use mailgun you only have to specify the

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=XY.mailgun.org
MAILGUN_SECRET=key-XYZ

in your .env file .. that should work out of the box.

At least it does for me, I use only Mailgun over the API (which is wayyy faster) :)

Maerryham's avatar

I want to use api which requires guzzle but i don't know how to go about it, my hosting server doesn't support smtp, while it works fine on my localhost, can someone please guide me on how to use guzzle with mailgun api for sending emails?

I am so new to this, and I need your help right away please.

Please or to participate in this conversation.