Kevin Loquencio
Kevin Loquencio

Reputation: 411

Laravel 5 Reset password notification won't send

I'm trying to implement the default reset password notification in my Application, but when I try to do password reset, nothing happens it will just say "We have e-mailed your password reset link!" but when I try to check my mailbox no email has been sent by my applications.

I did all necessary configuration on my application: Email Driver, Database Set-up

But there are some part of the Laravel i did changed: User Model, User Table Migration

Table Columns I did changed.

id, name, US_EMAIL, password, remember_token, created_at, updated_at, EMP_POSITION, FACTORY, CONTACT, SIGNATURE, ONLINE, DATE_ONLINE, ADMIN, LOCK

I did nothing on the reset password table, all fields are intact from the default laravel migration.

While I'm trying to debug my application it seems when I try to reset my password the application can successfully save the data to "password_resets" table but again I still cannot receive email reset notification.

I also did look to this trait, i tried to "Dump and Die" to see where the process goes after I click "send password reset link" and it seems the application can still proceed to this trait because it still display the "dd" message.

    <?php

namespace Illuminate\Auth\Passwords;

use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;

trait CanResetPassword
{
    /**
     * Get the e-mail address where password reset links are sent.
     *
     * @return string
     */
    public function getEmailForPasswordReset()
    {
        return $this->EMAIL;
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        dd('Test SendPasswordNotification');
        $this->notify(new ResetPasswordNotification($token));
    }
}

I also did look to this but when I try to "Dump and die" to the "toMail" function it did not proceed. I was thinking maybe the application won't send an email because it cannot proceed to this class, I'm just guessing but i hope there's anyone can help me to this.

    <?php

namespace Illuminate\Auth\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
    /**
     * The password reset token.
     *
     * @var string
     */
    public $token;

    /**
     * Create a notification instance.
     *
     * @param  string  $token
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
      dd('Test ToMail');
        return (new MailMessage)
            ->line('You are receiving this email because we received a password reset request for your account.')
            ->action('Reset Password', route('password.reset', $this->token))
            ->line('If you did not request a password reset, no further action is required.');
    }
}

Update: I can send email using my .env email settings, I think the application can authenticate to the email server i used. The only problem is i cannot receive email notification for the password-reset and also it does not display any error after i click "send password reset link".

Upvotes: 6

Views: 4780

Answers (4)

Kevin Loquencio
Kevin Loquencio

Reputation: 411

I finally found where the problem is.

When i change the email column from the user table to "US_EMAIL" there is a function from the Illuminate\Notifications trait that retrieves the email column.

Illuminate\Notifications

  public function routeNotificationFor($driver)
{


    if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
        return $this->{$method}();
    }


    switch ($driver) {
        case 'database':
            return $this->notifications();
        case 'mail':
            return $this->email;
        case 'nexmo':
            return $this->phone_number;
    }
}

Solution:

on the "routeNotificationFor" function i changed this line "return $this->email;"

to:

to " return this->US_EMAIL;"

Upvotes: 5

Eric
Eric

Reputation: 1197

It sounds to me like you should check your config/mail.php and .env to verify your email settings. My initial guess is you have it set to "mail" or "sendmail" and don't have either installed on your machine or server.

Maybe create a free account with an external email service (mailgun, sparkpost, etc) and set it up to rule that out?


EDIT: After reading again, I think you problem is this:

public function getEmailForPasswordReset() { return $this->EMAIL; }

Based on the top of your question the "email" field in your DB is actually "US_EMAIL".

public function getEmailForPasswordReset() { return $this->US_EMAIL; }

Upvotes: 0

Advaith
Advaith

Reputation: 2580

There are 3 things to check

First: .env file

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

This is for production and gmail message

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=usernamefrommailtrap
MAIL_PASSWORD=somepasswordfrommailtrap
MAIL_ENCRYPTION=null

This is for testing and mailtrap message

Second: config/mail.php

Be sure that your config file is set correctly with correct data's. Or make sure it is calling correct information from .env file

Third: Refresh

After editing .env file don't forget to stop the php artisan serve and run it again.
After editing config/mail.php file don't forget to run php artisan config:cache to delete saved cache and create a new one

Upvotes: 0

claudios
claudios

Reputation: 6656

Please check your .env file and look for MAIL_DRIVER. Create a dummy email in email tester like Mailtrap.io

It should look like below using Mailtrap.io:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=usernamefrommailtrap
MAIL_PASSWORD=somepasswordfrommailtrap
MAIL_ENCRYPTION=null

Now test it and check your dummy email in Mailtrap if you receive the email.

Upvotes: 0

Related Questions