rick
rick

Reputation: 463

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 <[email protected]>>

If i do mail.from.first i get [email protected] part but how do it get the name bit ie. rogger rabbit ?

thanks a lot Rick

Upvotes: 2

Views: 313

Answers (3)

jstafford
jstafford

Reputation: 418

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.

Upvotes: 0

Pablo Castellazzi
Pablo Castellazzi

Reputation: 4184

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

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

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

Upvotes: 2

tadman
tadman

Reputation: 211540

You might want to try the decoded method:

mail.from.first.decoded

Upvotes: 0

Related Questions