来自的邮件无法正常工作(来自电子邮件地址的显示不正确)

Mail from is not working properly (from email address is not appearing correctly)

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

   $user = Auth::user();
     $conference = Conference::find($id);
     $message = $request->message;
     $subject = $request->subject;
    Mail::to($conference->organizer_email)
    ->send(new UserNotification
    ($conference, $user, $message, $subject));

使用此代码,电子邮件将发送给会议组织者,这是正确的。问题是在发件人地址中,没有出现经过身份验证的用户的电子邮件,即发送电子邮件的用户,而是出现在 MAIL_USERNAME 中的 .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()
    {
         // shows the auth user email so why the received email is 
        // appears that was sent from the email
        // set in MAIL_USERNAME in .env file
        // instead of appear the auth user email?
        dd($this->user->email);

        return $this->from($this->user->email)->markdown('emails.userNotification', [
            'message' => $this->message,
            'subject' => $this->subject
        ]);
    }
}

编辑您的用户通知

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($conference->organizer_email)
            ->markdown('emails.userNotification', [
               'message' => $this->message,
              'subject' => $this->subject
          ]);
   }
}

您不能将发件人电子邮件传递给 gmail 的 smtp,因为会覆盖 headers 以根据您的帐户配置设置发件人电子邮件。

您可以更新帐户配置以添加不同的发件人:https://support.google.com/mail/answer/22370?hl=en

否则,您可以专门为您的应用创建另一个 gmail 帐户。

appname@gmail.com

或者您将不得不使用其他邮件 driver。