【问题标题】:Email Builder Laravel电子邮件生成器 Laravel
【发布时间】:2023-03-29 04:10:01
【问题描述】:

我正在使用 Laravel 5.3 构建一个应用程序,该应用程序允许管理员构建模板电子邮件以在系统内用于电子邮件地址验证、忘记密码等。模板电子邮件是使用在数据库。创建模板后,可以使用以下命令在应用程序中使用它们

Mail::to($user)->queue(new MailEmailTemplate($emailtemplate, $user));

其中$emailtemplate 是具有以下可填充字段的模型(模型中没有其他值得披露的内容) $fillable = ['from_name', 'from_email','subject','body','email_type','status'];

为了允许将电子邮件个性化给要发送给的用户(即电子邮件正文顶部的“亲爱的约翰”),我需要允许管理员在表单中输入变量。

系统管理员应如何填写表单正文的示例: Dear {{$user->first_name}}

Image of the form that admins will use to create the email template

我遇到的问题是在发送电子邮件时,变量没有注入到发送给客户端的 HTML 中。即在上面的示例中,收件人收到未插入变量的电子邮件 Dear {{$user->first_name}} 而不是 Dear John。

我在我的 app/routes/web.php 文件中添加了以下路由

Route::get('/email_templates/{emailtemplate}/send/{user}', 'emailTemplatesController@send')->name('email_templates.send');

我的 App/Http/Controllers/emailTemplatesController.php

...
use App\Mail\MailEmailTemplate;

class EmailTemplatesController extends Controller
{
    ...
    public function send(EmailTemplate $emailtemplate, User $user)
    {
        Mail::to($user)->queue(new MailEmailTemplate($emailtemplate, $user));
        return back();
    }
}

我的 app/Mail/MailEmailTemplate.php 可邮寄

use App\Models\User;
use App\Models\EmailTemplate;
use Session;

class MailEmailTemplate extends Mailable
    {
        use Queueable, SerializesModels;
        public $user;
        public $emailtemplate;

        public function __construct(EmailTemplate $emailtemplate, User $user)
        {
            $this->emailtemplate = $emailtemplate;
            $this->user = $user;
        }


        public function build()
        {
            $data['user']           = $this->user;
            $data['email_template'] = $this->emailtemplate;

            return $this->subject('testing')
                        ->view('emails.index')
                        ->with($data);
        }
    }

最后是 resources/views/emails/index.blade.php 文件(用于向收件人发送电子邮件)

{!! $email_template->body !!}

我在这里使用了未转义的输出,因为存储在正文字段中的文本是 HTML(管理员将在文本编辑器 (phpStorm) 中将电子邮件模板开发为 laravel 的“视图”,然后从视图中复制代码到正文的表单字段中以将其存储在应用程序中)。

如何修改它以允许插入来自 $user 对象的变量来代替 $emailtemplate->body 中指定的变量?

如果我将 resources/views/emails/index.blade.php 修改为以下可以实现正确的输出

{!! str_replace('<<$user->first_name>>',$user->first_name, $email_template->body)!!}

但是这种方法效率低下,因为它需要将 str_replace 用于电子邮件中要使用的任何变量。注意我必须在 $email_template->body 中使用 > 标签来防止解析错误。

更新: 变量变量是否可以与正则表达式一起使用来查找 $email_template->body 内的每个 {{ }} 标签实例并将其中包含的文本视为变量变量?

对于这个冗长的问题,我深表歉意 - 我对 Laravel 比较陌生,希望确保你们能够仔细检查我的代码结构。

感谢您的帮助。 约旦

【问题讨论】:

  • 问题是你想将变量传递给保存在数据库中的模板吗?
  • 电子邮件模板正文的数据库列应包含:

    尊敬的 {{$user->first_name}}

    。渲染视图时,字符串的 {{$user->first_name}} 部分应替换为 $user 对象的 first_name 字段(我将其传递到视图中)

标签: php laravel laravel-5.3


【解决方案1】:

尝试使用

{{$email_template->body}}

【讨论】:

  • 不,这不起作用,因为 body 属性存储了一个字符串,它是 html 代码。即 {{$email_template->body}} 的输出是:

    Dear {{$user->first_name}}

    而 {!!$email_template->body!!} 的输出是:Dear {{$user->first_name}} 所以 HTML 代码(段落标签)已经被正确解释,但是 $user 对象的名字没有被插入
  • 你能告诉我“$email_template->body”的内容是什么
  • $email_template->body = "亲爱的 {{$user->first_name}}"
  • 因此,我希望 $email_template->body 的 {{ }} 标记中包含的任何内容都被视为变量,而不是简单地将 $email_template 正文的内容呈现为字符串
  • 你可以试试这个 $email_template->body = "Dear" 。 {{$user->first_name}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 2021-06-30
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多