在Laravel 5中发送带有不同回复地址的电子邮件

4

我在Laravel中使用SMTP配置了该电子邮件。它的工作正常。

我希望一些用户能够使用自己的电子邮件地址发送电子邮件。

我以前这样做:

Mail::to($receiver)->from("myconfiguredSTMPemail@mycompany.com")->send(new email());

我现在这样做:
Mail::to($receiver)->from($email_given_by_the_user)->send(new email());

这样做很好,但我不喜欢这样的方式,因为实际上是从我的电子邮件发送的,而不是从用户提供的电子邮件发送的,即使最终用户看到的是$email_given_by_the_user。我想以myconfiguredSTMPemail@mycompany.com的形式发送它,但当用户想要回复时,它会回复到$email_given_by_the_user。有什么办法可以解决这个问题吗?

可能是"Laravel邮件中的"Reply-to"字段不起作用"的重复问题。 - Alex Tartan
@AlexTartan 不是,这个是Laravel 5.4。我试过那一个,但不一样。 - prgrm
1个回答

13

在 Laravel 5.4 中的可发送电子邮件类(Mailables)中,可以在 build 方法内设置 replyTo、subject、cc、bcc 等属性。同时,也可以通过 Mail 门面设置 to 属性。

因此,您可以像这样操作:

$attributes = ['replyTo' => $email_given_by_the_user];    
Mail::to($receiver)->from("myconfiguredSTMPemail@mycompany.com")->send(new email($attributes));

和电子邮件类

class email extends Mailable
{
    public $attributes;

    public function __construct($attributes = null)
    {
        $this->attributes = $attributes;
    }

    public function build()
    {
        if(!empty($this->attributes['replyTo']))
            $this->replyTo($this->attributes['replyTo']);

        ...
    }

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接