4

In my project after someone fill a pricequote I want to send an email for him/her. In the controller I made an array what I want to pass to the mailable, to show on the email.

Where I'm now:

$params = array(
        'name' => $name, //user's data
        'email' => $email,
        'phone' => $phone,
        'data' => $data, //other stuffs in the form 
);

Mail::to($email)
    ->send(new pricequote($params));

And after that how to pass from the mailable class to the template?

2
  • Sorry, I forgot to add: 5.4 Commented Aug 14, 2017 at 13:20
  • Updated my answer for L5.4. Commented Aug 14, 2017 at 14:17

2 Answers 2

2

You can use;

namespace App\Mail;

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

public function build()
{
    return $this->view('emails.contacts')
        ->with(['data' => $this->data]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I suppose the pricequote is the mailable class, then your pricequote mailable class should look like:

<?php

namespace App\Mail;

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

class pricequote extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The data for the email instance.
     *
     * @var data
     */
    protected $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->view('yourview')
                    ->with(['data' => $this->data]);
    }
}

Then in your controllers you can send the email like this:

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

Laravel 5.1:

You can choose the view you want to send as email like this:

$params = array(
    'name' => $name, //user's data
    'email' => $email,
    'phone' => $phone,
    'data' => $data, //other stuffs in the form 
); 

Mail::send('view', $params, function($m){

    $m->to($this->argument('to_email'));

});

The first argument is the view and second the data you want to have access on the view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.