简体   繁体   中英

Laravel mail provide sender detail

I need to get my user email address along with form data in order to set ->from() in my mailable file

code

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.

Any idea?

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM