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

前端之家收集整理的这篇文章主要介绍了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   #=> 'mikel@test.lindsaar.net'
mail.from.addresses  #=> ['mikel@test.lindsaar.net','ada@test.lindsaar.net']
mail.sender.address  #=> 'mikel@test.lindsaar.net'
mail.to              #=> 'bob@test.lindsaar.net'
mail.cc              #=> 'sam@test.lindsaar.net'
mail.subject         #=> "This is the subject"
mail.date.to_s       #=> '21 Nov 1997 09:55:06 -0600'
mail.message_id      #=> '<4D6AA7EB.6490534@xxx.xxx>'
mail.body.decoded    #=> 'This is the body of the email...

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

原文链接:https://www.f2er.com/ruby/269373.html

猜你在找的Ruby相关文章