Max
Max

Reputation: 841

Rails IMAP: Get name of sender

How can one get the name of the sender of an e-mail (not e-mail address) in Rails, via Net::IMAP ?

Here is the code I use in Rails to get the e-mail of the sender:

imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.login(USERNAME, PASSWORD)
imap.select('Inbox')

imap.search(["ALL"]).each do |message_id|
  emails = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
  mail = Mail.read_from_string emails
  @email = Email.create(:sender => mail.from[0], :name => 'NAME_HERE')
end

imap.disconnect

The mail.from[0] correctly gets the e-mail address, but how can I get the name of the sender?

Upvotes: 2

Views: 741

Answers (1)

Max
Max

Reputation: 841

Current solution is to use mail[:from].display_names.first.

You'd have the following added next to the :sender => mail.from[0]:

:name => mail[:from].display_names.first

Upvotes: 3

Related Questions