Ruby On Rails Interview Questions and Answers
Ques. How can you send a MULTI-PART Email?Ans.
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: email@example.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.
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: firstname.lastname@example.org 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
Most helpful rated by users: