Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boldstar's avatar

How To Change From Address On Mail Dynamically

So I am sending mail from my application and I want to change the from email address and name dynamically. I see that you can use a global setting in the config file but that will not do what I am looking for.

So here is where I am adding the from address dynamically but when the email is sent it does not use the $email property in the from($email, $name). However the replyTo works fine. What could I be doing wrong?

As you see below I want the from address to be from the user making the send email request...

$sender = User::where('id', auth()->user()->id)->first();
        $email = $sender->email;
        $name = $sender->name;

        return $this->from($email, $name)
                    ->replyTo($email, $name)
                    ->subject('Pending Questions From '. $name)
                    ->view('email');
0 likes
8 replies
aurawindsurfing's avatar

Hi @boldstar

Did you create a mailable class inside of app/Mail using an artisan command: php artisan make:mail NewMail ? if so then you are doing it correctly but you probably do not construct it correctly. To do this you will call it in your code like this:

 Mail::to($contact->email)->send(new NewMail($data));

then in your mailable class:

    public $name;

    public $email;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->name = $data['name'];
        $this->email = $data['email'];
    }

and then:


public function build()
    {
        
        return $this->from($this->name)
                    ->replyTo($this->email)
                    ->subject('Pending Questions From '. $this->name)
                    ->view('email');
    }

Hope it helps!

3 likes
ranamoizhaider's avatar

The rest is right but I think the last function you mentioned should be like this

	public function build()
    {
        
        return $this->from($this->email, $this->name)
                    ->replyTo($this->email, $this->name)
                    ->subject('Pending Questions From '. $this->name)
                    ->view('email');
    }
Snapey's avatar

Not related, but I see this so often;

$sender = User::where('id', auth()->user()->id)->first();

auth()->user() is already the user model. No need to query it again.

4 likes
vandan's avatar

Gmail doesn't allow you to change the FROM to something different than your gmail account.

It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.

1 like
boldstar's avatar

Thanks all for the responses. Yes I was trying to use gmail to send emails and it is true that they override whatever value you have set with default values. It can be changed in gmail but you will have to register each individual email you want to use as a sender and this is not dynamic so not worth trying. However thank you for the help.

@snapey , thanks for the advice on the auth()->user() so I can just call ('id', id) instead?

and @aurawindsurfing , I did use the mailable class. I pass in the data like so however the user was queried on the StartConversation class and not passed in as data attribute.

Perhaps there is a better way to do this but I call a sendMail() function in my controller like this

public function sendMail(Request $request)
    {
        $data = $request->validate([
            'id' => 'required|integer'
        ]);

        $question = Question::where('id', $request->id)->first();
        $engagement = Engagement::where('id', $question->engagement_id)->first();
        $client = CLient::where('id', $engagement->client_id)->first();

        Mail::to($client->email)->send(new StartConversation(['question' => $question, 'engagement' => $engagement, 'client' => $client]));

        $question->email_sent = true;
        $question->save();

        return response()->json([ 'question' => $question, 'message' => 'Email Was Sent']);
    }

which calls the StartConversation mailable class

 /**
     * The demo object instance.
     *
     * @var Client
     */
    public $client;
 
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($client)
    {
        $this->client = $client;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {   
        $account = Account::first();
        $sender = User::where('id', auth()->user()->id)->first();
        $email = $sender->email;
        $name = $sender->name;

        return $this->from($email, $name)
                    ->replyTo($email, $name)
                    ->bcc($email, $name)
                    ->subject('Pending Questions From '. $account->business_name)
                    ->view('email')
                    ->with([
                        'phoneNumber' => $account->phone_number,
                        'faxNumber' => $account->fax_number,
                        'accountName' => $account->business_name,
                        'accountEmail' => $account->email,
                        'userEmail' => $email
                    ]);
    }
}
1 like
Snapey's avatar

No, If you want the current user model, just do;

$sender = auth()->user();

Then you can

$email = $sender->email;

but why bother when you can just as easily

$email = auth()->user()->email;

But why bother with intermediate temporary variables...

return $this->from($email, auth()->user()->email)   //not needing $sender or $email
2 likes

Please or to participate in this conversation.