Ruby – 从text或html解析电子邮件

根据我的理解,在给定文档中扫描电子邮件时,正则表达不是最好的选择.我想知道这有什么替代方案吗?或者一些我不知道的最佳实践方式?

解决方法

对于解析作业,依赖库是一个好主意.你是对的,考虑到不同的情况等,图书馆总是比正则表达式更详细地处理问题.

一个用于解析电子邮件的Ruby库是Mail

Mail is an internet library for Ruby that is designed to handle emails
generation,parsing and sending in a simple,rubyesque manner.

[…] Mail has been designed with a very simple object oriented
system that really opens up the email messages you are parsing,if you
know what you are doing,you can fiddle with every last bit of your
email directly.

以下是访问电子邮件内容的示例:

mail = Mail.read('/path/to/message.eml')

mail.envelope.from   #=> '[email protected]'
mail.from.addresses  #=> ['[email protected]','[email protected]']
mail.sender.address  #=> '[email protected]'
mail.to              #=> '[email protected]'
mail.cc              #=> '[email protected]'
mail.subject         #=> "This is the subject"
mail.date.to_s       #=> '21 Nov 1997 09:55:06 -0600'
mail.message_id      #=> '<[email protected]>'
mail.body.decoded    #=> 'This is the body of the email...

它还使您能够解析多部分电子邮件,以及测试和提取附件.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...