Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 mailgun not sending emails - no errors

I am trying to send emails with mailgun but they won't send and I have no idea why because i don't get any errors at all.

This is my code:

mail.php:

'driver' => env('MAIL_DRIVER', 'mailgun'),

services.php:

'mailgun' => [
    'domain' => env('sandbox1e...60.mailgun.org'),
    'secret' => env('key-146...419'),
],

EmailController.php:

public function send($email, $uuid = null)
{
    if($uuid == null){
        $uuid = User::get()->where('customer_email' , $email)->first()->email_confirmed;
    }

    return Mail::to($email)->send(new ConfirmEmail($uuid));

}

ConfirmEmail.php:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ConfirmEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $uuid;

    public function __construct($uuid)
    {
        $this->uuid = $uuid;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('[email protected]')
            ->view('emails.confirm');
    }
}

I have added the emailadress I want to send to in mailgun, but it's not working. Am I doing something wrong or is there any way I can debug this?

like image 408
Luuk Wuijster Avatar asked Jan 17 '18 20:01

Luuk Wuijster


2 Answers

Your configuration is wrong:

'mailgun' => [
    'domain' => env('sandbox1e...60.mailgun.org'),
    'secret' => env('key-146...419'),
],

The env function looks for an environment variable with the name you provide and returns the value. You should change it to the name of an environment variable and define it in your .env or don't use the env function, but that's not recommended.

like image 58
Esteban Garcia Avatar answered Oct 05 '22 23:10

Esteban Garcia


Whereas Esteban Garcia's answer is correct, I to wish improve it with code snippets showing how exactly the configuration should look like:

In your config/services.php, leave the configuration as shown below:

'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],

In your .env file, that is where you define the actual mailgun credentials:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox1e...60.mailgun.org
MAILGUN_SECRET=key-146...419
like image 38
Richie254 Avatar answered Oct 06 '22 00:10

Richie254