将整个电子邮件正文作为字符串传递并在 rails 中呈现电子邮件

passing the entire email body as a string and rendering email in rails

我正在做一个 rails 5.2 中的项目。我正在使用 rails 操作邮件发送电子邮件。

我必须将整个电子邮件模板作为输入发送到操作邮件程序。

到目前为止,我已经覆盖了操作邮件程序的邮件方法。

class UAMailer < ActionMailer::Base
  def mail(headers = {}, &block)
    super(headers.merge(template_path: template_path), &block)
  end
end

我是通过一种叫做 say send_email.

的方法调用它的
def send_email(user, subject)
  mail(to: user.email, subject: subject)
end

然后我有一个正常的 send_email.html.erb 一个模板,其中写入了实际的邮件正文。

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
 </head>
<body>
  <p>Dear <%= @user.username %>,</p>
  <p>Testing</p>
</body>

我想将整个正文作为输入传递给之前定义的邮件方法。我需要将整个 HTML 正文标记传递给覆盖的邮件方法。

def send_email(user, subject, body)
  mail(to: user.email, subject: subject, body: body)
end

在电子邮件操作下添加的任何实例变量都可以在其相应的电子邮件模板中直接访问。

为了。例如。

@body = body 

并且在模板中,您可以调用

<%= @body.html_safe %>

对于 HTML 安全内容 html_safe