如何使用 Rails3 应用程序解析电子邮件正文中的文本?
我需要从转发的电子邮件中获取姓名和电子邮件地址,其中正文中有这样一行:
From: john smith <[email protected]>
执行此操作的最佳方法是什么?我应该使用一个好的图书馆/宝石吗?
此外,该行会因发送的电子邮件客户端以及语言而异,因此我需要一种更可靠的方法,而不仅仅是匹配该字符串。
另外,有些客户不包括姓名,而只包括电子邮件地址,所以我需要能够处理这个问题。
I need to get the name and email address from a forwarded email where there is a line like this in the body:
From: john smith <[email protected]>
What is the best way to do this? Is there a good library/gem I should use?
Also this line will vary from which email client it's sent from and also what language, so I need a more robust way than just matching just this string.
Also some clients do not include the name but just the email address on this line so I need to be able to handle that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mail
对象会自动包含在使用ActionMailer
的 Rails 项目中 - 但如果您不这样做,则需要将其添加到Gemfile
中使用ActionMailer
更新
我没有正确阅读OP的问题 - 他们想从转发邮件的正文中获取电子邮件地址。我只是使用一些正则表达式:
注意:这对电子邮件的结构进行了相当多的假设 - 纯 HTML 电子邮件可以轻松阻止此正则表达式。
The
Mail
object is automatically included in Rails projects that useActionMailer
- but you'll need to add it to yourGemfile
if you're not usingActionMailer
Update
I didn't read the OP's question properly - they want to get an email address from the body of a forwarded mail. I'd just use some regex:
NB: this assumes a fair amount about the structure of the email - an HTML-only email can easily thwart this regex.