Monday, April 20, 2009

Receive Mail with attachment using TMail - Ruby on Rails

Receive Mails using TMail. I like to use TMail for receive mail because TMail is best to handle the header of the email object. There are only a few methods that deal directly with the body of the email. So i just thought to share it. I have used pop3 for receive mail and use TMail for parse it.
For use TMail you need to do install gem.

gem install TMail

def popmail
     require 'net/pop'
     require 'tmail_mail_extension.rb'

     pop = Net::POP3.new 'mail.example.com'
     pop.start 'test-receive@example.com', 'password'

     if pop.mails.empty?
       puts 'No mail.'
     else
       pop.each_mail do |mail|
        email = TMail::Mail.parse(mail.pop)
        subject = email.subject
        from_email = email.from
        body = email.body_html
        if email.has_attachments?
        email.parts.each_with_index do |part, index|
          filename = part_filename(part)
          content_type = part.content_type
          filename ||= "#{index}.#{ext(part)}"
          file = filename
          fname = file.split(".")
          newfilename = fname[0]+'_'+(Time.now.to_i).to_s+'.'+fname[1]
          filepath = "#{RAILS_ROOT}/public/file_attachement/#{newfilename}"
          File.open(filepath,'wb'){|f| f.write(part.body)}
          filesize= File.size(filepath)
        end
      end
       # Do your Logic after getting details
     mail.delete
     end
   pop.finish
end

def part_filename(part)
     file_name = (part['content-location'] &&
     part['content-location'].body) ||
     part.sub_header("content-type", "name") ||
     part.sub_header("content-disposition", "filename")
end

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

Note: You can get body part of email by email.body and no need to use of tmail_mail_extension.rb

But it gives your body part two times. one in normal text of body and second is html of the same body. If you want only html of the body then you need tmail_mail_extension.rb. I found that logic of tmail_mail_extension.rb file from somewhere, i forgot the link for that.
But you can write below which i found from somewhere. Write below code in tmail_mail_extention.rb


tmail_mail_extention.rb
module TMail
   class Mail

   # returs an String with just the html part of the body
   # or nil if there is not any html part

   def body_html
     result = nil
     if multipart?
       parts.each do |part|
         if part.multipart?
          part.parts.each do |part2|
          result = part2.unquoted_body if part2.content_type =~ /html/i
          end
         elsif !attachment?(part)
          result = part.unquoted_body if part.content_type =~ /html/i
         end
       end
     else
       result = unquoted_body if content_type =~ /html/i
     end
     result
   end

   def parts_observer
     puts "INI"
     puts "content_type: #{content_type}"
     puts "body: #{body}"
     puts "parts.size: #{parts.size}"

     if multipart?
       parts.each_with_index do |part, index|
       puts ""
       puts " parts[#{index}]"
       puts " content_type: #{part.content_type}"
       puts " multipart? #{part.multipart?}"

       header = part["content-type"]

       if part.multipart?
         puts " --multipartt--"
         part.parts.each_with_index do |part2, index2|
          puts " part[#{index}][#{index2}]"
          puts " content_type: #{part2.content_type}"
          puts " body: #{part2.unquoted_body}"
         end
       elsif header.nil?
         puts " --header nil--"
     elsif !attachment?(part)
       puts " --no multipart, no header nil, no attachment--"
       puts " content_type: #{part.content_type}"
       puts " body: #{part.unquoted_body}"
     else
       puts " --no multipart, no header nil, attachment--"
       puts " content_type: #{part.content_type}"
     end

     end
     else
       puts " --no multipart--"
       puts " content_type: #{content_type}"
       puts " body: #{unquoted_body}"
     end

     puts "END"
   end

   end
end

1 comment:

  1. hi, i am rails beginner. i dont know where to place these codes in my application. will u plz give me some other reference links or example application for my references.

    ReplyDelete

Contact Me for any help regarding rails/nodejs/php/mysql

Name

Email *

Message *