Alex
Alex

Reputation: 6475

ruby/rails receiving plain text email with pop

I have used the pop mailer for ruby (net/pop)

The problem I am having is that some of the emails are in HTML format is there a way to specify that I want plain text ?

Thanks, Alex

Upvotes: 0

Views: 992

Answers (1)

Holger Just
Holger Just

Reputation: 55768

Emails can come in different formats. The most common is MIME which allows an email to contain multiple "parts". Commonly an HTML and a plain-text part. However, you can not control which parts the email actually contains. This can only the sender for obvious reasons.

You can however use ruby to get the plain text part if one is present or try to generate some representation of that from the HTML part.

The following condensed example to get the plain text part of an email is from the MailHandler model and the POP3 module of Redmine (licensed under GPLv2).

def plain_text_body(email)
  parts = email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
  if parts.empty?
    parts << email
  end
  plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
  if plain_text_part.nil?
    # no text/plain part found, assuming html-only email
    # strip html tags and remove doctype directive
    plain_text_body = strip_tags(email.body.to_s)
    plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
  else
    plain_text_body = plain_text_part.body.to_s
  end
  plain_text_body.strip
end

pop = Net::POP3.APOP(true).new(host,port)
pop.start(username, password) do |pop_session|
  if pop_session.mails.empty?
    puts "No email to process"
  else
    puts "#{pop_session.mails.size} email(s) to process..."
    pop_session.each_mail do |msg|
      message = msg.pop
      plain_text = plain_text_body(message)
      #
      # Now do something with the plain text body
      #
    end
  end
end

Upvotes: 2

Related Questions