Postal pearls. Extract recipient name and address as separate values

Using a mailbox (i.e. Rails + ActionMailer), is there a clean way to get the display name of the recipient?

I can get the address with:

mail.to.first 

And I can get the formatted display name + address with

 mail.header_fields.select{ |f| f.name == "To" }.first.to_s 

But how can I get only part of the display name (i.e. before < and > ). I know that someone is going to offer Regex, but this is not what I am looking for, since then I will have to parse some kind of encoding, which probably already makes the mail pearls. I am the author of the popular Mailer library in PHP, and I know about the pitfalls, just assuming the bit before < and > is a human readable heading when 8-bit characters come into play.

I can do it:

 mail.header_fields.select{ |f| f.name == "To" }.first.parse.individual_recipients.first.display_name.text_value 

But should there be a better way? :)

+6
source share
1 answer

I thought sorry. For those who address this topic, looking for a solution:

 mail[:to].display_names.first 
+18
source

Source: https://habr.com/ru/post/896025/


All Articles