定义发件人以发送电子邮件

时间:2018-07-08 18:34:04

标签: laravel

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

  Mail::to($conference->
  organizer_email)
  ->send(new UserNotification($conference, $message, $subject));

它起作用了,但是您知道如何定义来源吗?例如,电子邮件应从经过身份验证的用户发送到会议组织者。你知道那需要什么吗?

UserNotification:

class UserNotification extends Mailable
{
    use Queueable, SerializesModels;

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



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


    public function build()
    {

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

已更新:

发送电子邮件至:

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

UserNotification:

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 but the email is 
        // sent to the email set in MAIL_USERNAME in .env file
        // instead of sent to the auth user email
        dd($this->user->email);

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

1 个答案:

答案 0 :(得分:0)

仅使用$this->from($senderEmail, $senderName) <接受两个参数。发件人电子邮件和可选的发件人名称。检查documentation以获得更好的理解。