What I want to do:
I'm setting up a photo blog (probably using Hobix, but that's not
final yet). I want to be able to send an email to a certain email
address and have that email posted to the blog if it meets certain
criteria. I also want to be able to attach photos to the email
message. When the script finds that there are attached photos it
should read those attachements, de-encode them and copy them to a
specified photo directory.
It would be great if someone already has a script for doing this
sort of thing that they would be willing to share. If not, perhaps
someone could point me in the right direction - which would be
better for this: rubymail or TMail?
Hi Phil,
Disclaimer: I wrote and maintain RubyMail.
RubyMail and its companion package RubyFilter are geared toward this
kind of problem -- you really have to consider the features they
provide as a unit. I wrote them to replace my use of "procmail" and
they have been handling all of my incoming email for years.
TMail has some definite advantages over RubyMail: support for Japanse
encodings in email addresses, subject lines, etc.; better support for
reading/writing mail from mailboxes; supports more of the MIME
standards, etc. Despite RubyMail being 3+ years old, TMail is even
more mature. As a downside, I don't think it is as well documented
(at least in English) and I find it harder to figure out how to use it
(but clearly *my* opinion on this means nothing). I also wrote
RubyMail with efficiency in mind. It has been a while since I ran any
benchmarks, but it was faster than TMail at parsing messages at the
time.
Last week I made some noise about soon introducing lots of
incompatible changes to RubyMail, but I am going to back away from
that and make the changes much more incremental and more compatible.
Though, I do plan to fold RubyFilter into RubyMail itself.
RubyMail+RubyFilter have been used successfully in a few real world
applications (that I know of):
- One of the more minor top level country code domain registrars uses
it to process their abuse@ mailbox.
- There is
http://www.merlin.org/ruby/ rbmhshow -- a MIME aware
message viewer for the ancient MH mail system. I think this is
really cool! I'm actually working on folding in some of the
features he had to hack into RubyMail back into the main package.
- A few folks have mailed me about RubyFilter setup issues, etc.
What the package lacks are recipes for things like you describe.
Kevin's skeleton code below is pretty good:
msg = RMail:
arser.read( file_handle )
if msg.multipart?
msg.each_part do | part |
if part.header.media_type == 'image'
# decode image and write to directory
elsif part.header.media_type == 'text'
# add part.body to the text of your posting,
however you're storing it
end
end
end
Once you have a part that stores the .jpg or whatever, you can call
part.decode
and you'll get the un-base64 encoded version of the data.
----------------------------------------
If you go with RubyFilter, included in the package is an rdeliver
script that can handle the processing of the incoming message in a
pretty "safe" way (reduced chance for bouncing the message, etc.).
When set up, you'd have an .rdeliver script that looked something like
this obviously over-simplified script:
def main
if message.multipart?
message.each_part do | part |
case part.header.media_type
when 'image'
File.open(some_unique_file_name, 'w') { |f|
f.write(part.decode)
}
end
end
end
Anyway, I'd be happy to walk you through any rough patches with
RubyMail/RubyFilter.