How to send HTML content in email from net//smtp in ruby?

删除回忆录丶 提交于 2019-12-03 20:49:06

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.

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.

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

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 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

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

Content-Type: text/html;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!