Professional Web Applications Themes

Email parsing - Ruby

Hi there. Question, Im using net/pop to collect email, is there any way I can get fields as an object from it ? Such as subject, attachments, etc ? Thanks Rove Monteux -- Rove Monteux Systems Administrator com...

  1. #1

    Default Email parsing

    Hi there.

    Question, Im using net/pop to collect email, is there any way I can get
    fields as an object from it ? Such as subject, attachments, etc ?

    Thanks

    Rove Monteux

    --
    Rove Monteux
    Systems Administrator

    com




    Rove Guest

  2. #2

    Default Re: Email parsing

    That sounds good, thanks !


    Rove Monteux

    Sam Roberts wrote:
     
    >
    >I don't think ruby has any builtin mail message decoders, but I've used
    >rubymail to do this kind of thing, and it was easy. There was at least 1
    >other package, I used rubymail because I thought it looked the best at
    >the time, and can't for the life of me remember why anymore!
    >
    >Cheers,
    >Sam
    >
    >
    >
    >
    >
    >[/ref]

    --
    Rove Monteux
    Systems Administrator

    com




    Rove Guest

  3. #3

    Default Re: Email parsing

    On Sat, 7 Feb 2004, Rove Monteux wrote:
     

    http://raa.ruby-lang.org/list.rhtml?name=rubymail

    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

  4. #4

    Default Re: Email parsing

    Thanks again,

    I see that the 'Guide' session is incomplete, specially on the subjects
    of decoding, encoding and using rubymail with net/pop and net/smtp
    (whats basically you would use rubymail for), any
    sugestions/examples/basic doentation posted somewhere on that by any
    chance ?

    Again, thanks.

    Cheers

    Rove Monteux

    Ara.T.Howard wrote:
     
    >
    > http://raa.ruby-lang.org/list.rhtml?name=rubymail
    >
    >-a
    >
    >[/ref]

    --
    Rove Monteux
    Systems Administrator

    com




    Rove Guest

  5. #5

    Default Re: Email parsing

    Hi Sam,

    Yes, I got till there using the net/pop alright, can retrieve headers
    (subject, from, to etc). Im a bit lost now, I request the message.body
    as it says on the docs, and it returns me nil, both on multipart or not
    messages.

    Follows my snippet (message is the full message retrieved with m.pop),


    # do whatever with the message
    def process_message(message)
    p = RMail::Pr.new
    m = p.p(message)

    # deals with the header
    h = m.header()
    subject = h.subject
    from = h.from
    to = h.to
    puts "from: " + from.first
    puts "to: " + to.first
    puts "subject: " + subject

    # deals with the body
    puts m.to_s
    if m.multipart?()
    m.each_part { |g|
    puts g.to_s }
    else
    puts m.decode().to_s
    end

    end


    Everything until the 'body' bit works correctly, and according to the docs,

    http://www.lickey.com/rubymail/rubymail/doc/classes/RMail/Message.html#M000015

    Its pretty much the same as with the header, as long as you check if it
    is multipart or not. Doing a to_s should return me something, what it
    doesnt, so obviously Im doing something wrong. Any ideas on that ? :)


    Thanks again !

    Cheers

    Rove Monteux



    Sam Roberts wrote:
     
    >
    >Well, that's one way. The other way is to read a local mailbox in unix
    >mbox format (also the format used by Apple's Mail.app), and to injecting
    >mail into the delivery system by piping to /usr/bin/mail.
    >
    >Anyhow, it took me a minute to figure out how I did this, too.
    >
    >Here's some code ripped from a script I wrote to convert mbox files to
    >vcards, with some comments added:
    >
    >#!/usr/bin/ruby -w
    >
    >require 'rmail'
    >
    >addrs = RMail::Address::List.new
    >
    >ARGV.each { |mbox|
    > File.open(File.expand_path(mbox)) { |file|
    > puts "Reading: #{file.path}..."
    >
    > RMail::Mailbox::MBoxReader.new(file).each_message { |input|
    >
    > # input is a single string that is one message. I get it here from
    > # the MBoxReader, you'll get it as the return value of pop.pop (used
    > # in the mode where it returns the whole message as a string).
    >
    > # Then you have to p it to create a message, do it like this:
    > message = RMail::Pr.read(input)
    >
    > # Now you can play with the mesage:
    > header = message.header
    > addrs.concat(header.from)
    > addrs.concat(header.recipients)
    > addrs.concat(header.reply_to)
    > }
    > }
    >}
    >
    >addrs = addrs.uniq
    >
    >puts "Unique addrs: #{addrs.size}"
    >
    >
    >Cheers,
    >Sam
    >
    >
    >
    >
    >
    >[/ref]

    --
    Rove Monteux
    Systems Administrator

    com




    Rove Guest

  6. #6

    Default Re: Email parsing

    Hi Sam !

    Yes Im pretty puzzled now,

    puts m.to_s

    Outputs the whole of the message, including the two parts (normal body + attachment).

    Now, p m.body and p part return nil. And I can clearly see that the email message is properly formed and has a body (+ attachment).

    Gonna browse through the sources for rubymail and try to trace down whats happening, or where Im going wrong.

    Anyway I agree with the 4 day schema (as long's still paid full time, of course) :)


    Cheers and thanks again !


    Rove Monteux



    Sam Roberts wrote:
     
    >
    >Sorry, your on your own now, luckily you have the source!
    >
    >

    >
    >I think this should be g.decode().
    >
    >

    >
    >And this shouldn't have a .to_s.
    >
    >decode() is just a wrapper around a check that its not multi-part, then
    >looks to see if it's qp or base64 encoded, and decodes if appropriate.
    >
    >No docs on what the parts are, but my bet is that they are a
    >RMail:Message, because that's how MIME works: its recursive. Each of
    >those parts could have parts, too.
    >
    >

    >
    >First thing I'd do, though, is this:
    >
    >p m.body
    >
    >if m.body.is_a?(Array)
    > m.body.each do |part|
    > p part
    > end
    >end
    >
    >Just to kinda check out what Message.body is...
    >
    >The mail message you're looking at DOES have a body, doesn't it? :-)
    >
    >I wrote a library like this in C++, and it was fun, but got mostly
    >abandoned. Lickey isn't really working on this now, he's got a life, it
    >sounded like, last time I exchanged mail with him.
    >
    >I'd like to do some work on rubymail, I've some local changes I've made,
    >and I'd like to add some net/(imap,pop) integration, stuff like that,
    >but there are only so many hours in the day. Canada's pretty
    >progressive, but we still haven't managed to legislate a 26 hour day (or
    >even a 4-day workweek, sadly).
    >
    >Good luck,
    >Sam
    >
    >
    >
    >
    >
    >[/ref]

    --
    Rove Monteux
    Systems Administrator

    com




    Rove Guest

  7. #7

    Default Re: Email parsing


    Grugitate Mail
    http://raa.ruby-lang.org/list.rhtml?name=gurgitate-mail

    robert


    "Rove Monteux" <com> schrieb im Newsbeitrag
    news:com... 

    Robert Guest

Similar Threads

  1. Email button does not open to preferred email client in Windows 98SE
    By Catherine_Arceneaux@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 6
    Last Post: May 20th, 11:18 PM
  2. Email parsing with php
    By Matthieu Peschaud in forum PHP Development
    Replies: 0
    Last Post: October 21st, 12:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139