jack
jack

Reputation: 155

Laravel password reset "No Sender"

When I do a password reset in Laravel 5.1, I get the email, but it says "No Sender" in the title.

Is there a way to specify the sender from somewhere for password reset emails? Apart from the sender, I assume Laravel should automatically uses the email settings as specified in the config files? It's strange because when I set the Laravel mail config to use the 'mail' driver, I get bounced emails saying I can't send from a dynamic address (which is to be expected on dev), but still the password reset emails go through. Shouldn't email reset use the same config settings?

Upvotes: 3

Views: 3575

Answers (5)

Rodrigo Klim
Rodrigo Klim

Reputation: 126

Old question but in Laravel 10 in 2023, we handle this easily filling those fields on .env file

MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

Now if you wanna override and write your own email solution you can pass a closure callback function on line 47 of PasswordResetLinkController, it took me a while to learn it.

 $status = Password::sendResetLink(
        $request->only('email'),
        function ($user, $token) {
            $this->sendResetEmail($user->email, $token)

            return Password::RESET_LINK_SENT;
        }
    );

Upvotes: 0

shakee93
shakee93

Reputation: 5386

@SeriousJelly Answer Update for Laravel 5.2

in your Auth PasswordController Override resetEmailBuilder Method

class PasswordController extends Controller
{
    protected function resetEmailBuilder()
    {
        return function (Message $message) {
            $message->subject($this->getEmailSubject());
            $message->from('[email protected]', 'you');
        };
    }
}

This might help someone

Upvotes: 6

Rajkumar
Rajkumar

Reputation: 141

Jack, you can set in the From attributes for email id and name in config/mail.php. I too had the same problem and just got it sorted, as i mentioned above.

Upvotes: 14

ChrisBratherton
ChrisBratherton

Reputation: 1590

So, Alexey Mezenin's answer is almost there, however, one big no no is overwriting the core files as any future updates can break functionality.

As your PasswordController should be using the ResetsPassword trait you should be able to just overwrite any methods from ResetsPassword trait in your PasswordController.

For example, adding your own from and subject line to emails is a simple case of finding the relevant function in your trait, copy and pasting into your PasswordController and amending it.

Here is an example PasswordController with a function that over writes the sendResetLinkEmail() function.

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */

    use ResetsPasswords;

    /**
     * Create a new password controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function sendResetLinkEmail(Request $request)
    {

        $this->validate($request, ['email' => 'required|email']);

        $broker = $this->getBroker();

        $response = Password::broker($broker)->sendResetLink($request->only('email'), function (Message $message) {
            $message->subject($this->getEmailSubject());
            $message->from(env('MAIL_FROM'), env('APP_NAME'));
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return $this->getSendResetLinkEmailSuccessResponse($response);

            case Password::INVALID_USER:
            default:
                return $this->getSendResetLinkEmailFailureResponse($response);
        }
    }

Upvotes: 4

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Maybe there is a better solution, but you could manually add code into \vendor\laravel\framework\src\Illuminate\Foundation\Auth\ResetPasswords.php, after this line:

$message->subject($this->getEmailSubject()); // this is line 66

Add something like this:

$message->from('[email protected]', 'My Site');

https://laravel.com/docs/5.1/mail#sending-mail

Upvotes: 0

Related Questions