Eugene
Eugene

Reputation: 1730

laravel with mailgun set up

I read everything I can and looks like I set it up and fill in everywhere according to the docs (and internet) but still cant receive any emails.

I need to use MailGun API so I can send emails from my localhost as well as from the test and prod + because 25 port usually closed.

I filled in everything I can just in case some miracle happen (was changing between smtp/mailgun in .env)

I have tried both my real server settings and when I fail tried without any success sandbox as well.

my mailgun data access (some letters hidden):

  1. my mailgun sandbox server https://i.sstatic.net/yLxv7.png

  2. when i click to it i have an option to choose between API and SMTP
    2.1 in API tab i have API KEY: https://i.sstatic.net/iaS03.png
    2.2 in SMTP box i have smtp host, port, login, passwd: https://i.sstatic.net/tMh2T.png

  3. my .env:
MAIL_DRIVER=mailgun

MAILGUN_DOMAIN=sandbox8ffe89553e224c468f4ad0cf6b4da3c2.mailgun.org
MAILGUN_SECRET=b3ff30bxxxxxxxxxxxxxxxx087d-f877bd7a-01ecceaa

somewhere in internet found that MAILGUN_DOMAIN & MAILGUN_SECRET could be added in .env so tried them here as well

  1. config/mail.php
    'driver' => env('MAIL_DRIVER', 'mailgun'),
  1. app/mail/Test.php
    public function build()
    {
        return $this->from('[email protected]')
                    ->subject('HELLO')
                    ->view('test');
    }
  1. sandbox "to" email verified https://i.sstatic.net/raLJQ.png

  2. TestController

use App\Mail\Test;
use Illuminate\Support\Facades\Mail;

class TestController extends Controller
{
    public function test()
    {
        echo 'hello';

        try {
            Mail::to('[email protected]')->send(new Test());
        } catch  (\Exception $e) {
            dump($e);
        }

        echo 'hello2';

    }
}

  1. resources/views/test.blade.php
hello
  1. was using even so just in case
> artisan cache:clear
> artisan config:cache
> artisan cache:clear

9

please help, what is missing ? what i did wrong ?

Upvotes: 2

Views: 2544

Answers (2)

mwal
mwal

Reputation: 3113

The _HOST, _PORT, _USERNAME, _PASSWORD and _ENCRYPTION environment variables apply to SMTP only.

For that, your MAIL_DRIVER should be set to SMTP, with values for the five I just mentioned pointing to your mailgun values.

The _DOMAIN and _SECRET apply only to when you have set MAIL_DRIVER=mailgun (which means the mailgun api), and that means you are not using SMTP driver anymore.

Basically you have a choice: pick SMTP and point it to mailgun (set MAIL_DRIVER=smtp, OR mailgun API (set MAIL_DRIVER=mailgun).

I think if you think carefully till you understand the difference between those two, then you should be able to work it out.

Also, on the sandbox domain, you can't send real email unless you add the recipient address as a verified email. You do that on the mailgun dashboard. That might be what is causing the problem here (it is a sandbox after all - it's meant to be safe so you can't accidentally send to live email addresses you haven't specifically opted in with via an email verification by the specific address).

Another tip, go in to tinker and type config('mail') and you'll see what is getting picked up by the framework. Then you won't have to adjust the actual config files, if you see the values you want to put there are definitely there.

I also just noticed your config file you listed above is wrong. You have 'driver' => env('mailgun', 'mailgun'), That won't work (as you'll see if you do config('mail') in tinker). the env() function takes the name of an environment variable (like MAILGUN_DOMAIN or MAILGUN_SECRET as its first param, the second param is a default value if the shell environment variable of that name doesn't exist).

Upvotes: 4

kopz
kopz

Reputation: 783

You need to use the name of your ENV setting. In your case you are for example for the username trying to get an env variable named '[email protected]' that of course does not exist.

'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => env('SUPPORT', '[email protected]'), //if SUPPORT env does not exist, default to [email protected]
    'name' => env('NAME', 'Example'),
],
'username' => env('MAIL_USERNAME'),

'password' => env('PASSWORD'),

when using env , first parameter ie. ENV_NAME is the name you have set in your .env file and second (optional) parameter is the value to which it defaults if the first value is not found.

Upvotes: 1

Related Questions