简体   繁体   English

Laravel:从数据库获取记录并将其发送到电子邮件模板

[英]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. 我已经在Laravel 5中成功实现了发送电子邮件的过程。现在,我要做的是从数据库中获取一些记录并将其发送到电子邮件。 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: 我假设用户电子邮件地址存储在`user表的email列下,并且您具有User模型:

$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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM