Sample code for displaying a MIME email message on a web page?

Can anyone point me to a good source of sample code for taking a raw
email message (multipart, with attachments) and displaying it on a web
page? Also same thing for composing a new message with attachments?


Josh, I found the following very helpful:

Cheers, --Kip

Thanks for the pointer! That looks useful, but doesn't address what
I'm looking for. I want to be able to read in an mbox file, parse off
a message, and display it on a web page similar to how you would view
it in Hotmail or Gmail. That means decoding the MIME but also
"sanitizing" the HTML.


Josh, I have done a *very* simple wrapper around this message parsing
bit. This will let you get at the parts of a mime multipart message.
I guess you could use sanitize() strip_tags() and strip_links() to
help make the html more displayable. Note I just put this together
for a proof-of-concept for emailing articles and images to a content
system I'm writing so i sure wouldn't cut and paste!

  require 'hermes_messages.rb'
  my_messages.get_messages(pop server parameters)
  my_messages.message.each do |m|
    # Each message has many parts, but we simplifiy with some helper
    puts m.subject
    puts m.from
    puts m.body # Note the 'body' method retrieves the text/
plain part for my needs - modify as required to get the html part

Hope this helps a little more,


class HermesMessages
  require 'net/pop'
  require 'action_mailer'

  EMPTY_PART = " \r\n\r\n"
  TEXT_PLAIN = 'text/plain'
  MULTIPART_ALTERNATIVE = 'multipart/alternative'

  attr_reader :messages

  def get_messages(host, user, password, port, *args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    @messages = []
      Net::POP3.start(host, port, user, password) do |pop|
        if pop.mails.empty?
          puts 'No mail.'
          puts "#{pop.mails.size} messages will be processed."
          pop.each_mail do |m|
           @messages << IncomingImageHandler.receive(m.pop)
           m.delete if options[:delete] == true
        puts "#{pop.mails.size} mails popped."
        return pop.mails.size
      puts "Exception detected - could not retrieve email"
      return -1

  class Message
    attr_accessor :subject, :from
    attr_reader :parts

    def initialize
      @parts = []

    def add_part(p)
      @parts << p

    def has_jpeg?
      parts.each {|p| return true if p.sub_type == "jpeg"}
      return false

    def has_body?
      parts.each {|p| return true if p.main_type == "text" &&
p.sub_type == "plain"}
      return false

    def body
      parts.each {|p| return p.body if p.main_type == "text" &&
p.sub_type == "plain"}
      return ""

    def jpeg
      parts.each {|p| return p.body if p.main_type == "image" &&
p.sub_type == "jpeg"}
      return ""

    def image_filename
      parts.each {|p| return p.filename if p.main_type == "image" &&
p.sub_type == "jpeg"}
      return ""


  class Content

attr_accessor :main_type, :sub_type, :body, :filename, :file_extension, :index

  class IncomingImageHandler < ActionMailer::Base
    # email is a TMail::Mail
    def receive(email)
      @message =
      @message.from = email.from[0]
      @message.subject = email.subject
      puts "Processing message: #{@message.subject}"


    def process(part)
      if part.multipart? then {|p| process(p) } unless
          part.content_type == MULTIPART_ALTERNATIVE &&
      return @message

    def process_part(part)
      puts " part: #{part.content_type} with size
      pp " content is: '#{part.body}'" if part.body.length <= 10
      content =
      content.main_type = part.main_type
      content.sub_type = part.sub_type
      content.body = part.body
      content.filename = part_filename(part)
      content.file_extension = ext(part)


    def plain_part_is_empty(part) do |p|
        return true if p.content_type == TEXT_PLAIN && p.body ==
      return false

    def part_filename(part)
      # get filename
      if part['content-location'] != nil && part['content-
location'].body.length != 0
        filename = part['content-location'].body
      elsif part.type_param('name') != nil &&
part.type_param('name').length != 0
        filename = part.type_param('name')
      elsif part.disposition_param('filename') != nil &&
part.disposition_param('filename').length != 0
        filename = part.disposition_param('filename')
        filename = nil

    CTYPE_TO_EXT = {
      'image/jpeg' => 'jpg',
      'image/gif' => 'gif',
      'image/png' => 'png',
      'image/tiff' => 'tif'

    def ext( mail )
      CTYPE_TO_EXT[mail.content_type] || 'txt'
  end #class

Thanks Kip!

I just found sanitize() and that seems to be a big part of what I'm
looking for. I'll check out strip_links and strip_tags also!


What's the best place to find documentation on sanitize, strip_links,
etc. and find other functions like this? I'm looking through Ruby Core
and Ruby Standard Libs and don't see it.


Josh, here:

Cheers, --Kip

That was exactly what I was looking for. Thanks!


Sorry one last question on TextHelper... I can call sanitize from
within a view (.rhtml file) no problem, but when I try to use it
within a controller or model it complains that the method is unknown.
What do I need to require/include to reference sanitize from within a
controller or model?