Search code examples
ruby-on-railsimapyahoo

Rails accessing Yahoo mail using IMAP - Parse Email


Unable to parse email accessed from Yahoo email.

require 'net/imap'
def index
  conn = Net::IMAP.new('imap.mail.yahoo.com', 993, ssl:true)
  conn.login("email", "mypassword")

  conn.select("INBOX")
  conn.uid_search(['ALL']).each do |uid|
    source   = conn.uid_fetch(uid, ['RFC822']).first.attr['RFC822']
  end
end

the new output I am only getting my first mail and not second one.In my second Mail I am only getting one line and rest of the text is missing also I am getting html content which i don't want in my output .

Welcome to Yahoo



--------------------------------------------------------------------


http://in.yahoo.com

--------------------------------------------------------------------

Welcome!

Your Yahoo username is now active.

Dear aniketshivamtiwari,

Everyday we're working hard to make your daily habits more inspiring and entertaining. While you're here, check out the amazing new features available on Flickr, the all-new Yahoo Weather app, and Yahoo Mail for mobile and tablet.

Yahoo Homepage:
http://in.yahoo.com/

Flickr:
http://www.flickr.com/

Yahoo Weather app:
https://in.mobile.yahoo.com/weather/

Yahoo Mail:
http://in.overview.mail.yahoo.com/mobile/

If you have any questions or need additional support, 
please visit our Help Center.
http://help.yahoo.com/kb/index?page=product&y=PROD_MAIL_ML&locale=en_IN

Thanks and have a great day.

Yahoo Customer Care  

--------------------------------------------------------------------

Privacy Policy
http://info.yahoo.com/privacy/in/yahoo/

Web Beacons in Email
http://info.yahoo.com/privacy/in/yahoo/webbeacons/E13

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Yahoo</title>
</head>

<body bgcolor="#C0C0C0" style="padding: 0; margin: 0;" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><IMG SRC="https://email.communications.yahoo.com/t/dbdbaEFWF6CAA2DsKXyCTXDaaC3a4X0-2~aaaa?r=ybu**y**JRLA**bKXGbObIZUNBLWEP6KNTbTGJGN@B@&amp;b=dklhhqvelsdjwfzXuf_1Xklr.fq&amp;i=" WIDTH="1" HEIGHT="1" STYLE="border:0px hidden transparent !important; display:none !important; max-height:0;" ALT=""/>
<table align="center" width="100%" valign="top" border="0" cellspacing="0" cellpadding="0" bgcolor="#C0C0C0" style="font-family:Arial,sans-serif;" dir="">
<tr>
<td align="center" valign="top">
    <table cellpadding="0" cellspacing="0" width="500" align="center" border="0" style="min-width:500px;">
    <tr>
    <td width="500" align="center">

<!--Preheader-->

<!--Preheader End-->

<!-- Logo Start -->
    <table width="500" border="0" cellspacing="0" cellpadding="0" bgcolor="#400090">
    <tr>
    <td width="20" height="83">&nbsp;</td>
    <td width="140" valign="middle"><a href="http://in.yahoo.com"><img src="http://l.yimg.com/dh/ap/default/131029/yahoo_logo.png" width="140" alt="Yahoo" border="0" style="display:block;"></a></td>
    <td width="340">&nbsp;</td>
    </tr>
    </table>
<!-- Logo End --> 

<!-- Hero Start -->     
    <table align="center" width="500" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="500"><img src="http://l.yimg.com/dh/ap/default/131105/hero.jpg" alt="" width="500" height="280" border="0" style="display:block;"></td>
    </tr>
    </table>
<!-- Hero End -->

<!-- Headline Start -->
    <table align="center" width="500" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
    <tr>
    <td width="21">&nbsp;</td>
    <td align="left">
        <table valign="top" width="458" border="0" cellspacing="0" cellpadding="0">
        <tr>
        <td height="20" style="font-size:20px; line-height:20px;">&nbsp;</td>
        </tr>
        <tr>
        <td style="font-family:Arial,sans-serif; font-size:23px; line-height:23px; color:#6D329C;">Your Yahoo username is now active.</td>
        </tr>
        <tr>
        <td height="20" style="font-size:20px; line-height:20px;">&nbsp;</td>
        </tr>
        </table>
    </td>
    <td width="21">&nbsp;</td>
    </tr>
    </table>
<!-- Headline End -->

<!-- Content -->
<table align="center" width="500" border="0" cellspacing="0" cellpadding="0" bgcolor="#464958">
    <tr>
    <td width="22">&nbsp;</td>
    <td width="456" align="left" style="font-size:13px; line-height:21px; color:#8C8F9E;">
        <table valign="top" width="456" border="0" cellspacing="0" cellpadding="0" >
        <tr>
        <td height="28" style=" font-size:28px; line-height:28px;">&nbsp;</td>
        </tr>
        <tr>
        <td style="font-size:15.5px; line-height:21px; color:#ffffff; font-weight:bold;">Dear aniketshivamtiwari,</td>
        </tr>
        <tr>
        <td height="20" style="font-size:20px; line-height:20px;">&nbsp;</td>
        </tr>
        <tr>
        <td style="font-family:Arial,sans-serif; font-size:13px; line-height:21px; color:#ffffff;">Everyday we're working hard to make your daily habits more inspiring and entertaining. While you're here, check out the amazing new features available on <a href="http://www.flickr.com" style="font-family:Arial,sans-serif;font-size:12px;line-height:normal;color:#ffffff;text-decoration:underline;" target="_blank">Flickr</a>, the all-new <a href="https://in.mobile.yahoo.com/weather/" style="font-family:Arial,sans-serif;font-size:12px;line-height:normal;color:#ffffff;text-decoration:underline;" target="_blank">Yahoo Weather</a> app, and <a href="http://in.overview.mail.yahoo.com/mobile/" style="font-family:Arial,sans-serif;font-size:12px;line-height:normal;color:#ffffff;text-decoration:underline;" target="_blank">Yahoo Mail</a> for mobile and tablet.</td>
        </tr>
        <tr>
        <td height="20" style="font-size:20px; line-height:20px;">&nbsp;</td>
        </tr>
        <tr>
        <td style="font-family:Arial,sans-serif; font-size:13px; line-height:21px; color:#ffffff;">If you have any questions or need additional support, please visit our <a href="http://help.yahoo.com/kb/index?page=product&y=PROD_MAIL_ML&locale=en_IN" style="font-family:Arial,sans-serif;font-size:12px;line-height:normal;color:#ffffff;text-decoration:underline;" target="_blank">Help Center</a>.</td>
        </tr>
        <tr>
        <td height="20" style="font-size:20px; line-height:20px;">&nbsp;</td>
        </tr>
        <tr>
        <td height="21" style="font-family:Arial,sans-serif; font-size:13px; line-height:21px; color:#ffffff;">Thanks and have a great day.</td>
        </tr>
        <tr>
        <td height="35" style="font-size:35px; line-height:35px;">&nbsp;</td>
        </tr>
        </table>
    </td>
    <td width="22">&nbsp;</td>
    </tr>
</table>
<!-- Content End -->

<!-- Footer Banner -->
    <table width="500" border="0" cellspacing="0" cellpadding="0" bgcolor="#686F7F">
    <tr>
    <td width="20">&nbsp;</td>
    <td align="left">
        <table width="460" cellpadding="0" cellspacing="0" border="0">
        <tr>
        <td height="31" style=" font-size:28px; line-height:28px;">&nbsp;</td>
        </tr>
        <tr>
        <td width="460" style="font-family:Arial,sans-serif; font-size:22px; line-height:21px; color:#ffffff;">Yahoo Customer Care</td>
        </tr>
        <tr>
        <td height="30" style=" font-size:28px; line-height:28px;">&nbsp;</td>
        </tr>
        <tr>
        </table>
    </td>
    <td width="20">&nbsp;</td>
    </tr>
    </table>
<!-- Footer Banner End -->


<!--Footer-->
<table align="center" width="500" border="0" cellspacing="0" cellpadding="0" bgcolor="#F4F3F8">
    <tr><td width="20">&nbsp;</td>
        <td width="460" align="left" valign="top" >
            <table width="460" border="0" cellspacing="0" cellpadding="0">
                <tr><td width="1" style="line-height:22px">&nbsp;</td></tr>
                <tr><td><a href="https://email.communications.yahoo.com/t/dcdbaEFWF6CAA2DsKXyCTXDaaC3a4X0-2~aaaa?r=ybu**y**JRLA**bKXGbObIZUNBLWEP6KNTbTGJGN@B@&amp;b=dklhhqvelsdjwfzXuf_1Xklr.fq&amp;i=E0t&amp;u=&amp;u=ewqs://fqcr.vderl.flp/mufyXfv/lk/1Xklr/4578B7D221B27B7664D0BEF428E0856C6C8D06331F9D7350A397541C39A8FE5B" style="font-family:Arial,sans-serif; font-size:12px; line-height:normal; color:#333333;text-decoration:underline;">Privacy Policy</a>&nbsp;&nbsp;|&nbsp;&nbsp;
                        <a href="https://email.communications.yahoo.com/t/dcdbaEFWF6CAA2DsKXyCTXDaaC3a4X0-2~aaaa?r=ybu**y**JRLA**bKXGbObIZUNBLWEP6KNTbTGJGN@B@&amp;b=dklhhqvelsdjwfzXuf_1Xklr.fq&amp;i=E0u&amp;u=&amp;u=ewqs://fqcr.vderl.flp/mufyXfv/lk/1Xklr/thYebdZrkv/B407C67DE06C54FE81854824B429D6B0BD263D4F2659AF544407499E6836E7557F3" style="font-family:Arial,sans-serif; font-size:12px; line-height:normal; color:#333333;text-decoration:underline;">Web Beacons in Email</a>
                    </td>
                </tr>
                <tr><td width="1" style="line-height:24px">&nbsp;</td></tr>
            </table>
        </td>
        <td width="20">&nbsp;</td>
    </tr>
</table>
<!--Footer End-->

    </td>
    </tr>
    </table>

</td>
</tr>
</table>
</body>
</html>
Password change for your Yahoo account


<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
        html { -webkit-text-size-adjust:none; }
        body { width:100%; margin:0 auto; padding:0; }
        .ReadMsgBody { width: 100%; }
        .ExternalClass { width: 100%; }
    .iosLinkPurple a { color:#400090; text-decoration:none; }
    .iosLinkGrey a { color:#454958; text-decoration:none; }
    .iosLinkSlate a { color:#424242; text-decoration:none; }
</style>
<!--[if gte mso 15]>
<style type="text/css" media="all">
ol,li {
    margin: 0;
    padding: 0;
    -webkit-text-size-adjust: 100%;
}
ol {
    counter-reset: foo;
    display: table;
    padding: 23px;
}
</style>
<![endif]-->

<!--[if gte mso 9]>
<style type="text/css">
.m1930{margin-left:19px; margin-right:30px;}
</style>
<![endif]-->

</head>
<body>
<div>
<table cellpadding="0" cellspacing="0" bgcolor="#ffffff" width="100%" style="table-layout:fixed;">
<tbody>
<tr>
    <td align="center" valign="top" style="min-width:500px;" >

        <table cellspacing="0" cellpadding="0" width="500" bgcolor="#400090">
        <tr>
            <td><a href="#"><img src="https://s.yimg.com/sf/email/logo.gif" style="display:block; border:0;" width="500" height="87" /></a></td>
        </tr>
        </table>
        <table cellspacing="0" cellpadding="0" width="500" bgcolor="#f4f3f8">
        <tr>
            <td >

                <table cellspacing="0" cellpadding="0"><tr><td height="23" style="line-height:23px;"><img src="images/sp.gif" height="1" width="1" style="display:block; border:0;" /></td></tr></table>

<div class="m1930" style="font-family:Helvetica, Verdana, Arial, sans-serif; font-size:17px; line-height:21px; color:#464958; padding-left:19px; padding-right:30px;">
                     Hello Aniket,<br/>
                    <table cellspacing="0" cellpadding="0"><tr><td height="14" style="line-height:14px;"><img src="images/sp.gif" height="1" width="1" style="display:block; border:0;" /></td></tr></table>
            The password for your Yahoo account, <b>&lrm;an**************ri&lrm;</b>, was recently changed.
            <br>
                    <table cellspacing="0" cellpadding="0"><tr><td height="14" style="line-height:14px;"><img src="images/sp.gif" height="1" width="1" style="display:block; border:0;" /></td></tr></table>
        If you made this change, you�re all set.<br><br>
<div style="background-color:#eae9ee; font-size: 15px;padding: 24px;padding-top:10px;padding-bottom: 10px;">
<p style="padding-left:9px">

If not, please take these steps to secure your account:
</p>
<ol  style="padding-left:25px">
<li>
Reset your password on any sign-in screen by selecting the <a href="https://edit.yahoo.com/config/eval_forgot_pw?.intl=in&.lang=en-IN" target="_blank">I can�t access my account</a>link.
</li>
<br>
<li>
Visit the Yahoo <a href="https://security.yahoo.com" target="_blank">Security Centre</a>to learn how to protect against malicious activity.
</li>
</ol>
<p style="padding-left:9px">
Feel free to contact <a href="https://io.help.yahoo.com/contact/index?page=contactform&locale=en_US&token=w5FCchB1dWFBK0e02%252BwEyuLoMKXEj8hiMaAs%252B8Qhn9Fh%252FFMQCC3ebEMZfW125VoXEPU0mYntyapqUt3Om%252BB5MDOlh33Rl0Gx6HB0ZEOq2Z3DeGCPpmGP05SOzmmkLPGu3%252FF8rDTGhm4%253D&selectedChannel=email-icon#voice-form" target="_blank">Customer Care</a> with any questions or concerns.
</p>
</div>
                    <table cellspacing="0" cellpadding="0"><tr><td height="14" style="line-height:14px;"><img src="images/sp.gif" height="1" width="1" style="display:block; border:0;" /></td></tr></table>
            Thanks, <br>
            Yahoo
                </div>

                <table cellspacing="0" cellpadding="0"><tr><td height="24" style="line-height:24px;"><img src="images/sp.gif" height="1" width="1" style="display:block; border:0;" /></td></tr></table>

                <!-- black line -->
                <table cellspacing="0" cellpadding="0" bgcolor="#687080" width="500"><tr><td height="1" style="line-height:1px;"><img src="images/sp.gif" height="1" width="1" style="display:block; border:0;" /></td></tr></table>
                <!-- /line -->

            </td>
        </tr>
        </table>
        <!-- /body -->
<table cellspacing="0" cellpadding="0"><tr><td height="16" style="line-height:16px;"></td></tr></table>

        <table cellspacing="0" width="500" style="table-layout:fixed;">
        <tr>
            <td align="left">
                <table cellspacing="0" cellpadding="0"><tr><td height="19" style="line-height:19px;"></td></tr></table>
                <div style="font-family:Helvetica, sans-serif; font-size:13px; line-height:18px; color:#333333;">
                  Replies sent to this email address cannot be answered.
                </div>
                <table cellspacing="0" cellpadding="0"><tr><td height="14" style="line-height:14px;"></td></tr></table>
            </td>
        </tr>
        </table>
    </td>
</tr>
</tbody>
</table>
</div>

</body>
</html>

Solution

  • require 'net/imap'
    def index
      conn = Net::IMAP.new('imap.mail.yahoo.com', 993, ssl:true)
      conn.login("email", "mypassword")
    
      conn.select("INBOX")
      conn.uid_search(['ALL']).each do |uid|
        # fetches the straight up source of the email for ymail to parse
        msg = conn.fetch(uid, 'RFC822').first.attr['RFC822']
    
        mail = Mail.read_from_string msg
        puts mail.subject
        puts mail.text_part.body.to_s
        puts mail.html_part.body.to_s
      end
    end
    

    note: Install gem 'mail' for parsing email