0

I want to sent 2 mails, but from name is different for one, while for second its the default in env file

I tried this:

        Mail::send(
            'register.mail',
            ['record' => $record],
            function ($message) use ($record) {
                $message->to('[email protected]');
                $message->subject('New Store Registration Request');
            }
        );
        // Update the mail from name
        Config::set('mail.from.name', 'Custom name');  
        Mail::send(
            'register.welcomeMail',
            ['record' => $record],
            function ($message) use ($record) {
                $message->to($record['email']);
                $message->subject('Store Registration');
                // Update the mail from name back to "Portal"
                Config::set('mail.from.name', 'Portal');
            }
        );

But this send mail with "Portal" as from name to both of these mails, am I doing it wrong and how can I fix this?

1
  • 1
    there is from method itself in the Message object so you can just do $message->from(<address>, <name>); I do not recommend this at all. Try to look into Laravel's Mailable it is a much clearer and better way to handle mail. This thread will be helpful to you laracasts.com/discuss/channels/laravel/… Commented May 10, 2023 at 9:31

1 Answer 1

1

Well you can change it without using the config or changing the env variables

Since you are using the a callback instead of a class you can just do this :

use Illuminate\Mail\Message;

Mail::send(
    view: 'register.email',
    data: ['record', $record],
    callback: function (Message $message) {
        $message->to('[email protected]');
        $message->from('[email protected]', 'FromName');
        $message->subject('Mail Subject');
        
    }
);

The $message is a instance of Message class which where you can access the from() method to edit where the email come from. You can type hint it in the callback like the one I did so that you will be confuse.

You can also dig deeper to the Mail::send() to check how it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.