简体   繁体   中英

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?

$userInfo = User::find(1);
        Mail::send('yourTemplate', array('userInfo'=> $eventInfo), function($message)  {
        $message
            ->from('from@mail.com', 'test')
            ->to('to@mail.com', '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!

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.

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