Change FROM and REPLYTO address in Laravel 5.4

I can not send an email with the user address as FROM and Reply to

In 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));

}

I have an error:

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

I tried the full feature, but I can’t change the FROM and REPLYTO fields. Can you help me? Thanks x

+4
source share
4 answers

MailThe facade does not implement the method replyTo()anymore. Instead, this method has moved to the class Mailable. The official documentation suggests using a method build()to configure Mailable, however, this is not always convenient (for example, the replyTo field may be different each time)

, , :

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

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

Mailable . Mailable Documentation

+7

Laravel 5.4 Mailables, replyTo, subject, cc, bcc build. to, .

, :

to Mail, :

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

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

, , Mail::alwaysFrom() Mail::alwaysReplyTo() Mail::send(), from replyTo , .

+2

mailables, from() replyTo() .

Mail, alwaysFrom alwaysReplyTo. , , .

But, looking at the names of the methods, this may not be the best solution, so it’s better to look at the mail and use them to send emails in the latest releases of Laravel.

0
source

The problem is resolved. I am editing the application> Mail> Reservation.php

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

Application> Http> Requests> 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('contact@cotiga.fr')->send(new Reservation($reservation));
}
0
source

Source: https://habr.com/ru/post/1671666/


All Articles