Mego
2 months ago
1161
1
Requests

Sending data from inputs to mail

Posted 2 months ago by Mego

Hi, I am solving an issue about sending mail in Laravel (in Vanilla this is a job for a few minutes).

I have created a form BugReport, classic way - [email protected] and @report. @index will render the form, @report should process (so validate and authorize user's inputs) and send it to main application mail (so static mail address, not user's e-mail).

I have created a new Mailable by Artisan with a name "BugReport". However I really don't know how to pass data from inputs to it?

In my [email protected] I have something like this:

public function report(ReportBug $request) {
        $validatedData = $request->validated();
        if($validatedData) {
            Mail::to('[email protected]')->send(new BugReport($validatedData));
        }

and in my Mailable I have this:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class BugReport extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('[email protected]')
            ->view('emails.bugreport')
            ->with([
                'data' => $validatedData,
            ]);
    }
}

However $validatedData doesn't exists in Mailable class. I know that not, but I would like to ask how to do, desired data will be in Mailable? I have prepared them in BugReportController.

Thank you

Please sign in or create an account to participate in this conversation.