Email Not Being Sent in Laravel 8 using Mailgun API

Viewed 1952

I set up my mailgun account and was trying to use the sandbox domain to test sending email using API and not smtp. This is how my Laravel .env file was set up

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=sandboxxxxxxx.mailgun.org
MAILGUN_SECRET=xxxxxxxxx
MAILGUN_ENDPOINT=api.mailgun.net/v3/<mailgun-domain>

Then I was using this in my controller to send the email.

    $emailToSendTo = Page::where('name', 'contact')->first()->email;

    Mail::to($emailToSendTo)->send(new ContactSent($request));

My email is not being sent as I look into the mailgun dashboard. I am also not getting any error message. Also I know that mailgun restricts the emails you can send to in the sandbox domain to what you set up in mailgun. The $emailToSendTo is a verified email.

The ContactSent class has been set up fine with the blade view.

1 Answers

I'm going to answer my own question so it helps someone.

I put this inside vendor/laravel/src/Illuminate/Mail/Transport/MailgunTransport.php and the method send():

dd($this->endpoint);

Then I tried to call the method which sends mail. I noticed that the MAILGUN_ENDPOINT was not set up properly therefore dd($this->endpoint) showed me the wrong endpoint.

It should be:

MAILGUN_ENDPOINT=api.mailgun.net

This solved my issue. I hope this will help someone debug. Also I really think that the environment variable should be named MAILGUN_BASE_URL as it confused the crap out of me.

Related