Mail gem version 2 released

Sat Jan 23 17:05:00 -0800 2010

The past month has seen a flurry in activity on the Mail gem but I just pushed 2.0.3 to GemCutter, it is quite a release!

If you haven’t heard of mail, you can get some background here, here, here and here

Mail is a very Ruby way to handle emails.

Version 2.0.3 is the first gem release I have in the last couple of weeks, this is because I went through and (in good BDD style) refactored major parts of the Mail gem so it handles better.

Some of the major things were:

SMTP Delivery Agent revamped

I went through the SMTP delivery agent and cleaned it up, also adding examples for how you use Mail with GMail and MobileMe so there is no more guess work here.

Delivery agents are now instance based

This means that each mail object that you instantiate can have its own delivery method. Why is this important?

Well, say you are writing a web based email client for multiple users, but each user has their own SMTP hosts, when you make a mail object for that user, you could assign the delivery method for that user to that mail object, then delivery is just calling .deliver! on the mail object, and away it goes.

There is still default class wide settings for all the major delivery agents (SMTP, Sendmail and File) however, you can now over ride these.

mail1 = Mail.new
mail1.delivery_method #=> #<Mail::SMTP:0x101381c18 @setting
mail2 = Mail.new
mail2.delivery_method :sendmail
mail2.delivery_method #=> #<Mail::Sendmail:0x101381c18 @setting
mail1.delivery_method #=> #<Mail::SMTP:0x101381c18 @setting

Attachments are now just parts

Before, an Attachment had its own object type in Mail. This was nice and all, but was just added cruft that got in there during the BDD cycle. I ripped out the entire attachment class, and an attachment is now just a plain old Mail::Part. This makes the code simpler, which is good for everyone.

Mail also now has a very cool attachments API:

mail = Mail.new
mail.attachments #=> []
mail.attachments['filename.jpg'] = File.read('filename.jpg')
mail.attachments['file.pdf'] = {:content_type => 'application/x-pdf',
                                :content => File.read('file')
mail.attachments.length #=> 2
mail.attachments[0].filename #=> 'filename.jpg'
mail.attachments['filename.jpg'] #=> <# Mail::Part, filename = 'filename.jpg' ...>

Yes, that is an ArrayHashThingy™ class, and, it rocks :) It is actually an AttachmentsList object that inherits from Array and implements a custom [] class.

Thanks to David and Yehuda who were brainstorming on the new ActionMailer 3.0 API with me, which I used for inspiration for this implementation. (more on the ActionMailer 3.0 API that I am pair programming with José later :)

Mail returns default values for fields, that can be modified

Mail returns an array of address specs when you call mail.to and would re-initialize that array with new values when you called mail.to=.

However, this array object was just a result of a method, it was not a representation of the addresses within the address field, so then doing mail.to << value would seem to work (no error) but the address would get lost, for example:

# Old (unintuitive method)
mail = Mail.new("To: mikel@test.lindsaar.net")
mail.to #=> ['mikel@test.lindsaar.net']
mail.to << 'ada@test.lindsaar.net'
mail.to #=> ['mikel@test.lindsaar.net']

This now works on all Address fields that can take more than one address, so

# New (intuitive) method
mail = Mail.new("To: mikel@test.lindsaar.net")
mail.to #=> ['mikel@test.lindsaar.net']
mail.to << 'ada@test.lindsaar.net'
mail.to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']

Thanks to Sam for suggesting this feature.

Access returned to the Address objects

When you call mail.to you get a list of address spec strings (‘mikel@test.lindsaar.net’ for example), but it will not give you the display name, or formatted address etc.

To handle this, you can now call mail[:to] to get the actual ToField object, and then you can call #display_names, #formatted, and #addrs, the last of which will give you the actual Address objects in an array (the original behaviour of Mail), like so:

mail = Mail.new("To: Mikel Lindsaar <mikel@test.lindsaar.net>")
mail.to #=> ['mikel@test.lindsaar.net']
to = mail[:to]  #=> #<Mail::Field:0x10137c718...
to.addrs #=> [#<Mail::Address:2165620900 Address: |mikel@test.lindsaar.net| >]
to.addrs.each do |addr|
  puts "Formatted: #{addr.format}"
  puts "Display:   #{addr.display_name}"
  puts "Address:   #{addr.address}"
end
# Produces:
Formatted: Mikel Lindsaar <mikel@test.lindsaar.net>
Display:   Mikel Lindsaar
Address:   mikel@test.lindsaar.net
=> [#<Mail::Address:2165518560 Address: |Mikel Lindsaar <mikel@test.lindsaar.net>| >]

Thanks to Karl for suggesting this would be handy :)

More methods added to address fields

You can also access an array of the formatted, display_names or addresses directly from address fields:

mail = Mail.new("To: Mikel Lindsaar <mikel@test.lindsaar.net>")
mail[:to].addresses   
#=> ["mikel@test.lindsaar.net"]
mail[:to].formatted
#=> ["Mikel Lindsaar <mikel@test.lindsaar.net>"]
mail[:to].display_names
#=> ["Mikel Lindsaar"]

Remaining Stuff

There are a lot of other small bug fixes, parts now get sorted recursively on encode, body objects will accept an array of strings and call join on them, and many other small things that are in the commit and change logs. Check it out.

As always, tickets (and patches) are always welcome, I use GitHub’s Tracker for this. Or you can talk to us on the Mail Google Group

Happy Mailing!

blogLater

Mikel

  1. Androme Says:

    This is a very nice post for sure. legal studies school | Natural sciences degree | fire science school | Performing Arts degree | political science school

  2. restaurants for sale Says:

    I like the security of keeping copies on the server for a while. Thunderbird and Outlook allow this. Thanks.

  3. Mike Mondragon Says:

    Thanks Mikel, MMS2R http://rubygems.org/gems/mms2r version 3.0.0 is dependent upon the Mail gem now rather than TMail

  4. nfrdwe Says:

    Have I missed something, and is it possible to only delete some emails from the server – say only those that have been there longer than X days, or perhaps the oldest N emails. I like the security of keeping copies on the server for a while. Thunderbird and Outlook allow this. Thanks.

  5. Robin Says:

    QUESTION about receiving emails

    Hi, most of the discussion here (and almost all of the stuff about ActionMailer) deals with sending emails. However mail does seem to be able to receive all or some emails from a Pop3 server. But it only seems to allow deleting all the emails on the Pop3 server. Have I missed something, and is it possible to only delete some emails from the server – say only those that have been there longer than X days, or perhaps the oldest N emails. I like the security of keeping copies on the server for a while. Thunderbird and Outlook allow this. Thanks.

  6. Frank Says:

    I favor this posting. This is named a superb article. We are new here. I like your internet site too. This can be pretty awesome. i found some handy info right here. anyways thanks for sharing with us. I i’m looking foreword your following post. Cheers. Im just gonna shear this web site all this friends and i hope they live this blog. professional thesis writers

  7. Stefano Bernardi Says:

    I find it very hard to work with attachments now that they are just parts..

  8. Frank Says:

    I favor this posting. This is named a superb article. We are new here. I like your internet site too. This can be pretty awesome. i found some handy info right here. anyways thanks for sharing with us. I i’m looking foreword your following post. Cheers. Im just gonna shear this web site all this friends and i hope they live this blog. professional thesis writers

  9. Frank Says:

    I favor this posting. This is named a superb article. We are new here. I like your internet site too. This can be pretty awesome. i found some handy info right here. anyways thanks for sharing with us. I i’m looking foreword your following post. Cheers. Im just gonna shear this web site all this friends and i hope they live this blog. professional thesis writers

  10. erine Says:

    I have several issues encountered during the integration of the Mail returns field script… I don’t know how to get it work.
    ipad keyboard case

  11. clarinet maintenance Says:

    the emails on the Pop3 server. Have I missed something, and is it possible

  12. biomedical engineering salary Says:

    Great work learned a trick or two, thanks

  13. biomedical engineering salary Says:

    Great work learned a trick or two, thanks

  14. Buy Stromectal Says:

    Please write more articles like this one, I learned a lot!

  15. Buy Stromectal Says:

    Please write more articles like this one, I learned a lot!

  16. Digital Agency Orlando Says:

    I’ve never seen Steve P. Roma at one of his gyms and I’ve been working out there – on and off – for years. Dig deeper to the about WOW page. Here’s an excerpt I’d like to highlight.

  17. Pakaya Says:

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. however only some of the points were actually treated actually good, I believe digging deep for the issue to construct it more informative will actually help, will be looking ahead for more informative billet than this free itunes download

  18. Pakaya Says:

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. however only some of the points were actually treated actually good, I believe digging deep for the issue to construct it more informative will actually help, will be looking ahead for more informative billet than this free itunes download

  19. Mike Says:

    The simplicity of this blog is the main quality of this blog which i like and appreciate. music production

  20. Mike Says:

    The simplicity of this blog is the main quality of this blog which i like and appreciate. music production

  21. how fast to lose weight Says:

    I, for one, appreciate it.

  22. conan Says:

    Believe that everything happens for a reason.
    <a href=“=”http://www.freestylecustoms.net/" rel="folllow">brazilian jiu jitsu gi

  23. how fast to lose weight Says:

    I, for one, appreciate it.

  24. how fast to lose weight Says:

    I, for one, appreciate it.

  25. conan Says:

    Believe that everything happens for a reason.
    <a href=“=”http://www.freestylecustoms.net/" rel="folllow">brazilian jiu jitsu gi

  26. Dry Fruits Says:

    Have I missed something, and is it possible to only delete some emails from the server – say only those that have been there longer than X days, or perhaps the oldest N emails. I like the security of keeping copies on the server for a while. Thunderbird and Outlook allow this. Thanks.

  27. Atik Says:

    Thanks for this share mate. I have looking for this information for quite some time now and I am glad to come across your post. essays term papers

  28. Grand Rapids SEO Says:

    Yeah, this is my issue. I can’t delete select emails from the server. It just wipes out the entire database. Any help on this?

  29. Gifts For Men Says:

    That’s also my problem and I’m really looking forward to read your response in this same page..can you help me for this? Anyway, you have make a nice and informative blog!

  30. buy silver Says:

    Mikel, thanks for pushing 2.0.3 to GemCutter.

  31. Shohel Rana Says:

    I think it will work now

  32. Locksmith Torrance Says:

    Fantastic work ! Your web blog has presented me all the understanding I required .

  33. Christmas Gifts Says:

    I must appreciate you for the information you have shared.I find this information very useful and it has considerably saved my time.

  34. Gifts For Men Says:

    Found your weblog by accident for the second time these days so I considered I would have a nearer appear. I’ve just started producing my own blog site and modeling it right after what you have done. I hope mine is going to be as profitable as yours.

  35. Electronic Toys Says:

    It is great to read such thoughts. I have to agree to most of the mentioned minds and those are well investment in to future developments in this area. Even if it may have different effect scenarios, positive trend should be undoubted.

  36. Baju Wanita Says:

    Still leaerning Ruby on Rails and find its beauty :)
    Thinking of focusing more to Ruby than PHP.

  37. Gifts For Kids Says:

    Wonderful blog! I definitely love how it’s easy on my eyes and also the data are well written. I am wondering how I might be notified whenever a new post has been made.Thanks.

  38. rob Says:

    I had the same problem when I deleted e-mails from pop server it just wiped out the whole lot. I have been searching for a way to fix this but have been unable to find a solution anybody with the same problem as a solution I will be grateful to hear it as it is a very frustrating problem.

  39. Gifts For Women Says:

    Advantageously, the article is actually the best on this noteworthy topic. I fit in with your conclusions and will thirstily look forward to your coming updates. Just saying thanks will not just be sufficient, for the tremendous clarity in your writing. Gratifying work and much success in your site!

  40. Christmas Toys Says:

    I was very encouraged to find this site. The reason being that this is such an informative post. I wanted to thank for this informative analysis of the subject. I ate every bit of it and I submitted your site to some of the biggest social networks so others can find this blog.

  41. Pengar Says:

    This site is very interesting thanks for the information. Im looking for further articles

  42. Baju Wanita Says:

    I am quite excited with all the article content of your site. It would be my pleasure to gather some more strategies from your web-site and come up to offer people what I have benefited from you.
    Baju Wanita Import Korea

  43. Meilibahenling Says:

    This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want.HaHa). You definitely put a new spin on a subject thats been written about for years. Great stuff, just great!

  44. hvlp paint Says:

    I really enjoy reading your articles. thanks for sharing information …I found it quiet interesting, hopefully you hvlp gun

  45. weer vandaag Says:

    I enjoyed this blog at all specially its background is very charming and attractive for eyes,i liked it very much.

  46. ways to pregnant Says:

    I was waiting for this version, can i ask when will the new version will be released

  47. how to be a mom Says:

    The new added feature in this version will help us a lot

  48. bro spirit Says:

    Thanks for Nice blog…"great stories .. it will be helpful to all .. thanks so much God Bless"

  49. bro spirit Says:

    Thanks for Nice blog…"great stories .. it will be helpful to all .. thanks so much God Bless"

  50. webroot Says:

    i have read this and was very helpful information

  51. Makita Tool Parts Says:

    One more vote id here in your favor look at this "I do agree with all the ideas you have presented in your post. They’re very convincing and will definitely work. Still the posts are very short for starters. Could you please extend them a bit from next time? "

  52. Discount Power Tools Says:

    This is absolutely fantastic "It already has a history longer than 20 years, more and more people are collecting these shoes as a collection. "

  53. Mobil Bekas Semarang Says:

    Hi commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!Keep `em coming. you all do such a great job at such Concepts. can’t tell you how much I, for one appreciate all you do!

  54. key man insurance Says:

    This looks interesting. I have been looking for an option to handle mail via Ruby. Thanks

  55. Diet Plan Reviews Says:

    I’m not that much of a internet reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your website to come back down the road. Many thanks

  56. Mark Olsen Says:

    Emails nowadays are very important and helpful. It helps us send letters directly and saves our money. I personally uses emails a lot.

    Miami Roofer

  57. lyn cayce Says:

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. however only some of the points were actually treated actually good, I believe digging deep for the issue to construct it more informative will actually help, will be looking ahead for more informative billet than this. best christmas gifts 2011

  58. lyn cayce Says:

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. however only some of the points were actually treated actually good, I believe digging deep for the issue to construct it more informative will actually help, will be looking ahead for more informative billet than this. best christmas gifts 2011

  59. Peanut Machine Says:

    This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.

  60. Peanut Machine Says:

    This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.

  61. James Mason Says:

    This is a pretty in depth post about Mail Gem. I, for one, appreciate it.

    spy on my girlfriend’s phone

  62. Harga Samsung Galaxy Says:

    Thanks for the information. This is very useful to me. I waited for another article. Harga Samsung Galaxy Harga Nokia Asha 303 Berita Terkini

  63. fertibella success Says:

    The post is written in a very good manner and entails valuable information for me to work on. Thanks for sharing such remarkable ideas!

  64. erni Says:

    Thanks for your article, it’s very helpfull, just waiting for another tips…:)

    merry christmas buddy…:)

  65. dfh45fdd Says:

    Have I missed something, and is it possible to only delete some emails from the server – say only those that have been there longer than X days, or perhaps the oldest N emails. I like the security of keeping copies on the server for a while. Thunderbird and Outlook allow this. Thanks.ikpdjnvaj sg qqyjivwzl hg qtaowcahr

  66. mpie Says:

    I never found any interesting article like yours.jekardah

  67. Bible Study Lessons by T.O.D. Johnston Says:

    I was wondering when the SMTP Delivery agent revamped was going to be released. Bible Study Lessons by T.O.D. JohnstonNow it has been released my email experience has been without any problem.

  68. dramatic Says:

    then delivery is just calling .deliver! on the mail object, and away it goes.

  69. grand rapids djs Says:

    I always use SMTP Delivery Agent which is very useful and easy to implement especially when uploading materials for my website. grand rapids djs

  70. share accommodation Sydney Says:

    This is a wonderful site where we are getting more information. I have been talking with my friend about, he though it is really interesting as well. Keep up with your good work; I would come back to you.

  71. Sutton Locksmiths Says:

    Sutton Locksmiths – We are an emergency locksmith service that covers South London and the Surrey areas. Sutton Locksmiths

  72. Natural SEO Says:

    This is very useful things for me because i use smtp.
    thank you for sharing this.

  73. New Hampshire Wedding Venues Says:

    Image online on the net in excess of several time currently in addition to When i got to the site that write-up, still When i never ever located almost any useful document including your own property. It truly is rather value plenty of in my opinion. I believe, in the event many internet marketers in addition to blog writers manufactured beneficial information since you performed.

  74. vintage cameo lockets Says:

    I just started using the mail gem and it is truly a magnificent application

  75. Locksmiths Says:

    This really is a fantastic website, its really helped me a lot.

    Regards Locksmiths

  76. graphic design Says:

    The simplicity of this blog is the main quality of this blog which i like and appreciate

  77. causes of seizures Says:

    I recently found much useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing.

  78. caydenmiller Says:

    Thank you very much for taking your time to create this very informative site.I have learned a lot from your site.
    price of gold 2011

  79. Cuba Explorer Tours Says:

    This mail update has a really cool interface and it makes the software more efficient.

  80. Tow Dolly Says:

    This is sensitive issue, everyone has diffirent opinion regarding this issue, but make a shot with this article, You explain it very well. Love it ! How can i contact you.. I need further discussion.

    Mike
    Tow Dolly

  81. shamelessboyz Says:

    Awesome, these look fantastic :) Very artistic. Have a great vacation :)

  82. Locksmith Basingstoke Says:

    This is an awesome site we are a local Locksmith Basingstoke we have been serving the local community with all there locksmith requirements.

  83. Custom t-shirts Says:

    This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your design is so perfect!

  84. locksmith egham Says:

    Locksmith egham the local locksmiths in egham, call us today for a free quote.

  85. Storage World melbourne Says:

    I share many of you read the blog and I got to this conclusion on social service work you do through your blog

  86. safe storage melbourne Says:

    Wow what a blog is your blog, whatever the hymn should be,I did like all of your blog,

  87. SierraWhitaker Says:

    And in this span of time, we have understood that the most important part of this business is the client – we service what the client wants. We focus on innovation in our service offerings by investing in the hardware, software and network infrastructure so that our clients can tend to their online business and their customers without worrying about the complexities of the backbone of their online presence.

  88. Cheap Dedicated Servers Says:

    And in this span of time, we have understood that the most important part of this business is the client – we service what the client wants. We focus on innovation in our service offerings by investing in the hardware, software and network infrastructure so that our clients can tend to their online business and their customers without worrying about the complexities of the backbone of their online presence.

  89. Cheap Dedicated Servers Says:

    Yeah its very nice!

    Thanks for the article.

  90. learner permit test Says:

    Providing secure email is more important than ever, particularly securely removing them from the mail servers.

    People should not send credit card information in emails but they do and getting that stuff off the servers and erased is an important anti-hacking feature.

    Thanks for the article.

  91. Egham Locksmiths Says:

    This was an interesting article to read, thanks very much.

  92. diet meals to your door Says:

    Great information shared on the blog. diet meals to your door

  93. Business Directory Says:

    Thanks for the nice blog. It was very Useful for me. I’m happy I found this blog. Thank you for sharing with us, I always learn something new too from your post .

  94. Online Business Listings Says:

    I must say that overall I am really impressed with this blog. It is easy to see that you are passionate about your writing.

  95. Premium Directory Says:

    I i’m looking foreword your following post. Cheers. Im just gonna shear this web site all this friends and i hope they live this blog.

  96. Business Directory Says:

    This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.

  97. Jedi Force Says:

    the article is so helpful and attractive. It is full of useful material
    and many information which can be easily understand

  98. onlineearnings Says:

    oh , really I am surprised by seeing your site information. It is a great
    article for me. Ya , I think it’s a great work………

  99. alisaalba Says:

    hey, this is a very amazing site for me and i become
    very helpful for me. its really a top class solution for us.
    a lot of informatic material are posted on this site……

  100. cluster headache treatment Says:

    Between me and my wife we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated

  101. puska Says:

    This website is very exciting thanks for the details. Im looking for further articles

  102. portable table saw Says:

    This website is very exciting thanks for the details. Im looking for further articles

  103. freelance django developer Says:

    Im looking for further articles

  104. network marketing Says:

    Success is not impossible in Network Marketing. All making money takes is Leads, advice, tips and hard work! network marketing

  105. arginine Says:

    L-Arginine supplements help with better blood flow for an active lifestyle. Buy Arginine at discounted prices! arginine

  106. arginine Says:

    L-Arginine supplements help with better blood flow for an active lifestyle. Buy Arginine at discounted prices! arginine

  107. Gold ETFs Says:

    Success is not impossible in Network Marketing. All making money takes is Leads, advice, tips and hard work!

  108. payday one Says:

    I believe digging deep for the issue to construct it more informative will actually help, will be looking ahead for more informative billet than this free

  109. this site Says:

    I wanted to thank for this informative analysis of the subject. I ate every bit of it and I submitted your site to some of the biggest social networks so others can find this blog.

  110. best cash loans Says:

    Thanks for this share mate. I have looking for this information for quite some time now and I am glad to come across your post.

  111. quick quid Says:

    This can be pretty awesome. i found some handy info right here. anyways thanks for sharing with us

  112. payday loans Says:

    I believe digging deep for the issue to construct it more informative will actually help, will be looking ahead for more informative billet than this

  113. Business Directory Says:

    I like the security of keeping copies on the server for a while. Thunderbird and Outlook allow this. Thanks.

  114. 1800 Social Says:

    I feel strongly about it and love learning more on this topic. however only some of the points were actually treated actually good….

  115. Handyhomes Says:

    I wanted to thank for this informative analysis of the subject. I ate every bit of it

  116. Hotels for all Says:

    Wonderful blog! I definitely love how it’s easy on my eyes and also the data are well written. I am wondering how I might be notified whenever a new post has been made.Thanks.

  117. Web Design Leeds Says:

    I have looking for this information for quite some time now and I am glad to come across your post.

  118. Dominos Pizza Says:

    Fantastic work ! Your web blog has presented me all the understanding I required .

  119. cleopatra slots Says:

    I enjoyed this blog at all specially its background is very charming and attractive for eyes,i liked it very much.

  120. Beirut Dental Clinic Says:

    It was very well authored and easy to understand. Unlike additional blogs I have read which are really not good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!

  121. levitra canada Says:

    I have been seeking information on this topic for the past few hours and found your post to be well written

  122. HAJJ PACKAGES Says:

    Thanks for enhanced features, especially for “More methods added to address fields”, as previously it wasn’t that much user-friendly but now it works like a charm.

  123. lighting fixtures Says:

    I favor this posting. This is named a superb article. We are new here. I like your internet site too

  124. solarenergie gelsenkirchen Says:

    Unternehmenswebseite der GelsenPV GmbH – Ihr Systemhaus für Solarenergie.

  125. yasirra Says:

    Good article, thanks for pointing this out. Fortunately this topic is also presented in your blog, assuring a good coverage. Keep up the good work. options trading software

  126. Bournemouth Locksmiths Says:

    Thanks for the help, I am still having STMP problems. There is a conflict with the ports I believe. Any help?

  127. buy camel cigarettes Says:

    Wonderful blog! I definitely love how it’s easy on my eyes and also the data are well written. I am wondering how I might be notified whenever a new post has been made.Thanks.

Leave a Reply