【问题标题】:Rendering HTML in jade email templates with readFileSync encoded as utf8使用编码为 utf8 的 readFileSync 在 Jade 电子邮件模板中呈现 HTML
【发布时间】:2013-02-10 04:14:13
【问题描述】:

我正在尝试发送一个 Jade 电子邮件模板,但它目前在实际电子邮件中将所有内容呈现为字符串而不是 HTML。我尝试为 fs.readFileSync 寻找其他编码类型,但不确定是否应该更改。

在这里调用模板:

var emailTemplate = jade.compile(fs.readFileSync('./views/email/new_user.jade', 'utf8'));
    var html = emailTemplate({
      confirmCode: 233,
      name: params.name,
      siteName: config.siteName
      siteLogo: config.siteLogo
    });

模板:

div(style='width: 300px; margin: 0 auto')
  div(style='text-align: center')
    img(src='#{siteLogo}')
  | Hi #{name},
  p
    | Welcome to #{siteName}! You can now vote on submissions and leave comments.
    | In order for your submissions to be public, please confirm your account by
    | clicking the confirmation link below
  div(style='background-color: #fafafa; border: 1px solid #ddd; border-right: none; border-left: none; display: block; font-weight: bold; line-height: 35px; height: 35px; text-align: center; width:    100%;')
    a(href='http://localhost/users/confirm/') Confirm your account

输出(在我的电子邮件中):

<div style="width: 300px; margin: 0 auto"><div style="text-align: center"><img src="http://localhost/site_logo.png"/></div>Hi USER,<p>Welcome to SITENAME! You can now vote on submissions and leave comments. In order for your submissions to be public, please confirm your account by clicking the confirmation link below</p><div style="background-color: #fafafa; border: 1px solid #ddd; border-right: none; border-left: none; display: block; font-weight: bold; line-height: 35px; height: 35px; text-align: center; width: 100%;"><a href="http://localhost/users/confirm/">Confirm your account</a></div></div>

【问题讨论】:

  • 您能否再解释一下这个问题,因为看起来一切正常?从外观上看,该模板运行良好。
  • @AndyD 它只是将字符串吐出到电子邮件正文中,并没有真正呈现 HTML,所以我看到了所有 &lt;div style="width.. 标记。
  • 我的猜测是问题在于您如何发送电子邮件而不是模板代码。

标签: javascript node.js express pug


【解决方案1】:

我认为您遇到此问题是因为您的电子邮件的Content-Type 不是text/html。尝试这样做。

【讨论】:

  • 就是这样。最终切换到使用 sendgrid npm 模块而不是 Mailer 模块,并将 html 指定为可选参数,现在它可以工作了。
猜你喜欢
  • 2020-01-14
  • 2022-01-24
  • 1970-01-01
  • 2020-09-18
  • 2012-11-28
  • 1970-01-01
  • 2019-11-30
  • 2021-06-03
  • 2015-03-30
相关资源
最近更新 更多