attachments
Extract mail parts of specified mime types.
DESCRIPTION
It’s original intention is as a helper class class for filtering email attachments before forwarding. The extracted attachments are saved in the /tmp directory.
FEATURES/PROBLEMS:
Is the /tmp directory and the ‘file’ shell command on your OS? If not some porting work is needed.
It is built on top of the mail gem, which in my experience is about 10 times faster at decoding large attachments if you us ruby 1.9 instead of 1.8.
NEW STUFF:
Version 0.0.14
-
Support in-memory storage of attachments to support processing on systems without access to file system (i.e. Heroku + Cloudmailin)
-
Include set in extract.rb to avoid exception in ruby 1.9.2
Version 0.0.11
-
Convert the subject to utf-8
Version 0.0.11
-
A message object from the mail gem library may now be passed to parse
Version 0.0.10
-
Find text body even if the mail is not multipart
Version 0.0.9
-
Pass raw mail data into to Extract#parse(=> c)
-
Pass stream to Extract#parse(=> s)
Version 0.0.8
-
Extract#text_body will try to convert the text_part to utf-8, and (for ruby>=1.9) ensure utf-8 encoding on the returned string
REQUIREMENTS:
Attachments is presently using the mail and uuidtools ruby gems. It uses the ‘file’ shell command from filemagic to decide mime types when they are uncertain.
SYNOPSIS:
require 'rubygems'
require 'attachments'
# Which mime types do you want to accept?
extract = Attachments::Extract.new [ "text/plain" ]
# I make postfix save incoming mails in MailDir,
# then glob that directory
INCOMING_MAIL_DIR = "/your/MailDir"
mailfiles = Dir.glob(INCOMING_MAIL_DIR + "/*")
# Process the mails
mailfiles.each do |m|
# Open mail stored in filename
extract.parse m
# Or use hash. # extract.parse({ :filename => m }) # Parse raw data or stream instead # (forcing binary encoding/stream is a good idea) # extract.parse({ :content => c }) # extract.parse({ :stream => s }) # Some info about the mail puts extract.to puts extract.from puts extract.subject
# This will ask the mail gem for the text body, # try to convert it to utf-8 from the encoding # defined in the mail and (if ruby>=1.9) force utf-8 # encoding on the returned string. This may be useful # if you want to do markdown to html processing or # similar on the mail before forwarding it puts extract.text_body
# Traverse through the extracted attachments
extract.files.each do |f| # Do something with the attachments puts f.inspect case f
when "text/plain" then
puts f[:body] || File.read(f[:tmpfile])
end
end
# Remove tmp files and prepare for new parsing action extract.close
# Probably you want to delete or move the processed email # to an archive here # FileUtils::rm(m)
end
Note on Patches/Pull Requests
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright
Copyright © 2010 Rune Myrland. See LICENSE for details.