针对网页浏览器和手机,我们的Email的content_type分别为"text/html"和"text/plain"
我们可以这样做[b]ruby script\generate mailer Notifier multipart_alternative[/b]
app/models/notifier.rb:
[code]
def multipart_alternative(recipient, name, sent_at = Time.now)
subject "Something for everyone."
recipients recipient
from 'barnam@chadfowler.com'
sent_on sent_at
content_type "multipart/alternative"
part :content_type => "text/plain",
:body => render_message("multipart_alternative_plain", :name => name)
part :content_type => "text/html",
:body => render_message("multipart_alternative", :name => name)
end
[/code]
我们的两套模板:
1,app/views/notifier/multipart_alternative_plain.rhtml
[code]
Hi <%= @name %>!
This is a plain-text message. Enjoy!
[/code]
2,app/views/notifier/multipart_alternative.rhtml
[code]
<html>
<body>
<h1>Hi <%= @name %></h1>
This is a rich-text message. Enjoy!
</body>
</html>
[/code]
甚至我们可以简化它们:
app/models/notifier.rb:
[code]
def implicit_multipart(recipient, name, sent_at = Time.now)
subject "Something for everyone."
recipients recipient
from 'barnam@chadfowler.com'
sent_on sent_at
body(:name => name)
end
[/code]
然后我们通过给app/views/notifier目录下不同的模板名来做content_type区分,如:
implicit_multipart.text.plain.rhtml和implicit_multipart.text.html.rhtml
这样我们就可以隐式的发送针对不同客户端的Email了
我们可以这样做[b]ruby script\generate mailer Notifier multipart_alternative[/b]
app/models/notifier.rb:
[code]
def multipart_alternative(recipient, name, sent_at = Time.now)
subject "Something for everyone."
recipients recipient
from 'barnam@chadfowler.com'
sent_on sent_at
content_type "multipart/alternative"
part :content_type => "text/plain",
:body => render_message("multipart_alternative_plain", :name => name)
part :content_type => "text/html",
:body => render_message("multipart_alternative", :name => name)
end
[/code]
我们的两套模板:
1,app/views/notifier/multipart_alternative_plain.rhtml
[code]
Hi <%= @name %>!
This is a plain-text message. Enjoy!
[/code]
2,app/views/notifier/multipart_alternative.rhtml
[code]
<html>
<body>
<h1>Hi <%= @name %></h1>
This is a rich-text message. Enjoy!
</body>
</html>
[/code]
甚至我们可以简化它们:
app/models/notifier.rb:
[code]
def implicit_multipart(recipient, name, sent_at = Time.now)
subject "Something for everyone."
recipients recipient
from 'barnam@chadfowler.com'
sent_on sent_at
body(:name => name)
end
[/code]
然后我们通过给app/views/notifier目录下不同的模板名来做content_type区分,如:
implicit_multipart.text.plain.rhtml和implicit_multipart.text.html.rhtml
这样我们就可以隐式的发送针对不同客户端的Email了