如何设置发送HTML邮件与邮件宝石? [英] How to setup send HTML emails with mail gem?
问题描述
我使用Mail gem发送电子邮件。这是我的代码:
I am sending emails using the Mail gem. Here's my code:
require 'mail'
require 'net/smtp'
Mail.defaults do
delivery_method :smtp, { :address => "smtp.arrakis.es",
:port => 587,
:domain => 'webmail.arrakis.com',
:user_name => 'myname@domain.com',
:password => 'pass',
:authentication => 'plain',
:enable_starttls_auto => true }
end
Mail::ContentTypeField.new("text/html") #this doesnt work
msgstr= File.read('text2.txt')
list.each do |entity|
begin
Mail.deliver do
from 'myname@domain.com'
to "#{entity}"
subject 'a good subject'
body msgstr
end
rescue => e
end
end
end
I不知道如何设置内容类型,以便我可以将我的电子邮件格式化为html格式。虽然我实际上只是希望能够像我的电子邮件客户端那样定义粗体文本:粗体文本。有人知道我需要指定什么内容类型来实现这个目的,以及如何使用邮件实现它吗?
I don't know how to set up the content type so that i can format my email as html for example. Though i actually just wish to be able to define bold text like my email client does: bold text. Does anybody know what is the content-type that i need to specify in order to achieve this and how to implement it with mail?
只需要注意,上面的代码就可以工作罚款发送纯文本电子邮件。
Just a note, the code above works fine for sending plain text emails.
推荐答案
From the documentation
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邮件与邮件宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!