using rails 3 mail

How do you get the name part from the front field of an email using the new rails 3 mail ?

In actual email there is a from field like this :

<From: rogger rabbit <roggerrabbit@hotmail.com>>

If i do mail.from.first i get roggerrabbit@hotmail.com part but how do it get the name bit ie. rogger rabbit ?

thanks a lot Rick

Answers


Looking at mail-2.2.17/lib/mail/elements/address.rb i found this:

a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>')
a.format       #=> 'Mikel Lindsaar <mikel@test.lindsaar.net> (My email address)'
a.address      #=> 'mikel@test.lindsaar.net'
a.display_name #=> 'Mikel Lindsaar'
a.local        #=> 'mikel'
a.domain       #=> 'test.lindsaar.net'
a.comments     #=> ['My email address']
a.to_s         #=> 'Mikel Lindsaar <mikel@test.lindsaar.net> (My email address)'

Try with something like mail[:from].addrs.collect {|a| a.display_name}


You might want to try the decoded method:

mail.from.first.decoded

After tearing my hair out all morning, I finally got this to work with:

message[:from].display_names.first

Where "message" is a mail object you might be iterating through.

Beware: If the sender doesn't have a display name, this will be nil.


Need Your Help

Loading javascript file asynchronously

javascript ajax asynchronous

I am trying to make an asynchronous call to a server in the following way

Writing a persistent perl script

linux perl bash unix

I am trying to write a persistent/cached script. The code would look something like this: