dsqve08622
2018-07-25 14:00
浏览 29
已采纳

如何在回复地址中设置经过身份验证的用户的电子邮件?

I have this code to send an email to the conference organizer:

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

With this code the email is sent to the conference organizer, that is correct. The issue is that in the from address, instead of appear the email of the authenticted user, the user that sent the email, it appears the email configured in the .env file in the MAIL_USERNAME. So the conference organizer receives the email but dont know the email of the user that sent the email.

So, do you know how to set a reply-to address with the email of the authenticated user?

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条回答 默认 最新

  • dongxi7722 2018-07-25 14:07
    已采纳

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

    Where $this->user->email and $this->user->name contains the email address and name of the authenticated user respectively.

    Ref: https://laravel.com/docs/5.1/mail

    点赞 评论
  • dot_0620 2018-07-25 14:14

    You can simply add name into ->from(),->replyTo() methods. This will be more clear:

           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
                    ]);
            }
    
    点赞 评论

相关推荐 更多相似问题