Saani
Saani

Reputation: 782

Laravel: Fetch record from db and send it to email template

I have successfully implemented the sending email process in Laravel 5. Now, what I have to do is to fetch some record from database and send it to email. My send email code is:

 Mail::send('email', ['name' => "EE"], function($m) {
 $m->to(Input::get('email'), '')->subject('Password Request');
 });

The email template is:

Hello,

Your password has been changed, successfully.
Your new password is:"**password from database**" 
Kindly, change your password later on.

Thanks!

So the "password from database" has to be from database. How may I do that? any help?

Upvotes: 2

Views: 4012

Answers (2)

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

$userDetail = User::find(1);
Mail::send('yourTemplate', array('userInfo'=> $userDetail), function($message) {
    $message->from('[email protected]', 'test')
        ->to('[email protected]', 'name')
        ->subject('subject');
});

Then in your template file use this variable,like

Hello,

Your password has been changed, successfully.
Your new password is: {!! $userInfo->password !!};
Kindly, change your password later on.

Thanks!

Upvotes: 2

Emeka Mbah
Emeka Mbah

Reputation: 17545

According to your question you wish to grab the password from database table, well this not possible because password are encrypted and sending encrypted version to user is useless.

What you could do is generate a new password, save new password in database under user row, email column then email the generated password to user:

You could do this:

I assume user email address is stored under email column in `user table and you have a User model:

$email = Input::get('email');
$user = User::where('email', $email)->first();

if($user){
      $new_password = str_random(8); //generates random string , eight character long

      $user->password = \Hash::make($new_password);
      $user->save();

      $data = [
                 'name'          => $user->first_name,
                 'new_password ' => $new_password 
      ];

      Mail::send('emails.password-reset', $data, function($m) use ($user){
         $m->to($user->email, '')->subject('Password Request');
      });
}


In email template: views\emails\password-reset.blade.php:

Hello {!!$name!!},

Your password has been changed, successfully.
Your new password is:"{!!$new_password!!}" 
Kindly, change your password later on.

Upvotes: 3

Related Questions