如何在 Laravel 邮件通知中更改 From Name

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41972333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:38:19  来源:igfitidea点击:

How to change From Name in Laravel Mail Notification

laravelemaillaravel-notification

提问by Masterpiece Mas Icang

This is the problem:

这就是问题:

This is the problem

这就是问题

I need help for this one. Thanks

我需要这方面的帮助。谢谢

回答by Dev

In config/mail.php

config/mail.php

set fromproperty as:

from属性设置为:

'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.

PS 这将是您发送的每封电子邮件的默认电子邮件设置。

回答by Haseena P A

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

参考 - https://laravel.com/docs/5.0/mail

回答by Kankuro

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

更好的方法是在.env文件中添加变量名称和值。

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 nameand from emailfields within the Email that is sent.

注意最后两行。这些将与发送的电子邮件中的发件人名称发件人电子邮件字段相关联。

回答by AlmostPitt

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

对于使用Laravel 5.8并遇到此问题的任何人,请试一试,它对我有用

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 :)

快乐编码:)

回答by Kevin RED

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.

在 google SMTP 的情况下,即使您在邮件类中提供此地址,发件人地址也不会更改。这是由于谷歌邮件的政策,而不是 Laravel 问题。以为我会在这里分享。

回答by Connectify_user

  • 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]

  • 如果您想要全局“来自名称”和“来自电子邮件”,

  • 在 .env 文件中创建这两个键

  • MAIL_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.

并删除控制器上的“from”。或 php 代码,如果您手动声明。现在它可以通过姓名和电子邮件进行访问。

config\mail.php

配置\邮件.php

 'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'info@[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>

回答by InfoTronika

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'))

我认为您的代码片段中有错误。你有
from(config('app.senders.info'), 'My Full Name')
config('app.senders.info') 返回数组。
方法 from 应该有两个参数:第一个是包含地址的字符串,第二个是包含发件人姓名的字符串。所以你应该把它改成
from(config('app.senders.info.address'), config('app.senders.info.name'))