如何传递动态内容(来自数据库的内容)以在laravel 5.4中发送邮件

时间:2018-08-23 11:31:17

标签: php

我是laravel的新手。我有表email_template,并希望在用户忘记密码时向用户发送邮件。我从数据库中动态获取内容,但我不知道如何将其传递给laravel中的邮件功能。

Mail :: send($ posts ['email_template'],['USER'=> $ post ['user]],函数($ message)         {

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="item" data-id=1>Banana B</div>
  <div class="item" data-id=2>Carrot F</div>
  <div class="item" data-id=3>Orange B</div>
  <div class="item" data-id=4>Apple</div>
  <div class="item" data-id=5>Carrot</div>
</div>

其中$ posts ['email_template']是我要发送的内容,而user是我要在内容中替换的变量

2 个答案:

答案 0 :(得分:1)

Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
    $message->from('test@gmail.com', 'Your Application');
    $message->to('test8@gmail.com', $user->name)->subject('Welcome to laravel');
});

emails.template是您的视图-template.blade.php文件-/resources/views/emails/template.blade.php 现在,在您看来,即emails.template,您可以执行以下操作:

{{ $user->name }}, {{ $data->address }}

答案 1 :(得分:0)

您可以定义 config文件夹中常量文件中的ADMIN_EMAIL和CC_EMAIL

            $emailData = array(
                'name'=>'toName',
                'toEmail'=>$request->email
            );
            $this->sendEmail($emailData);

电子邮件功能

function sendEmail($emailData){
    $this->adminEmail = config('constant.ADMIN_EMAIL');
    $this->ccEmail = config('constant.CC_EMAIL');
    $this->toEmail = $emailData['toEmail'];
    $this->emailTemplate = $emailData['emailTemplate'];
    $data['emailInfo'] = array(
        'name'=>$emailData['name']
    );
    Mail::send('emails.yourTemplate', $data, function ($message) {
        //$message->attach($pathToFile);
        $message->from($this->adminEmail, 'Laravel Email Test');
        $message->to($this->toEmail)->cc($this->ccEmail);
    });

}