【问题标题】:how to pass dynamic content(content which is coming from database) to send mail in laravel 5.4如何通过动态内容(来自数据库的内容)在 laravel 5.4 中发送邮件
【发布时间】:2019-01-29 18:36:55
【问题描述】:

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

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

        $message->from('test@gmail.com')->subject('Welcome to laravel');

        $message->to('test8@gmail.com');

    });

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

【问题讨论】:

标签: php


【解决方案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 }}

【讨论】:

  • 嘿,谢谢你这么多,我得到了解决方案 Mail::send([], ['USER' =>'savi' ], function ($message) use ($template) { $ message->from('savita.w3i@gmail.com')->subject('Welcome to ebiz'); $message->to('savitadhadwad08@gmail.com'); $message->setBody(htmlentities($模板),'text/html'); });其中 htmlentities($template) 是来自数据库的数据。现在问题是数据包含的 html 没有正确呈现。它在邮件中显示
  • $template 包含html?
  • $template=
    你好{{!! USER !!}} 很高兴认识你
  • ...您在电子邮件中收到了什么?您可以删除一个链接。
【解决方案2】:

您可以定义 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);
    });

}

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2019-01-28
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    相关资源
    最近更新 更多