Sidney Sousa
Sidney Sousa

Reputation: 3604

I cannot send email using gmail even though I already allowed less secure apps | Laravel

I have an observer that fires off an event if a user updated his email address.

public function updated(User $user)
{
    if($user->isDirty('email')){
        $new_email = $user->email; 
        $old_email = $user->getOriginal('email');

        //dd($user);
        event(new UserUpdate($user, $old_email));
    }
}

In the UserUpdate event I have the following inside the constructor:

public function __construct($user, $old_email)
{
    $this->user = $user;
    $this->old_email = $old_email;
}

In the event listener, I want to send an email to the old email address.

public function handle(UserUpdate $event)
{
    $user = $event->user;
    Mail::to($event->old_email)->send(new UserUpdated($user));
}

And the Mail class looks like this:

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

public function build()
{
    $this->subject("Updated Email");
    return $this->view('emails.updatedUser');
}

When I update the user, I get the following error:

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required

I setup the email in the .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls

in the mail.php I also edited a few details:

'host' => env('MAIL_HOST', 'smtp.gmail.com'),

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Admin Email'),
],

I went to my email and enabled the option to allow less secure apps.

How can I get my emails to work?

Upvotes: 0

Views: 156

Answers (1)

Sidney Sousa
Sidney Sousa

Reputation: 3604

Using this question as a reference, I then noticed that all I needed to do was to restart my server and clear cache.

Upvotes: 0

Related Questions