Laravel 邮件提供发件人详细信息

Laravel mail provide sender detail

我需要获取我的用户电子邮件地址以及表单数据,以便在我的可邮寄文件中设置 ->from()

代码

controller function

public function store(Request $request)
    {
        $request->validate([
            'product' => 'required|string',
            'email' => 'required|email',
            'message' => 'required|string',
        ]);

        $user = $request->user(); // need to add this

        $inquery = new Inquery;
        $inquery->user_id = $user->id;
        $inquery->product = $request->input('product');
        $inquery->email = $request->input('email');
        $inquery->message = $request->input('message');
        $inquery->save();

        Mail::to('xyz@example.com')->send(new InquiryToAdmin($inquery));
}

Mailable file

class InquiryToAdmin extends Mailable
{
    use Queueable, SerializesModels;

    public $data;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($user['email'])->subject('New Inquiry was sent')->view('emails.inquiry.admin');
    }
}

NOTE:

What I need is to get ->from($user['email']) for that i need to send my $user from controller to mailable file (commented in code), the problem is I cannot set $user to be send to my mailable file.

有什么想法吗?

试试这个

Mail::to('xyz@example.com')->send(new InquiryToAdmin($inquery,$user));

class InquiryToAdmin extends Mailable
{
    use Queueable, SerializesModels;

    public $data;
    protected $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data,$user)
    {
        $this->data = $data;
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->user->email)->subject('New Inquiry was sent')->view('emails.inquiry.admin');
    }
}