11

I am trying to send email after user successfully register. so right now i am stuck to pass data in email template.I am sending email with Mailable . so from my Register Controller i using like that Mail::to('[email protected]','User Name')->send(new Verify_Email()) So my question is how to pass array param into new Verify_Email()Massage build class.and so then how to pass from Verify_Email to View.

RegisterController.php

public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'firstname' => 'required|max:255',
        'lastname' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    $confirmation_code = str_random(30);
    $user =  User::create([
        'firstname' => $data['firstname'],
        'lastname' => $data['lastname'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'confirmation_code' => $confirmation_code
    ]);
    $email_data = ([
       'name' => $data['firstname'].' '.$data['lastname'],
        'link' => '#'
    ]);
    Mail::to('[email protected]','User Name')->send(new Verify_Email());

    return $user;

}

Verify_Email.php

class Verify_Email extends Mailable
{
 use Queueable, SerializesModels;

/**
 * Create a new message instance.
 *
 * @return void
 */

public function __construct()
{
    //
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->from('[email protected]')
        ->view('emails.verify-user');
        //--------------------------> **Send data to view**
        //->with([            
            //'name' => $this->data->name,
            //'link' => $this->data->link
        //]); 
}

1 Answer 1

18

Please follow this approach

Pass the inputs to the Verify_Email constructor and use $this->variable to pass them onto the view.

Mail::to('[email protected]','User Name')->send(new Verify_Email($inputs))

and then this in Verify_Email

class Verify_Email extends Mailable {

  use Queueable, SerializesModels;

  protected $inputs;

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

  /**
   * Build the message.
   *
   * @return $this
   */
  public function build()
  {
    return $this->from('[email protected]')
                ->view('emails.verify-user')
                ->with([
                  'inputs' => $this->inputs,
                ]);
  }

}

Hope that answers your question :)

Sign up to request clarification or add additional context in comments.

4 Comments

@Marz What do you mean?
@Marz Not sure what you are implying with all those one line comments but I'd suggest giving out more information so I can be of help.
i know this is a 3 year post, but to clarify with the sample above, can i still use for example above the contents of that $inputs; in a sentence line, I have this line on the build where it says ->line('the reason you got declined is because of this : "data here " '), i have build a database where the reason can be inputted on the database and i can extract it and use it on this sentence, can you help me out? thanks
@Aia'sBlog I am not sure how exactly the code looks like for you. But yes, you should be able to send in any content to the view. Just make sure you use the same variable to display it inside the view file. So like if you do ->with(['line' => $this->line]), then in your view, just do {{ $line }}. Hope this helps.

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.