如何在laravel 8中动态指定from::send



从我网站上的联系表单,我向访问者发送一封确认电子邮件,并向网站管理员发送一封电子邮件。两个电子邮件都将发送到.env中定义的电子邮件地址。如何更改发送给管理员的电子邮件的from字段?我当前的代码,其中第二个Mail::给出了一个错误。

// send html email to user
Mail::to(request('email'))
->send(new ContactWebsite($emailFields));
// send html email to admin
Mail::to("newemail@address")
->from(request('email'))
->send(new ContactWebsite($emailFields));

Daniel解决方案很好,但我如何在我的情况下实现它?

在Contact controller存储函数中,我将fromAddress放在$emailFields对象中:

$emailFields = (object) [
'fromName' => $fullnameUser,
'fromEmail' => request('email'),
'fromAddress' => $fullnameUser.' <'.request('email').'>',
'subject' => '...',
'body' => request('body')
];

Then in the Mailable:

public function __construct($emailFields) {
$this->emailFields = $emailFields;
$this->fromAddress = $emailAddress['fromAddress'];
}
public function build() {
return $this->markdown('emails.contact-confirm-user');
}

__construct函数的语法正确吗?

我如何在构建函数中传递$this->fromAddress ?

您应该在Mailable类的build()函数(在您的示例中为ContactWebsite)中定义->from()部分,作为$emailFields的一部分,或作为第二个参数。然后在build函数中使用它:

class ContactWebsite extends Mailable
{
use Queueable, SerializesModels;

private $fromAddress = 'default@value.com';
public function __construct($emailFields, $fromAddress = null)
{
if ($fromAddress) {
$this->fromAddress = $fromAddress;
}
}
public function build()
{
return $this->from($this->fromAddress)
// Whatever you want here
->send()
}
}

相关内容

  • 没有找到相关文章

最新更新