在Laravel 5中使用不同的回复发送电子邮件

prg*_*grm 2 php email laravel

我在Laravel中使用SMTP配置了此电子邮件.它运作良好.

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

我曾经这样做过:

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

我现在这样做:

Mail::to($receiver)->from($email_given_by_the_user)->send(new email());
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我不喜欢这样,因为我实际上是通过我的电子邮件发送它们,而不是来自用户提供的电子邮件,即使最终用户将其视为$email_given_by_the_user.我想发送它,myconfiguredSTMPemail@mycompany.com但当用户想要回复时,它回复$email_given_by_the_user.有没有办法做到这一点?

Aut*_*a_z 9

在Laravel 5.4 Mailables中,replyTo,subject,cc,bcc和其他可以在构建方法中的mailable中设置.对于也可以在Mail外观上设置的内容也是如此.

所以你可以这样做:

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

和电子邮件课程

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

        ...
    }
Run Code Online (Sandbox Code Playgroud)

}