1

I have this scenario in my app where I have to send user's password and username to their email right away after their account created by admin. Here is what I've done so far : Controller :

public function store(Request $request) {
        $data = $request->all();

        $validasi = Validator::make($data, [
                    'name' => 'required|max:255',
                    'username' => 'required|max:255|unique:users',
                    'email' => 'required|email|max:150|unique:users',
                    'password' => 'required|confirmed|min:6',
                    'level' => 'required|in:admin,author',
        ]);
        if ($validasi->fails()) {
            return redirect('user/create')->withInput()->withErrors($validasi);
        }
        $data['password'] = bcrypt($data['password']);
        $email = $data['email'];
        Mail::to($email)->send(new UserAdded());
        User::create($data);
        return redirect('user');
    }

The email will send successfully but I want to pass $data['username'] and $data['password'] to email view as well.

email view :

<div class="row">
    <div class="col-sm-12 col-xs-12">
         <h2><span>Welcome new User!</span></h2>
         <p>Your username </p>
         <p>Your password</p>
    </div>

Mailable function :

class UserAdded extends Mailable
{
    use Queueable, SerializesModels;
    public function __construct()
    {

    }
    public function build()
    {
        return $this->view('email/newuser');
    }
}

How to do it ? where will I define the $user data ? thanks for the help!

5
  • what is the code for you UserAdded() function? Commented Nov 25, 2016 at 9:03
  • I think that @manniL answer is more useful. My answer is not details. Anyway, you can reference it :) Commented Nov 25, 2016 at 9:10
  • @manniL's answer is correct Commented Nov 25, 2016 at 9:15
  • @KelvinKyaw Okay :) Commented Nov 25, 2016 at 9:18
  • @PhillisPeters yep, that's done it Commented Nov 25, 2016 at 9:18

1 Answer 1

4

First of all, you need a class that builds the view. There you can define properties which you'll pass to the class. They'll be accessable in the view then.

<?php

namespace App\Mail;

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

class UserAdded extends Mailable
{
    use Queueable, SerializesModels;

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


    public function build()
    {
        return $this->view('reference.your.view.path.here'); //CHANGE
    }
}

You can now access the variables in your view.

<div class="row">
    <div class="col-sm-12 col-xs-12">
         <h2><span>Welcome new User!</span></h2>
         <p>Your username: {{$username}} </p>
         <p>Your password: {{$password}} </p>
    </div>

Calling your view can be realized like this:

    Mail::to($email)->send(new UserAdded($data['username'],$password));
Sign up to request clarification or add additional context in comments.

1 Comment

No problem! Happy I was able to help @PrestonGarvey22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.