Laravel Mail::send how to pass data to mail View

40,018

Solution 1

Send data like this.

$data = [
           'data' => $user->pidm,
           'password' => $user->password
];

You can access it directly as $data and $password in email blade

Solution 2

$data = [
       'data' => $user->pidm,
       'password' => $user->password
];

second argument of send method passes array $data to view page

Mail::send('emails.auth.registration',["data1"=>$data] , function($message)

Now, in your view page use can use $data as

User name : {{ $data1["data"] }}
password : {{ $data1["password"] }}

Solution 3

The callback argument can be used to further configure the mail. Checkout the following example:

Mail::send('emails.dept_manager_strategic-objectives', ['email' => $email], function ($m) use ($user) {
        $m->from('[email protected]', 'BusinessPluse');
        $m->to($user, 'admin')->subject('Your Reminder!');
});

Solution 4

for those using the simpleMail this might help :

  $message = (new MailMessage)
   ->subject(Lang::getFromJson('Verify Email Address'))
   ->line(Lang::getFromJson('Please click the button below to verify your email address.'))
   ->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
   ->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
  $message->viewData['data'] = $data;
        return $message;
Share:
40,018
saimcan
Author by

saimcan

Updated on August 03, 2022

Comments

  • saimcan
    saimcan over 1 year

    How can i pass data from my Controller to my customized mail View ?

    Here's my controller's send mail method :

    $data = array($user->pidm, $user->password);
    Mail::send('emails.auth.registration', $data , function($message){
    $message->to(Input::get('Email'), 'itsFromMe')
            ->subject('thisIsMySucject');
    

    Here's my emails.auth.registration View

    <p>You can login into our system by using login code and password :</p>
    <p><b>Your Login Code :</b></p> <!-- I want to put $data value here !-->
    <p><b>Your Password :</b></p>   <!--I want to put $password value here !-->
    <p><b>Click here to login :</b>&nbsp;www.mydomain.com/login</p>
    

    Thanks in advance.

  • Dharman
    Dharman over 4 years
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.