Laravel:在通知中添加收件人名称

时间:2018-06-08 15:14:33

标签: php laravel-5 notifications

我注意到Laravel重置密码通知(通过php artisan make:auth创建)不包含电子邮件to字段中的名称:

因此<test@test.com>代替Max Mustermann <test@test.com>

如何修复通知还包括to字段中用户的名称?

我发现可以按照https://stackoverflow.com/a/41064280/2311074中的说明自定义重置通知,但我不知道在哪里可以修改to字段。

1 个答案:

答案 0 :(得分:2)

php artisan make:mail SendMail
  

它会在 app / Mail / SendMail.php

中生成文件
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;


class SendMail extends Mailable
{
    use Queueable, SerializesModels;
    public $type;
    public $user;

    public function __construct($a,$b)
    {
        $this->type=$a;
        $this->user=$b;
    }

    public function build()
    {

            return $this->to($this->user['email'])
                        ->subject('Reset Password')
                        ->from('from@gmail.com','From User Name')
                        ->view('email.reset')
                        ->with(['name' =>$this->user['name'],'reset_url'=>$this->user['reset_url']]);

    }

}

在UserModel中:

<?php

namespace App\Model;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\Auth;
    use Mail;
    use App\Mail\SendMail;
    use DB;

    class User extends Authenticatable
    { 
      use Notifiable;
      protected $table='users';
      public $primaryKey='id';

        public function sendPasswordResetNotification($token)
        {
           $user=User::where('email',request()->email)->first();
           $user->reset_url="http://www.yourdomain.com/password/reset/".$token;
             try{
            Mail::send(new SendMail('reset_password',$user));
             }catch(\Exception $e){
               // Get error here
            }


        }
    }
  

resourses / views / email / reset.blade.php 内,自定义视图

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<style type="text/css">
    /* FONTS */
    @media screen {
        @font-face {
          font-family: "Lato";
          font-style: normal;
          font-weight: 400;
          src: local("Lato Regular"), local("Lato-Regular"), url(https://fonts.gstatic.com/s/lato/v11/qIIYRU-oROkIk8vfvxw6QvesZW2xOQ-xsNqO47m55DA.woff) format("woff");
        }

        @font-face {
          font-family: "Lato";
          font-style: normal;
          font-weight: 700;
          src: local("Lato Bold"), local("Lato-Bold"), url(https://fonts.gstatic.com/s/lato/v11/qdgUG4U09HnJwhYI-uK18wLUuEpTyoUstqEm5AMlJo4.woff) format("woff");
        }

    @font-face {
      font-family: "Lato";
      font-style: italic;
      font-weight: 400;
      src: local("Lato Italic"), local("Lato-Italic"), url(https://fonts.gstatic.com/s/lato/v11/RYyZNoeFgb0l7W3Vu1aSWOvvDin1pK8aKteLpeZ5c0A.woff) format("woff");
    }

    @font-face {
      font-family: "Lato";
      font-style: italic;
      font-weight: 700;
      src: local("Lato Bold Italic"), local("Lato-BoldItalic"), url(https://fonts.gstatic.com/s/lato/v11/HkF_qI1x_noxlxhrhMQYELO3LdcAZYWl9Si6vvxL-qU.woff) format("woff");
    }
}



/* CLIENT-SPECIFIC STYLES */
body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
img { -ms-interpolation-mode: bicubic; }

/* RESET STYLES */
img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; }
table { border-collapse: collapse !important; }
body { height: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important; }

/* iOS BLUE LINKS */
a[x-apple-data-detectors] {
    color: inherit !important;
    text-decoration: none !important;
    font-size: inherit !important;
    font-family: inherit !important;
    font-weight: inherit !important;
    line-height: inherit !important;
}

/* MOBILE STYLES */
@media screen and (max-width:600px){
    h1 {
        font-size: 32px !important;
        line-height: 32px !important;
    }
}



 /* ANDROID CENTER FIX */
    div[style*="margin: 16px 0;"] { margin: 0 !important; }
</style>
</head>
<body style="background-color: #f4f4f4; margin: 0 !important; padding: 0 !important;">

<!-- HIDDEN PREHEADER TEXT -->
<div style="display: none; font-size: 1px; color: #fefefe; line-height: 1px;max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden;">
    Looks like you tried signing in a few too many times. Let's see if we can get you back into your account.
</div>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <!-- LOGO -->
    <tr>
        <td bgcolor="#ffe0b2" align="center">
            <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" >
                <tr>
                    <td align="center" valign="top" style="padding: 40px 10px 40px 10px;">
                         <a href="http://stackoverflow.com" target="_blank">
                            <img src=https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded" height="70" width="210" style="display: block; width:210px;max-width: 210px;min-width: 210px;height:70px;max-height:70px;min-height:70px;color: #ffffff; font-size: 18px;" border="0">
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <!-- HERO -->


  <tr>
        <td bgcolor="#ffe0b2" align="center" style="padding: 0px 10px 0px 10px;">
            <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" >
                <tr>
                    <td bgcolor="#ffffff" align="center" valign="top" style="padding: 40px 20px 20px 20px; border-radius: 4px 4px 0px 0px; color: #111111;font-size: 48px; font-weight: 400; letter-spacing: 4px; line-height: 48px;">
                      <h1 style="font-size: 48px; font-weight: 400; margin: 0;">{{$name}} , Trouble signing in?</h1>
                    </td>
                </tr>
            </table>

        </td>
    </tr>
    <!-- COPY BLOCK -->
    <tr>
        <td bgcolor="#f4f4f4" align="center" style="padding: 0px 10px 0px 10px;">

            <table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" >
              <!-- COPY -->
              <tr>
                <td bgcolor="#ffffff" align="left" style="padding: 20px 30px 40px 30px; color: #666666;font-size: 18px; font-weight: 400; line-height: 25px;" >
                  <p style="margin: 0;">Resetting your password is easy. Just press the button below and follow the instructions. We'll have you up and running in no time. </p>
                </td>
              </tr>
            <!-- BULLETPROOF BUTTON -->
                <tr>
                    <td bgcolor="#ffffff" align="left">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td bgcolor="#ffffff" align="center" style="padding: 20px 30px 60px 30px;">
                            <table border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td align="center" style="border-radius: 3px;" bgcolor="#00bcd4"><a href="{{$reset_url}}" target="_blank" style="font-size: 20px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; color: #ffffff; text-decoration: none; padding: 15px 25px; border-radius: 2px; border: 1px solid #008ba3; display: inline-block;">Reset Password</a></td>
                                </tr>
                            </table>
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>


    <!-- FOOTER -->


</table>

</body>
</html>