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

时间:2018-07-25 14:00:55

标签: php laravel

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

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_USERNAME中.env文件中配置的电子邮件。因此,会议组织者会收到电子邮件,但不知道发送电子邮件的用户的电子邮件。

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

UserNotificaiton

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

2 个答案:

答案 0 :(得分:0)

您可以使用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分别包含已认证用户的电子邮件地址和名称。

参考:https://laravel.com/docs/5.1/mail

答案 1 :(得分:0)

您只需将名称添加到 ->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
                ]);
        }