2012-05-01 96 views
1

我正在尋找一些方法來解析如何使用nokogiri解析郵件。以下是一個示例電子郵件。我已閱讀此文檔http://nokogiri.org/tutorials/parsing_an_html_xml_document.html和Google營業時間。我是Ruby on Rails的新手,並且正在尋找一個很好的示例或詳細說明。感謝您的時間。使用nokogiri解析郵件

MIME-Version: 1.0 
Received: by 10.76.129.52; Mon, 30 Apr 2012 22:11:24 -0700 (PDT) 
Date: Mon, 30 Apr 2012 22:11:24 -0700 
Message-ID: <[email protected]om> 
Subject: Customize Gmail with colors and themes 
From: Gmail Team <[email protected]> 
To: parse email <[email protected]> 
Content-Type: multipart/alternative; boundary=bcaec545501825242f04bef29a74 

--bcaec545501825242f04bef29a74 
Content-Type: text/plain; charset=ISO-8859-1 
Content-Transfer-Encoding: quoted-printable 

To spice up your inbox with colors and themes, check out the Themes tab 
under Settings. 
     Customize Gmail =BB <https://mail.google.com/mail/#settings/themes> 


Enjoy! 

- The Gmail Team 
[image: Themes thumbnails] 

Please note that Themes are not available if you're using Internet Explorer 
6.0. To take advantage of the latest Gmail features, please upgrade to a 
fully supported 
browser<http://support.google.com/mail/bin/answer.py?answer=3D6557&hl=3Den&= 
utm_source=3Dwel-eml&utm_medium=3Deml&utm_campaign=3Den> 
. 

--bcaec545501825242f04bef29a74 
Content-Type: text/html; charset=ISO-8859-1 

<html> 
<font face="Arial, Helvetica, sans-serif"> 
<p>To spice up your inbox with colors and themes, check out the Themes tab 
under Settings.</p> 

<table cellpadding="0" cellspacing="0"> 
    <col style="width: 1px;"/> 
    <col/> 
    <col style="width: 1px;"/> 
    <tr> 
    <td></td> 
    <td height="1px" style="background-color: #ddd"></td> 
    <td></td> 
    </tr> 
    <tr> 
    <td style="background-color: #ddd"></td> 
    <td background="https://mail.google.com/mail/images/welcome-button-background.png" 
     style="background-color: #ddd; background-repeat: repeat-x; 
      padding: 10px; font-size: larger"> 
      <a href="https://mail.google.com/mail/#settings/themes" 
      style="font-weight: bold; color: #000; text-decoration: none; 
      display: block;"> 
     Customize Gmail &#187;</a> 
    </td> 
    <td style="background-color: #ddd"></td> 
    </tr> 
<tr> 
    <td></td> 
    <td height="1px" style="background-color: #ddd"></td> 
    <td></td> 
    </tr> 
</table> 

<p>Enjoy!</p> 

<p>- The Gmail Team</p> 

<img width="398" height="256" src="https://mail.google.com/mail/images/gmail_themes_2.png" 
alt="Themes thumbnails" /> 

<p><font size="-2" color="#999">Please note that Themes are not available if 
you're using Internet Explorer 6.0. To take advantage of the latest Gmail 
features, please 
<a href="http://support.google.com/mail/bin/answer.py?answer=6557&hl=en&utm_source=wel-  
eml&utm_medium=eml&utm_campaign=en"><font color="#999"> 
upgrade to a fully supported browser</font></a>.</font></p> 

</font> 
</html> 

--bcaec545501825242f04bef29a74-- 

回答

1

Nokogiri非常適合解析HTML,但您在這裏得到的是一封電子郵件。嘗試使用TMail首先從電子郵件中獲取HTML部分,然後您可以使用Nokogiri來解析該部分。從TMail文檔中推斷出來,你可以這樣做:

email = TMail::Mail.load('my_email.eml') 
    html_doc = Nokogiri::HTML(email.body) 
+0

除了使用TMail獲取HTML部分之外,還有其他方法嗎? – user1367188

+0

Nokogiri不知道如何閱讀電子郵件,只有HTML/XML格式的內容。由於電子郵件包含標題,內容類型分區等,因此必須先將所有這些內容解析爲HTML。 理論上來說,你可以嘗試一些自定義的基於正則表達式的(eeks!)策略來找到Content-Type:text/html這一行,但這很脆弱而且很難做到。 –

+0

好的謝謝。我被要求解決沒有TMail。聽起來很瘋狂。 – user1367188