如何用ruby从net/ smtp发送电子邮件中的HTML内容?

时间:2021-12-01 18:14:07

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

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

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

2 个解决方案

#1


5  

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.

要做您想做的事情,您应该生成一个MIME文档。如果您真的想要正确操作,那么创建一个多部分MIME文档,这样您就有了文本部分和富文本部分。

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.

您可以从Net::SMTP实现它,但是您必须添加必要的MIME报头和部分分隔符到文档中。有关如何使用Ruby - SMTP发送电子邮件的示例,请参见“如何使用Ruby - SMTP发送电子邮件”。

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

它更容易使用邮件gem,它支持这两种方法,特别是当您包含多个部分或添加附件时。从文档:

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.

您还可以创建MIME电子邮件。有一些帮助方法用于为文本/纯文本和文本/html(最常见的一对)创建多部分/备用电子邮件,您可以手工创建任何其他类型的MIME电子邮件。

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

在“编写和发送多部分/可选(html和文本)电子邮件”的文档中更进一步:

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

#2


0  

Sounds like you need to set the 'Content-Type' header:

听起来你需要设置“内容类型”的标题:

Content-Type: text/html;

#1


5  

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.

要做您想做的事情,您应该生成一个MIME文档。如果您真的想要正确操作,那么创建一个多部分MIME文档,这样您就有了文本部分和富文本部分。

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.

您可以从Net::SMTP实现它,但是您必须添加必要的MIME报头和部分分隔符到文档中。有关如何使用Ruby - SMTP发送电子邮件的示例,请参见“如何使用Ruby - SMTP发送电子邮件”。

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

它更容易使用邮件gem,它支持这两种方法,特别是当您包含多个部分或添加附件时。从文档:

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.

您还可以创建MIME电子邮件。有一些帮助方法用于为文本/纯文本和文本/html(最常见的一对)创建多部分/备用电子邮件,您可以手工创建任何其他类型的MIME电子邮件。

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

在“编写和发送多部分/可选(html和文本)电子邮件”的文档中更进一步:

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

#2


0  

Sounds like you need to set the 'Content-Type' header:

听起来你需要设置“内容类型”的标题:

Content-Type: text/html;