10

Server: Digital Ocean

Ubuntu 16.04

Laravel 5.8

I cannot get email to send out of laravel using mailgun.com

In Digital Ocean I have all outgoing ports open on the firewall, I have the correct DNS settings in Digital ocean for TXT and MX records. I have the correct and verified DNS records on my domain registar and mailgun has a green checkmark on all

config/mail.php

return [
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => [
    'address' => 'orders@domain.com',
    'name' => 'Company Name'
],
'encryption' => 'tls'),
'username' => 'orders@mg.domain.com',
'password' => 'xxxxd663hd02j727bb2eefd1ea38bbe0-58bc211a-670xxxx'
];

config/services.php

'mailgun' => [
    'domain' => 'https://api.mailgun.net/v3/mg.domain.com',
    'secret' => 'xxxxehbe8v25g3374e5as3ff32a45995-39bc661a-4716xxxx',
],

Controller

use Illuminate\Support\Facades\Mail;

$data = [
        'email' => 'email@yahoo.com',
        'name' => 'Bob Smith'
    ];

    $body_data = [
        'id' => '1234'
    ];

    Mail::send('emails.shipped', $body_data, function($message) use ($data)
{
    $message->to($data['email'], $data['name'])->subject('Test Email');
});

When I change mail driver to log and then check log file it looks great. Everything looks perfect and I have used mailgun before on Laravel 5.5 with no problems. I have also tried the new laravel build method and same issue.

I get no errors, I checked logs on apache2, no logs are appearing in mailgun and of course no email comes through in inbox or spam.

My question is, am I missing anything? What other troubleshooting can I do? Seems like my app isn't connecting to mailgun correctly.

Chad Priddle
  • 650
  • 2
  • 15
  • 32

5 Answers5

12

I think that in your config/services.php the mailgun.domain should be more like mg.domain.com (or sandboxXXXXXXX.mailgun.org if that's a dev environment), and not a url like the one you've set.

Cvetan Mihaylov
  • 872
  • 6
  • 15
0

Try to put:

'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'), 

in your mailgun array.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
0

I'm using laravel 5.8 and it's working with all default configuration

.env

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox8a408833ad1540e7b3a5d0151f606531.mailgun.org
MAILGUN_SECRET=92df7e85eeeaccaeae3d3b3164600666-87cdd773-8c819599

web.php

Route::get('send_test_email', function(){
    Mail::raw('Sending emails with Mailgun and Laravel is easy!', function($message)
    {
        $message->to('your_test_email@gmail.com');
    });
});

services.php

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],
Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0

I struggled with this very same problem (laravel + mailgun) for a FULL DAY.

This is what ultimately solved my problem. Hope this helps!

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=mail.mydomain.com
MAILGUN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxx
MAIL_FROM_ADDRESS=kp@mydomain.com
MAIL_FROM_NAME='KP'

In routes/web.php:

Route::get('/tst', function(){
    Mail::raw('Sending emails with Mailgun and Laravel is easy!', function($message)
    {
        $message->to('kp@yahoo.com', 'K P')->subject('Hello there, how are you?');
    });
    echo "string";
});

Note: you will need to make sure that your MAILGUN_DOMAIN is set and the MX / DNS records exist on your server / domain. This can take up to 24 hrs to propagate (sadly, the most annoying part). But these are all the settings you would need.

kp123
  • 1,250
  • 1
  • 14
  • 24
0

You may try Installing SwiftMailer library in your server.

sudo apt install -y php-swiftmailer