2
votes

I have a contact form with fields like name, email, phone, etc. I want to pass the fields into the email sent and the example i tried below doesnt seem to send the variables to the html email thats being sent out.

this is in the Route::post of this page:

$data = [ 'name' => $request->input('name'), 'email' => $request->input('email'), 'phone' => $request->input('phone'), 'message' => $request->input('message') ];

  Mail::to('[email protected]')->send(new ContactForm($data));

then in the contactform.php under the mail folder within the build method i have

public function build()
{
    return $this->from('[email protected]')->view('emails.app.contact-form-submission');
}
4

4 Answers

3
votes

The answers given are too complex for me. Heres a simple example using a name, email, and body field in the form. Put this in your controller method:

    Mail::send('emails.contact', request()->all(), function ($message) {
        $message->subject('Website Contact');
        $message->to(config('mail.from.address'));
        $message->replyTo(request()->input('email'));
    });

Heres the emails/contact view file:

Name: {{ $name }}
Email: {{ $email }}
Message:

{{ $body }}
2
votes

Here an example from the laravel docs

namespace App\Mail;

use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class YOURCLASSNAME extends Mailable
{
    use Queueable, SerializesModels;

    //Here you are going to define the variables 
    //which you are pass to your view
    public $name;
    public $email;
    public $phone;
    public $message;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        //Now in the construct, you receive the input from request
        $this->name = $request->input('name');
        $this->email = $request->input('email');
        $this->phone = $request->input('phone');
        $this->message = $request->input('message');
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        //And now you return the input to your view
        return $this->view('emails.app.contact-form-submission')
                ->with([
                    'name' => $this->name,
                    'email' => $this->email,
                    'phone' => $this->phone,
                    'message' => $this->message,
                ]);
     }
}

After that, you can use any variable of the with in your blade

The name: {{ $name }} and email: {{ $email }}

1
votes

Laravel 5.6 way using public properties (https://laravel.com/docs/5.6/mail#writing-mailables):

Mailable class, e.g. "CallBack.php":

namespace App\Mail;

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

class CallBack extends Mailable
{
    use Queueable, SerializesModels;

    public $name;
    public $phone;
    public $body;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.callback')->subject('mail subject');
    }
}

Controller:

    Mail::to('[email protected]')
        ->send(new CallBack($request->input('name'), $request->input('phone'), $request->input('body')));

callback.blade.php:

<h1>Laravel Email</h1>

<p>name: {{ $name }}</p>

<p>phone: {{ $phone }}</p>

<p>body: {{ $body }}</p>
0
votes

Create public properties in your ContactForm mailabel class and receive data from controller to that class using constuctor.

For example-

public $name;

 public function __construct($data)
    {
        $this->name= $data.name;
    }