【问题标题】:Mail from is not working properly (from email address is not appearing correctly)来自的邮件无法正常工作(来自电子邮件地址的显示不正确)
【发布时间】:2018-07-09 14:42:05
【问题描述】:

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

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

【问题讨论】:

  • 你使用的是什么版本的 Laravel?
  • 它的版本是 5.5。
  • 您是否使用 gmail 作为您的 smtp?
  • 是的,发件人电子邮件和收件人电子邮件都是 gmail 电子邮件。

标签: php laravel laravel-5.5


【解决方案1】:

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

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

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

appname@gmail.com

否则您将不得不使用另一个邮件驱动程序。

【讨论】:

    【解决方案2】:

    编辑您的用户通知

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

    【讨论】:

    • 谢谢,但同样的问题,发件人中出现的电子邮件地址也是“MAIL_USERNAME”中 .env 文件中设置的电子邮件地址。
    • 如果“MAIL_USERNAME=" 为空,则发送电子邮件时会出现错误“预期响应代码为 250,但收到代码为“530”。
    • @John dd($conference->organizer_email);不为空?
    • 不是,是会议组织者的邮箱。
    • @John 邮件添加到日志?或发送到真实的电子邮件?
    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2020-04-26
    • 2018-07-09
    相关资源
    最近更新 更多