Q8root
Q8root

Reputation: 1355

Laravel passing data from controller to mail viewer

I am trying to pass a variable from controller to mail send class, so when user created a new account , his password will be sent to his email.

This is my Controller class /app/Http/Controllers

 public function store(Request $request)
{

    $data = uniqid();

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

    //return back();
}

This is Mail class App\Mail :

    <?php

namespace App\Mail;

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

class UserAccount extends Mailable
{
    use Queueable, SerializesModels;

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

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

mail.UserAccount view file :-

 <table>
    <tr>
        <td>Email Address:</td>
        <td></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td>{{$password}}</td>
    </tr>


</table>

When i got an email with blank password .

Upvotes: 0

Views: 643

Answers (3)

Koluchaw
Koluchaw

Reputation: 21

change ur build function in mail class

like this

 public function build()
    {
        return $this->view('mail.UserAccount')->with([
                        'password' => $this->password,
                    ]);;
    }

Upvotes: 2

Maaz
Maaz

Reputation: 67

instead of all try this in controller

Mail::send('mail.UserAccount', ['password'=>$password], function ($m) use ($request_email) {
                    $m->from('[email protected]', 'XYZ');
                    $m->to($request_email)->subject('YOUR SUBJECT');

Upvotes: 0

Q8root
Q8root

Reputation: 1355

@linktoahref

check passowrd and password there is spelling difference!

Solved

Upvotes: -1

Related Questions