简体   繁体   中英

Laravel mailable not sending email

I am looking at sending a welcome email event via a Eloquent model using the saved event.

Everything seems to be working okay, up until actually sending the email. So I have added this at the top of the User.php model.

protected $dispatchesEvents = [
  'saved' => \App\Events\UserCreated::class
];

The UserCreated.php file just contains the basic boilerplate and assigning the user as well

<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Queue\SerializesModels;

class UserCreated extends Event
{
    use InteractsWithSockets, SerializesModels;

    /**
     * @var User
     */
    public $user;

    /**
     * Create a new event instance.
     *
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

Inside of the EventServiceProvider.php I am listening for then triggering the SendWelcomeEmail.php file.

 protected $listen = [
    'App\Events\UserCreated' => [
        'App\Listeners\SendWelcomeEmail'
    ]
];

SendWelcomeEmail.php looks like this...

<?php

namespace App\Listeners;

use App\Events\UserCreated;
use App\Mail\WelcomeEmail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail
{

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserCreated  $event
     * @return void
     */
    public function handle(UserCreated $event)
    {
        $user = $event->user;

        Mail::to($user->email_address)->send(new WelcomeEmail($user));
    }
}

So far, I can do a dd inside of the WelcomeEmail class and I can see it display on screen, however the request completes, I get the response back and I get no error. However, the mail never actually sends to the user.

I am using SendGrid for this and I have followed everything exactly what they have put in, I cannot see any requests going into SendGrid and the request from my end is far to quick to actually send the email.

I have registered the mail config and the MailServiceProvider inside of app.php so I really don't know what it could be.

I also 100% know I am getting the correct user email address and dumping that out I am getting the correct input.

Thanks in advance.

I think your method call may be incorrect. According to the docs (for 5.1 anyways), you're supposed to wrap the to() function in a callback:

Mail::send('your.email.blade', $blade_variables, function($msg) use ($user) {
    $msg->to($user->email_address);
});

See https://laravel.com/docs/5.1/mail#sending-mail for more info.

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