1

I have a question about net/smtp

For html emails you have to set this in the header of the email content-type: text/html . However, if you want to send an attachment you have to change it to content-type: multipart/mixed. Which would make the html email...not html anymore.

So the question is.. how do I accomplish both? HTML and attachment?

Thank you

4

1 回答 1

2

每个附件都有自己的 MIME 类型

多部分电子邮件的每个部分都有自己的 MIME 类型。因此,虽然电子邮件的内容类型是“多部分/混合”,但每个附件都有自己的 MIME 类型(文本、HTML 等)。

这是Doug Steinwand的电子邮件中来自MIME 和 HTML的示例多部分电子邮件:

To: whoever@someplace.com
Subject: MIME test
Content-type: multipart/mixed; boundary="theBoundaryString"

--theBoundaryString

Plain text message goes in this part. Notice that it
has a blank line before it starts, meaning that this
part has no additional headers.

--theBoundaryString
Content-Type: text/html
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Content-Base: "http://somewebsite.com/"

<body><font size=4>This</font> is a 
<i>test</i>.
--theBoundaryString--

在这里您可以看到文本附件没有明确的内容类型。当附件没有明确的内容类型时,它是 US ASCII TEXT。HTML 附件的内容类型为“text/html”。可能还有其他附件,每个附件都有自己的 MIME 类型。

考虑使用“邮件”宝石

邮件gem使发送和解析多部分电子邮件变得非常容易。它稳定,维护良好,应用广泛。

自述文件中的示例显示了如何发送包含文本部分和 HTML 部分的多部分邮件:

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
于 2014-03-30T15:15:08.267 回答