热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

面试准备

Ruby On Rails 面试题与答案

Question: How can you send a MULTI-PART Email?
Answer:

Nowadays most email clients support HTML email, however there are still some old Blackberry phones that prefer emails the ‘ol text way. 

Therefore it is important to send emails both as HTML and text. This technique is called multi-part emails. 

The ActionMailer class (included in Rails 3.0) does a great job of sending both text and HTML emails out to the end user at the same time. 

By default Rails sending an email with plain/text content_type, for example:

# app/models/notifier.rb
def send_email(email)
  subject       email.subject
  from          email.from
  recipients    email.recipients
  sent_on       Time.now
  body          :email => email
end

Next let’s update the view in : app/views/notifier/send_email.html.erb

Welcome to here: 

The sent email is a plain text email

Date: Thu, 5 Aug 2010 16:38:07 +0800
From: RailsBP 
To: flyerhzm@gmail.com
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Welcome: http://rails-bestpractices.com

The link url is just displayed as a plain text because of the email content_type.

TEXT/HTML

If we want the email clients to display link url as html format, we should change the content_type to text/html in the app/models/notifier.rb file

def send_email(email)
  subject          email.subject
  from             email.from
  recipients       email.recipients
  sent_on          Time.now
  content_type     "text/html"
  body             :email => email
end

Now the sent email is a html formatted email

Date: Thu, 5 Aug 2010 17:32:27 +0800
From: RailsBP 
To: flyerhzm@gmail.com
Mime-Version: 1.0
Content-Type: text/html; charset=utf-8

Welcome: http://rails-bestpractices.com

Now the email client can display the link url correctly with html format.

The email header looks somewhat like this:

Content-Type: multipart/alternative;
boundary="----=_NextPart_000_002C_01BFABBF.4A7D6BA0"
Content-Type: multipart/alternative tells the e-mail program to expect different parts to follow, separated by a boundary which specified in quotation marks. Actually the boundary could be anything, though hyphens, equal signs, and underscores insure that the e-mail program won't try to display this boundary to the recipient.
------=_NextPart_000_002C_01BFABBF.4A7D6BA0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

保存以便复习

收藏此条目、标记为困难题,或将其加入复习集合。

打开我的学习资料库
这有帮助吗?

用户评价最有帮助的内容:

版权所有 © 2026,WithoutBook。