将 Laravel 控制器中的 MAIL_FROM_NAME 值更改。

huangapple go评论98阅读模式
英文:

Change MAIL_FROM_NAME value from laravel controller

问题

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:

  1. Mail::send(
  2. 'register.mail',
  3. ['record' => $record],
  4. function ($message) use ($record) {
  5. $message->to('usama@gmail.com');
  6. $message->subject('New Store Registration Request');
  7. }
  8. );
  9. // Update the mail from name
  10. Config::set('mail.from.name', 'Custom name');
  11. Mail::send(
  12. 'register.welcomeMail',
  13. ['record' => $record],
  14. function ($message) use ($record) {
  15. $message->to($record['email']);
  16. $message->subject('Store Registration');
  17. // Update the mail from name back to "Portal"
  18. Config::set('mail.from.name', 'Portal');
  19. }
  20. );

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?

英文:

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:

  1. Mail::send(
  2. 'register.mail',
  3. ['record' => $record],
  4. function ($message) use ($record) {
  5. $message->to('usama@gmail.com');
  6. $message->subject('New Store Registration Request');
  7. }
  8. );
  9. // Update the mail from name
  10. Config::set('mail.from.name', 'Custom name');
  11. Mail::send(
  12. 'register.welcomeMail',
  13. ['record' => $record],
  14. function ($message) use ($record) {
  15. $message->to($record['email']);
  16. $message->subject('Store Registration');
  17. // Update the mail from name back to "Portal"
  18. Config::set('mail.from.name', 'Portal');
  19. }
  20. );

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

  1. 您可以在不使用`config`或更改`env`变量的情况下进行更改。
  2. 由于您使用的是回调而不是类,您可以这样做:

use Illuminate\Mail\Message;

Mail::send(
view: 'register.email',
data: ['record', $record],
callback: function (Message $message) {
$message->to('usama@gmail.com');
$message->from('from@email.com', '发件人姓名');
$message->subject('邮件主题');
}
);

  1. `$message``Message`类的实例,您可以使用`from()`方法来编辑电子邮件的发件人地址。您可以像我所做的那样在回调中进行类型提示,以避免混淆。
  2. 您还可以深入了解`Mail::send()`的工作原理。
英文:

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 :

  1. use Illuminate\Mail\Message;
  2. Mail::send(
  3. view: 'register.email',
  4. data: ['record', $record],
  5. callback: function (Message $message) {
  6. $message->to('usama@gmail.com');
  7. $message->from('from@email.com', 'FromName');
  8. $message->subject('Mail Subject');
  9. }
  10. );

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.

huangapple
  • 本文由 发表于 2023年5月10日 16:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定