如何发送电子邮件中的HTML内容从净/红宝石smtp? [英] How to send HTML content in email from net//smtp in ruby?

查看:169
本文介绍了如何发送电子邮件中的HTML内容从净/红宝石smtp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关代码:

Pertinent code:

msg = "Subject: Reset password instructions\n\nHello " + @request_payload["email"] + "!\n\n" +
      "A new account has been created for you at <a href=\"presentation-layer.dev\">presentation-layer.dev<a>." + 
      "Please go to that page and click \"forgot password\" to set your password."
      smtp = Net::SMTP.new 'smtp.gmail.com', 587
      smtp.enable_starttls
      smtp.start('domain', "email", 'password', :login) do
        smtp.send_message(msg, 'sender', "recip")
      end

生成的电子邮件中只有原始文本。如何让服务器评估HTML标签?

The resulting email just has the raw text in it. How do I get the server to evaluate the HTML tags?

推荐答案

要做到你想做的事,你应该生成一个MIME文档。如果你真的想做得对,请创建一个多部分MIME文档,以便同时拥有TEXT和富文本部分。

To do what you want, you should generate a MIME document. If you really want to do it right, create a multipart MIME document so you have both the TEXT and rich-text parts.

您可以从Net :: SMTP ,但您必须将必要的MIME标题和部分分隔符添加到文档中。有关示例,请参阅使用Ruby - SMTP发送电子邮件

You can do it from Net::SMTP, but you have to add the necessary MIME header and part dividers to the document. See "Sending Email using Ruby - SMTP" for an example how.

使用 Mail gem更容易,它支持两者,特别是如果你包含多个部分或添加附件。从文档:

It's easier to use the Mail gem, which supports both, especially if you're including multiple parts or adding attachments. From the documentation:


您也可以创建MIME电子邮件。对于text / plain和text / html(最常见的一对)制作多部分/备用电子邮件有助手方法,您可以手动创建任何其他类型的MIME电子邮件。

You can also create MIME emails. There are helper methods for making a multipart/alternate email for text/plain and text/html (the most common pair) and you can manually create any other type of MIME email.

And farther down in the document in "Writing and sending a multipart/alternative (html and text) email":


Mail提供了一些基本的假设,并尽可能地简化了常见的事情......(从邮件库中询问很多内容)

Mail makes some basic assumptions and makes doing the common thing as simple as possible.... (asking a lot from a mail library)



mail = Mail.deliver do
  to      'nicolas@test.lindsaar.net.au'
  from    'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
  subject 'First multipart email sent with Mail'

  text_part do
      body 'This is plain text'
  end

  html_part do
      content_type 'text/html; charset=UTF-8'
      body '<h1>This is HTML</h1>'
  end
end

这篇关于如何发送电子邮件中的HTML内容从净/红宝石smtp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆