Home > Net >  Laravel and mailgun set up correctly, but wont send email
Laravel and mailgun set up correctly, but wont send email

Time:05-31

I am trying to get my emails to work from Laravel using mailgun. However, I have hit a brick wall. I have set up my everything as per the documents. All config, env everything is good.

I followed this example (of many). enter image description here

Next I do 'driver' => env('MAIL_DRIVER', 'mailgun'), in the config/mail.php file

Then I do the below in config/services.php

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

And finally in my .env

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=https://api.mailgun.net/v3/website.com
MAILGUN_SECRET=reallysupersecretkeygoeshere
MAIL_PORT=587
MAILGUN_ENDPOINT=api.eu.mailgun.net

That should do it, but for some reason no success?!?

So I looked at the guzzle to see what is going on, to see if its even sending. Like the pic below and I get a 200 success value, so its sending it off. enter image description here

I went even further and did it with curl just to double check

 $from = 'Testemail';
        $to = '[email protected]';
        $cc = 'Member Placement';
        $bcc = 'Member Placement';
        $subject = 'Member Team Placement';
        $message = '<b>Member Team Placement</b> .';

        $mailgunsecret = 'secretcode';
        $mailguurl = 'https://api.mailgun.net/v3/a.website.com';
        var_dump($mailguurl);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$mailgunsecret);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_URL, $mailguurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            array('from' => $from,'to' => $to, 'cc' => $cc, 'bcc' => $bcc, 'subject' => $subject, 'html' => $message));

        $response = curl_exec($ch);
        curl_close($ch);

        var_dump($response);

And I get "Mailgun Magnificent API" which is great, but no email gets sent which is not really magnificent.

So its failing on their end. What could I possibly have missed?

CodePudding user response:

You have setup two different mailgun endpoints.

Check:

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

and

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=https://api.mailgun.net/v3/website.com
MAILGUN_SECRET=reallysupersecretkeygoeshere
MAIL_PORT=587
MAILGUN_ENDPOINT=api.eu.mailgun.net <-- here

CodePudding user response:

The answer is slightly embarrassing. Make sure you do below consistently. (Late nights, but still does not explain the hardcoded part)

php artisan config:clear
php artisan config:cache
php artisan cache:clear
php artisan route:cache
php artisan optimize

Mailgun support was helpful and had to find out via them that there was a typo. Might be useful to someone.

  • Related