python, mbox files and forwarding messages

Discussion in 'Python' started by Brad Tilley, Oct 26, 2004.

  1. Brad Tilley

    Brad Tilley Guest

    Can Python parse a mbox file and forward each individual message within
    that file to someone else?

    For example, let's say I have a 10MB mbox file that has 678 messages.
    I'd like to send each of the messages to ...
    how might I do that? Also, how might I handle messages that have an
    attachment associated with them during the forward process?

    Thanks,
    Brad
     
    Brad Tilley, Oct 26, 2004
    #1
    1. Advertising

  2. > Can Python parse a mbox file and forward each individual message within
    > that file to someone else?


    Yes.

    > For example, let's say I have a 10MB mbox file that has 678 messages.
    > I'd like to send each of the messages to ...
    > how might I do that? Also, how might I handle messages that have an
    > attachment associated with them during the forward process?


    import smtplib

    def find_all_at_linestart(data, st):
    start = 0
    if data[:len(st)] == st:
    start += len(st)
    yield 0
    end = len(data)
    st = \n'+st
    while start < end:
    nstart = data.find(st, start, end)
    if nstart == -1:
    return
    start = nstart+len(st)
    yield nstart

    def forward_email(mboxfname, email_from, email_to, host, port):
    mail = open('mboxfname', 'rb').read()
    lst = list(find_all_at_linestart(mail, 'From '))
    smtpc = smtplib.SMTP(host, port)
    for i in xrange(len(lst)-1):
    start = lst
    end = lst[i+1]
    #strip off the mbox From ... line
    r = mail.find('\n', start, end)
    email = mail[r+1:end]
    if email:
    smtpc.sendmail(email_from, [email_to], email)
    start = lst[-1]
    end = len(mail)
    r = mail.find('\n', start, end)
    email = mail[r+1:end]
    if email:
    smtpc.sendmail(email_from, [email_to], email)
    smtpc.close()


    Pardon if it doesn't quite work, this was done in my email client, and
    has not been tested.

    - Josiah
     
    Josiah Carlson, Oct 26, 2004
    #2
    1. Advertising

  3. Brad Tilley

    Jorgen Grahn Guest

    On Tue, 26 Oct 2004 13:08:36 -0400, Brad Tilley <> wrote:
    > Can Python parse a mbox file and forward each individual message within
    > that file to someone else?
    > [...] Also, how might I handle messages that have an
    > attachment associated with them during the forward process?


    By ignoring them. You don't have to be MIME-aware to forward a message, and
    the mbox format(s) aren't MIME-aware either.

    /Jorgen

    --
    // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ algonet.se> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Oct 28, 2004
    #3
    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. steph

    mbox files

    steph, Nov 3, 2004, in forum: Java
    Replies:
    1
    Views:
    529
    Oscar kind
    Nov 3, 2004
  2. Replies:
    3
    Views:
    565
    John Nagle
    Aug 27, 2010
  3. Andrew Tomazos
    Replies:
    5
    Views:
    581
  4. Tuxedo
    Replies:
    18
    Views:
    234
    Tuxedo
    Jun 17, 2007
Loading...

Share This Page