在 Laravel 5.4 中更改 FROM 和 REPLYTO 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42639459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:29:20  来源:igfitidea点击:

Change FROM and REPLYTO address in Laravel 5.4

laravellaravel-5laravel-5.4

提问by Boss COTIGA

i can't send email with user address as FROM and Reply To

我无法发送用户地址为 FROM 和 Reply To 的电子邮件

In the FormRequest :

在 FormRequest 中:

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

}

I have the error :

我有错误:

FatalThrowableError in ReservationForm.php line 48:
Call to undefined method Illuminate\Mail\PendingMail::from()

I tried full of possibility, but I can not change the field FROM and REPLYTO Can you help me ? Thank's

我尝试了很多可能性,但我无法更改字段 FROM 和 REPLYTO 你能帮我吗?谢谢

回答by igaster

The MailFacade does not implement the replyTo()method anymore. Instead this method has moved to the Mailableclass itself. Official documentation proposes to use the build()method to setup the Mailable, however this is not always convenient (eg the replyTo field might be different each time)

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

However if you still want to use a similar syntax you can use:

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

$mailable = new myMailableClass;
$mailable->replyTo('[email protected]');

Mail::to('email@tocom')
  ->send($mailable);

For a complete list of available methods on the Mailable class see the Mailable Documentation

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

回答by Arian Acosta

In Laravel 5.4 Mailables, the replyTo, subject, cc, bccand others can be set inside the mailable in the buildmethod. This is also true for the towhich can also be set on the Mail facade.

在 Laravel 5.4 Mailables 中replyTosubjectccbcc等可以在build方法中的mailable内设置。对于to也可以在 Mail 外观上设置的也是如此。

Here is a simple example of a contact form mailable using an array of attributes:

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

You may use the tostatic method directly on the Mailfacade, but as an example, we're going to set it inside the mailable:

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

Mail::send(new ContactCompany($attributes));

Then set the replyToinside the buildmethod:

然后设置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');
    }
}

Please, note that Mail::alwaysFrom()and Mail::alwaysReplyTo()can be used before Mail::send()to set the fromand replyToof allemails, so make sure to use them with care.

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

回答by Marcin Nabia?ek

The preferred method of sending emails are now mailablesand you can set from and reply to using from() or replyTo() methods.

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

However using plain Mailfacades you should try to use alwaysFromand alwaysReplyTomethods. However after sending such e-mail you should set again previous values to be sure no other emails will be impacted by this change.

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

But looking at method names it might be not the best solution so better is to look at mailables and use them to send e-mails in latest Laravel releases.

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

回答by Boss COTIGA

Problem resolved. I edit app>Mail>Reservation.php

问题解决了。我编辑 app>Mail>Reservation.php

    public function build()
    {
//      return $this->markdown('emails.reservation-email');
        return $this->from($this->reservation->email)->markdown('emails.reservation-email');
    }

app>Http>Request>ReservationForm.php

app>Http>Request>ReservationForm.php

public function persist()
{
    $reservation = Resa::create(
        $this->only(['nom', 'email', 'phone', 'formule', 'date_arr', 'date_ret', 'nb_adu', 'nb_enf', 'lemessage'])
    );
    Mail::to('[email protected]')->send(new Reservation($reservation));
}