Php 如何在回复地址中设置认证用户的电子邮件?

Php 如何在回复地址中设置认证用户的电子邮件?,php,laravel,Php,Laravel,我有以下代码向会议组织者发送电子邮件: public function contactOrganizer($id, Request $request){ $conference = Conference::find($id); $user = Auth::user(); $message = $request->message; $subject = $request->subject; Mail::to($conference->

我有以下代码向会议组织者发送电子邮件:

public function contactOrganizer($id, Request $request){
    $conference = Conference::find($id);

    $user = Auth::user();

    $message = $request->message;
    $subject = $request->subject;

    Mail::to($conference->organizer_email)->send(new UserNotification($conference, $user, $message, $subject));
    Session::flash('email_sent', 'Your email was sent with success for the conference organizer.');

    return redirect()->back();
}
使用此代码,电子邮件将发送给会议组织者,这是正确的。问题在于,在发件人地址中,不是显示发送电子邮件的身份验证用户的电子邮件,而是在MAIL_用户名中显示.env文件中配置的电子邮件。因此,会议组织者收到电子邮件,但不知道发送电子邮件的用户的电子邮件

那么,您知道如何使用经过身份验证的用户的电子邮件设置回复地址吗

用户通知

class UserNotification extends Mailable
{
    use Queueable, SerializesModels;

    public $conference;
    public $user;
    public $message;
    public $subject;


    public function __construct(Conference $conference, User $user, $message, $subject)
    {
        $this->conference = $conference;
        $this->user = $user;
        $this->message = $message;
        $this->subject = $subject;
    }

    public function build()
    {
        return $this
            ->from($this->user->email)
            ->to($this->conference->organizer_email)
            ->markdown('emails.userNotification', [
                'message' => $this->message,
                'subject' => $this->subject
            ]);
    }
}

您可以使用
replyTo

public function build()
    {
        return $this
            ->from($this->user->email)
            ->to($this->conference->organizer_email)
            ->replyTo($this->user->email, $this->user->name)
            ->markdown('emails.userNotification', [
                'message' => $this->message,
                'subject' => $this->subject
            ]);
    }
其中,
$this->user->email
$this->user->name
分别包含经过身份验证的用户的电子邮件地址和名称


Ref:

您只需将名称添加到
->from(),->replyTo()
方法。这将更加明确:

       public function build()
        {
            return $this
                ->from($this->user->email,$this->user->name)
                ->to($this->conference->organizer_email)
                // you can add another reply-to address
                ->replyTo($this->user->email, $this->user->name)
                ->markdown('emails.userNotification', [
                    'message' => $this->message,
                    'subject' => $this->subject
                ]);
        }