简体   繁体   中英

Laravel and mailgun set up correctly, but wont send email

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). https://enlear.academy/using-mailgun-to-send-emails-from-laravel-framework-9d5a39ba946a

So far, mailgun shows DNS is good after verification

在此处输入图像描述

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. 在此处输入图像描述

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

 $from = 'Testemail';
        $to = 'someemail@bla.com';
        $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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM