简体   繁体   中英

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('mymail@mail.com')->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 .

change ur build function in mail class

like this

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

instead of all try this in controller

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

@linktoahref

check passowrd and password there is spelling difference!

Solved

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