hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Fwding with RubyMail #

by why in bits

When it comes to e-mail manipulation, RubyMail has always been my answer. The fortunate thing about RubyMail is that it parses each message into RMail::Message objects, which can also be converted back into e-mail with to_s.

This makes for simple modification of messages. The issue is slightly complicated by stuff like multiple From: fields. So, if you’re forwarding a message and simply want to modify header fields, be sure to delete the fields from the message first.

Here’s an excerpt from a simple mailing list script:

 msg.header.delete('From')
 msg.header.subject = "[#{ list_title }] #{ msg.header.subject }" 
 msg.header['From'] = "#{ from_recip } <#{ reply_to }>" 
 Net::SMTP.start( 'localhost' ) do |smtp|
     list['recipients'].each do |recip|
         to_email = "#{ recip['name'] } <#{ recip['email'] }>" 
         msg.header.delete('To')
         msg.header['To'] = to_email
         smtp.send_message( msg.to_s, msg.header['Reply-To'], to_email )
     end
 end

Note: This code won’t run from Irb unless you setup some variables and arrays. The point is to simply illustrate deletion of the header fields.

said on 20 Jan 2005 at 16:47

I’m still looking for an email package that will let me do “smtp.attach_file(‘somefile.xls’)”. Perl’s Mail::Sender can do this. Time to swipe some code? Or can RubyMail already do this?

said on 20 Jan 2005 at 17:59

the new comment boxes are sexual.

said on 21 Jan 2005 at 06:01

They make comments look as if etched eternally into stone. Which is nice, come to think of it.

said on 21 Jan 2005 at 09:52

You guys have your whole lives ahead of you to talk about boxes.

said on 21 Jan 2005 at 12:12

unless we all die of rubella

said on 22 Jan 2005 at 18:52

Gikt Agur!

Comments are closed for this entry.