How to change From Name in Laravel Mail Notification
Asked Answered
J

7

53

This is the problem:

This is the problem

The name associated with the email shows up as "Example"

Jez answered 1/2, 2017 at 5:13 Comment(1)
->from('[email protected]', 'Awesome Name')Edgaredgard
S
79

In config/mail.php

set from property as:

'from' => ['address' => '[email protected]', 'name' => 'Firstname Lastname']

Here, address should be the one that you want to display in from email and name should be the one what you want to display in from name.

P.S. This will be a default email setting for each email you send.

If you need to use the Name as a variable through code, you can also call the function from() as follows (copying from Brad Ahrens answer below which I think is good to mention here):

return $this
    ->from($address = '[email protected]', $name = 'Sender name')
    ->subject('Here is my subject')
    ->view('emails.view');
Sobel answered 1/2, 2017 at 5:20 Comment(5)
Thank for replying, even though i have found it first then your comment..Jez
Of course yes. But in the said comment you need to do this in each and every Mail::send() method wherever you use it. But in my case you don't need to do it. Thanks for accepting this answer.Sobel
if we configure like this, can we overwrite the from address as mentioned in the above comment?Ell
Yes, you can. Please see answer below from @Haseena P A.Sobel
->from('[email protected]', 'Website Name') worked for me. Thanks.Lightner
S
36

You can use

 Mail::send('emails.welcome', $data, function($message)
    {
        $message->from('[email protected]', 'Laravel');

        $message->to('[email protected]')->cc('[email protected]');
    });

Reference - https://laravel.com/docs/5.0/mail

Snowball answered 1/2, 2017 at 5:59 Comment(0)
E
17

In the case of google SMTP, the from address won't change even if you give this in the mail class. This is due to google mail's policy, and not a Laravel issue. Thought I will share it here.

Ell answered 27/8, 2019 at 9:21 Comment(0)
T
16

A better way would be to add the variable names and values in the .env file.

Example:

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

MAIL_FROM_NAME="My Name"
[email protected]

Notice the last two lines. Those will correlate with the from name and from email fields within the Email that is sent.

Transposition answered 3/3, 2019 at 5:7 Comment(0)
S
11

For anyone who is using Laravel 5.8 and landed on this question, give this a shot, it worked for me:

Within the build function of the mail itself (not the view, but the mail):

public function build()
{
    return $this
        ->from($address = '[email protected]', $name = 'Sender name')
        ->subject('Here is my subject')
        ->view('emails.welcome');
}

Happy coding :)

Selfridge answered 12/8, 2019 at 11:23 Comment(0)
P
3
  • If you want global 'from name' and 'from email',

  • Create these 2 keys in .env file

  • MAIL_FROM_NAME="global from name"

  • MAIL_FROM_ADDRESS[email protected]

And remove 'from' on the controller. or PHP code if you declare manually. now it access from name and from email.

config\mail.php

 'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'write name if not found in env'),
    ],

ON my controller.

$conUsBody = '';

        $conUsBody .= '<h2 class="text-center">Hello Admin,</h2>
                        <b><p> '.trim($request->name).' Want some assesment</p></b>
                        <p>Here are the details:</p>
                        <p>Name: '.trim($request->name).'</p>
                        <p>Email: '.trim($request->email).'</p>
                        <p>Subject: '.trim($request->subject).'</p>';

      $contactContent = array('contactusbody' =>  $conUsBody);

      Mail::send(['html' => 'emails.mail'], $contactContent,

      function($message) use ($mailData)
      {
        $message->to('[email protected]', 'Admin')->subject($mailData['subject']);
        $message->attach($mailData['attachfilepath']);
      });
      return back()->with('success', 'Thanks for contacting us!');
    }

My blade template.

<body>
{!! $contactusbody !!}
</body>
Pain answered 6/4, 2020 at 13:30 Comment(0)
R
0

I think that you have an error in your fragment of code. You have
from(config('app.senders.info'), 'My Full Name')
so config('app.senders.info') returns array.
Method from should have two arguments: first is string contains address and second is string with name of sender. So you should change this to
from(config('app.senders.info.address'), config('app.senders.info.name'))

Rodd answered 5/6, 2018 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.