2014-02-08 37 views
2

我使用Mikel的郵件gem成功,但我試圖從電子郵件地址拉出名字,沒有太多的運氣。使用Mikel的郵件寶石訪問未解析的電子郵件地址

如果我收到'Mikel Lindsaar([email protected])',那麼我可以訪問[email protected],但不能訪問'Mikel Lindsaar'。

我一直在使用郵件對象本身的to/cc/from方法。我不知道我會怎麼使用的Mail :: Address類的文檔中:http://www.ruby-doc.org/gems/docs/m/mail-2.5.4/Mail/Address.html

回答

2

這裏是你所需要的一個簡單的例子:

mail = Mail.new do 
    from "Mikel Lindsaar <[email protected]>" 
end 

mail.header[:from].field.display_names #=> ["Mikel Lindsaar"] 

以上是一種尷尬,但工程。標題[:from]上的#field方法返回一個Mail::FromField,它或多或少是Mail::CommonAddress,後者又封裝了若干個Mail::Address es。有點複雜的權利? CommonAddress有幾個方便的方法來操縱該地址列表。

您可以通過

mail.from.instance_variable_get("@field") 
+0

非常感謝,尼科斯得到相同FromField實例。它運作良好,並會做得很好。 –

相關問題