在Laravel 5.4中更改FROM和REPLYTO地址

Bos*_*IGA 5 laravel laravel-5 laravel-5.4

我无法发送带有用户地址的电子邮件作为FROM和回复

在FormRequest中:

    public function persist()
{
    $reservation = Resa::create(
        $this->only(['nom', 'email', 'phone', 'formule', 'date_arr', 'date_ret', 'nb_adu', 'nb_enf', 'lemessage'])
    );
    Mail::to('contact@cotiga.fr')
    ->from($reservation->email, $reservation->nom)
    ->replyTo($reservation->email, $reservation->nom)
    ->send(new Reservation($reservation));

}
Run Code Online (Sandbox Code Playgroud)

我有错误:

FatalThrowableError in ReservationForm.php line 48:
Call to undefined method Illuminate\Mail\PendingMail::from()
Run Code Online (Sandbox Code Playgroud)

我尝试了充分的可能性,但我无法改变字段FROM和REPLYTO你能帮助我吗?谢谢

iga*_*ter 9

Mail门面不实现replyTo()了方法.相反,这种方法已经转移到了Mailable类本身.官方文档建议使用该build()方法来设置Mailable,但这并不总是方便的(例如,每次replyTo字段可能不同)

但是,如果您仍想使用类似的语法,则可以使用:

$mailable = new myMailableClass;
$mailable->replyTo('reply@to.com');

Mail::to('email@tocom')
  ->send($mailable);
Run Code Online (Sandbox Code Playgroud)

有关Mailable类的可用方法的完整列表,请参阅Mailable文档


Ari*_*sta 6

在 Laravel 5.4 Mailables 中replyTosubjectccbcc等可以在build方法中的mailable内设置。这也适用于to也可以在 Mail 外观上设置的 。

这是一个使用属性数组可邮寄的联系表单的简单示例:

您可以to直接在MailFacade上使用静态方法,但作为示例,我们将在 mailable 中设置它:

Mail::send(new ContactCompany($attributes));
Run Code Online (Sandbox Code Playgroud)

然后设置replyTo里面的build方法:

class ContactCompany extends Mailable
{
    use Queueable, SerializesModels;

    public $attributes;

    /**
     * Create a new message instance.
     *
     * @param $attributes
     */
    public function __construct($attributes)
    {
        $this->attributes = $attributes;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->to($this->attributes['departmentEmail'], config('app.name'));
        $this->replyTo($this->attributes['email'], $this->attributes['name']);
        $this->subject(sprintf("New contact message from %s", $this->attributes['name']));

        return $this->markdown('emails.contact.company');
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,Mail::alwaysFrom()Mail::alwaysReplyTo()之前可以使用Mail::send()设置fromreplyTo所有电子邮件,所以一定要小心使用他们。


Mar*_*łek 0

现在发送电子邮件的首选方法是可邮寄,您可以使用 from() 或replyTo() 方法设置发件人和回复。

然而,使用普通的Mail外观,您应该尝试使用alwaysFromalwaysReplyTo方法。但是,发送此类电子邮件后,您应该再次设置以前的值,以确保其他电子邮件不会受到此更改的影响。

但从方法名称来看,它可能不是最好的解决方案,所以更好的方法是查看 mailables 并使用它们在最新的 Laravel 版本中发送电子邮件。