pop3 body email

Discussion in 'Ruby' started by Erika, Dec 9, 2008.

  1. Erika

    Erika Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi,

    I have to check the body of the email which is a html code, like the
    following:
    <tr><td>Text1: </td>

    <td>Text2</td>

    I have to check that for "Text1" what "Text2" is shown, which can vary
    for different reasons.

    I managed to get the emails, to check the subject of the email to
    identify the correct email, but I can't get to resolve this problem
    which I mentioned previously.

    Thank you,
    Erika

    require 'net/pop'

    pop = Net::pOP3.new('smtp server name')
    pop.start('user', 'parola')
    if pop.mails.empty?
    puts 'No mail.'
    else
    i = 0
    pop.each_mail do |m|
    sbj=m.header.split("\r\n").grep(/^Subject:/)
    rate=m.pop.grep(/^Test1/)
    puts sbj
    puts m.pop
    i += 1
    end
    puts "#{pop.mails.size} mails popped."
    end
    pop.finish
    Erika, Dec 9, 2008
    #1
    1. Advertising

  2. Erika wrote:
    > Hi,
    >
    > I have to check the body of the email which is a html code, like the
    > following:
    > <tr><td>Text1: </td>
    >
    > <td>Text2</td>
    >
    > I have to check that for "Text1" what "Text2" is shown, which can vary
    > for different reasons.


    If you are already able to read the E-mail via POP3, and the body
    consists of a single text/html part, then this just becomes a question
    about parsing HTML. Hpricot is usually cited as the best library for
    doing that. I'd say it's not worth digging about with regexps when you
    can do the job properly.

    If your E-mail is a multipart/alternative then you may need a bit more
    work to extract the text/html part first. The rubymail library may help
    here.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Dec 9, 2008
    #2
    1. Advertising

  3. Erika

    Robert Dober Guest

    On Tue, Dec 9, 2008 at 9:29 PM, Brian Candler <> wrote:
    > Erika wrote:
    >> Hi,
    >>
    >> I have to check the body of the email which is a html code, like the
    >> following:
    >> <tr><td>Text1: </td>
    >>
    >> <td>Text2</td>
    >>
    >> I have to check that for "Text1" what "Text2" is shown, which can vary
    >> for different reasons.

    >
    > If you are already able to read the E-mail via POP3, and the body
    > consists of a single text/html part, then this just becomes a question
    > about parsing HTML. Hpricot is usually cited as the best library for
    > doing that. I'd say it's not worth digging about with regexps when you
    > can do the job properly.

    Thx a lot there, but please do not underestimate regexen, especially
    as they will become more powerful in 1.9.

    Are you aware of dependencies of Hpricot notwithstanding that it is a
    wonderful tool.
    Being an old Unix guy however I feel that you do not need a full
    fledged library + dependencies if a three liner
    can do the job.
    However if this was only an example and OP needs more parsing Hpricot
    is a very sensible way to go.
    Cheers
    R
    Robert Dober, Dec 9, 2008
    #3
  4. Erika

    Erika Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi,

    I tried using Hpricot in the following way:
    .....
    Connect with POP3 and get the email
    .....
    email = TMail::Mail.parse(m.pop)
    body2=Hpricot(email.body)
    elements = body2.search("/html/body/table//td").collect{|k| k.inner_html.split(',') unless k.inner_html =~ /</ }.flatten.compact
    puts elements

    ....

    Is there any better way to extract the info?

    My html email looks like something like this:
    <table>
    <tr><td>Info1</td><td>Info2</td></tr>
    </table>
    <table>
    <tr><td>Info1</td><td>Info2</td></tr>
    </table>
    <table>
    <tr><td>Info1</td><td>Info2</td></tr>
    </table>
    <table>
    <tr><td>Info1</td><td>Info2</td></tr>
    <tr><td>Info1</td><td>Info2</td></tr>
    <tr><td>Info1</td><td>Info2</td></tr>
    </table>

    So the general rule is that i have 3 tables and I need to check that for Info1 the correct Info2 is shown. Every Info1 / Info2 can vary.

    Is there a better way to have in to arrays for example Info1 and Info2. Because my solution is ok, only I subtract all the info from the html code which I need to parse one more time.

    Thanks,
    Erika


    ________________________________
    From: Robert Dober <>
    To: ruby-talk ML <>
    Sent: Tuesday, December 9, 2008 11:02:39 PM
    Subject: Re: pop3 body email

    On Tue, Dec 9, 2008 at 9:29 PM, Brian Candler <> wrote:
    > Erika wrote:
    >> Hi,
    >>
    >> I have to check the body of the email which is a html code, like the
    >> following:
    >> <tr><td>Text1: </td>
    >>
    >> <td>Text2</td>
    >>
    >> I have to check that for "Text1" what "Text2" is shown, which can vary
    >> for different reasons.

    >
    > If you are already able to read the E-mail via POP3, and the body
    > consists of a single text/html part, then this just becomes a question
    > about parsing HTML. Hpricot is usually cited as the best library for
    > doing that. I'd say it's not worth digging about with regexps when you
    > can do the job properly.

    Thx a lot there, but please do not underestimate regexen, especially
    as they will become more powerful in 1.9.

    Are you aware of dependencies of Hpricot notwithstanding that it is a
    wonderful tool.
    Being an old Unix guy however I feel that you do not need a full
    fledged library + dependencies if a three liner
    can do the job.
    However if this was only an example and OP needs more parsing Hpricot
    is a very sensible way to go.
    Cheers
    R
    Erika, Dec 10, 2008
    #4
  5. pop3 error-Need Help

    Hi,

    I tried with pop to get my inbox messages from my gmail account.but i
    got "execution expired" error.plz help me.My code is

    require 'net/pop'
    puts "Running Mail Importer..."
    begin
    if Net::pOP3.start("pop.gmail.com", nil, "gmailaccount",
    "gmailpassword")
    Net::pOP3.start("pop.gmail.com", nil, "gmailaccount", "gmailpassword")
    do |pop|
    puts "After....."
    if pop.mails.empty?
    puts "NO MAIL"
    else
    pop.mails.each do |email|
    begin
    puts "receiving mail..."
    Notifier.receive(email.pop)
    email.delete
    rescue Exception => e
    puts "Error receiving email at " + Time.now.to_s + "::: " +
    e.message
    end
    end
    end
    end
    else
    puts "Not Connecting..."
    end
    rescue Exception => e
    puts e
    end

    puts "Finished Mail Importer."
    --
    Posted via http://www.ruby-forum.com/.
    Poornima Dhanasekar, Feb 17, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robin Becker

    pop3 email header classifier?

    Robin Becker, Sep 19, 2003, in forum: Python
    Replies:
    6
    Views:
    418
    David Mertz
    Sep 23, 2003
  2. Paul Schmidt

    POP3 and email

    Paul Schmidt, Jun 6, 2004, in forum: Python
    Replies:
    4
    Views:
    432
    Larry Bates
    Jun 9, 2004
  3. moni
    Replies:
    7
    Views:
    1,699
    Greg R. Broderick
    Oct 6, 2007
  4. Stephane Wirtel

    class to read an email from net/pop3 ?

    Stephane Wirtel, Oct 16, 2005, in forum: Ruby
    Replies:
    5
    Views:
    128
    Lyndon Samson
    Oct 18, 2005
  5. Deepa Rajendran
    Replies:
    0
    Views:
    131
    Deepa Rajendran
    Feb 23, 2009
Loading...

Share This Page