imap folder scanner

Discussion in 'Python' started by Kun, Mar 24, 2006.

  1. Kun

    Kun Guest

    Hey guys, I would like to have a code in python (as simple as possible)
    to scan a specific folder in my mailbox and if the subject is equal to,
    say, 'BIKES', I would like to have the code automatically send the
    SENDER an email saying something like "We have received your Email".
    Furthermore, I would also like to somehow save the sender's email into a
    list which would be compiled by another python program into an html file
    that would show a list of email addresses whose subject matched 'BIKE'

    I know i am asking for a lot but since i am new to python, can someone
    help me out with this? Whether its tips or code, i'll be very excited to
    hear your answer. Thanks.
     
    Kun, Mar 24, 2006
    #1
    1. Advertising

  2. Kun

    Kun Guest

    Re: UPDATE imap folder scanner

    Okay So I got the 'search' part to work, which outputs me a long list of
    message numbers. how do i use that list of message numbers to fetch the
    'from' address for each one and send them a confirmation email?

    is this some sort for loop?

    any help would be greatly appreciated.

    cheers.


    Kun wrote:
    > Hey guys, I would like to have a code in python (as simple as possible)
    > to scan a specific folder in my mailbox and if the subject is equal to,
    > say, 'BIKES', I would like to have the code automatically send the
    > SENDER an email saying something like "We have received your Email".
    > Furthermore, I would also like to somehow save the sender's email into a
    > list which would be compiled by another python program into an html file
    > that would show a list of email addresses whose subject matched 'BIKE'
    >
    > I know i am asking for a lot but since i am new to python, can someone
    > help me out with this? Whether its tips or code, i'll be very excited to
    > hear your answer. Thanks.
     
    Kun, Mar 24, 2006
    #2
    1. Advertising

  3. A very simple example...

    import imaplib
    m = imap.IMAP4(<myserver ip or host>)
    m.login(username,password)
    m.select('myfolder')
    status, data = m.search(None,'(SUBJECT "BIKES")')
    assert status=='OK', "Error. Message: %s"%data
    data = data[0] #you get your results in a list and search returns only
    one result
    assert data,"No results"
    #cool, we have results, but IMAP's search command only returns IDs so
    we have to fetch
    #msgs now
    status,senders = m.fetch(data.replace('
    ',','),'(BODY.PEEK[HEADER.FIELDS (FROM)])')
    assert status=='OK', "Error. Message: %s"%data

    Now you just have to parse the "senders" data. There are many examples
    about sending emails with python, like this one:

    def send_notice():
    import smtplib
    msg = 'we got your mail, indeed'
    from email.MIMEText import MIMEText
    mail = MIMEText(msg, 'plain', 'utf-8')
    mail['From'] =fro=''
    mail['Subject'] = "Spam machine"
    mail['To'] = to = ''
    server = smtplib.SMTP('localhost')
    errors = server.sendmail(fro, to, mail.as_string())
    server.quit()

    That other program should be very simple to make now.

    Sebastjan

    On 3/24/06, Kun <> wrote:
    > Hey guys, I would like to have a code in python (as simple as possible)
    > to scan a specific folder in my mailbox and if the subject is equal to,
    > say, 'BIKES', I would like to have the code automatically send the
    > SENDER an email saying something like "We have received your Email".
    > Furthermore, I would also like to somehow save the sender's email into a
    > list which would be compiled by another python program into an html file
    > that would show a list of email addresses whose subject matched 'BIKE'
    >
    > I know i am asking for a lot but since i am new to python, can someone
    > help me out with this? Whether its tips or code, i'll be very excited to
    > hear your answer. Thanks.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Sebastjan Trepca, Mar 24, 2006
    #3
  4. Kun

    Kun Guest

    Sebastjan Trepca wrote:
    > A very simple example...
    >
    > import imaplib
    > m = imap.IMAP4(<myserver ip or host>)
    > m.login(username,password)
    > m.select('myfolder')
    > status, data = m.search(None,'(SUBJECT "BIKES")')
    > assert status=='OK', "Error. Message: %s"%data
    > data = data[0] #you get your results in a list and search returns only
    > one result
    > assert data,"No results"
    > #cool, we have results, but IMAP's search command only returns IDs so
    > we have to fetch
    > #msgs now
    > status,senders = m.fetch(data.replace('
    > ',','),'(BODY.PEEK[HEADER.FIELDS (FROM)])')
    > assert status=='OK', "Error. Message: %s"%data
    >
    > Now you just have to parse the "senders" data. There are many examples
    > about sending emails with python, like this one:
    >
    > def send_notice():
    > import smtplib
    > msg = 'we got your mail, indeed'
    > from email.MIMEText import MIMEText
    > mail = MIMEText(msg, 'plain', 'utf-8')
    > mail['From'] =fro=''
    > mail['Subject'] = "Spam machine"
    > mail['To'] = to = ''
    > server = smtplib.SMTP('localhost')
    > errors = server.sendmail(fro, to, mail.as_string())
    > server.quit()
    >
    > That other program should be very simple to make now.
    >
    > Sebastjan
    >
    > On 3/24/06, Kun <> wrote:
    >> Hey guys, I would like to have a code in python (as simple as possible)
    >> to scan a specific folder in my mailbox and if the subject is equal to,
    >> say, 'BIKES', I would like to have the code automatically send the
    >> SENDER an email saying something like "We have received your Email".
    >> Furthermore, I would also like to somehow save the sender's email into a
    >> list which would be compiled by another python program into an html file
    >> that would show a list of email addresses whose subject matched 'BIKE'
    >>
    >> I know i am asking for a lot but since i am new to python, can someone
    >> help me out with this? Whether its tips or code, i'll be very excited to
    >> hear your answer. Thanks.
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>


    Thank you very much for your help. I am trying to use your code and
    currently it works up to the 'fetch', where I am getting the following
    error:

    error: FETCH command error: BAD ['Protocol Error: "Specified message set
    is invalid".']

    I guess I do not understand why you have data.replace('',',') and what
    ",',' means.

    Thanks so much.

    Kun
     
    Kun, Mar 24, 2006
    #4
  5. On 3/24/06, Sebastjan Trepca <> wrote:

    > m.select('myfolder')


    Some attention is required here to retrieve subfolders.
    Some imap servers like Cyrus and Courier uses "INBOX.subfolder" to
    access subfolders.
    --
    Marco Carvalho (macs) | marcoacarvalho(a)gmail.com
    http://arrakis.no-ip.info | http://cdd.debian-br.org
    Maceio - Alagoas - Brazil
    Debian GNU/Linux unstable (Sid)
    GNU-PG ID:08D82127 - Linux Registered User #141545
    Notícias Semanais do Debian em Português: http://www.debian.org/News/weekly
    Alertas de Segurança Debian (DSA): http://www.debian.org/security
     
    Marco Carvalho, Mar 24, 2006
    #5
  6. Kun

    Kun Guest

    Marco Carvalho wrote:
    > On 3/24/06, Sebastjan Trepca <> wrote:
    >
    >> m.select('myfolder')

    >
    > Some attention is required here to retrieve subfolders.
    > Some imap servers like Cyrus and Courier uses "INBOX.subfolder" to
    > access subfolders.
    > --
    > Marco Carvalho (macs) | marcoacarvalho(a)gmail.com
    > http://arrakis.no-ip.info | http://cdd.debian-br.org
    > Maceio - Alagoas - Brazil
    > Debian GNU/Linux unstable (Sid)
    > GNU-PG ID:08D82127 - Linux Registered User #141545
    > Notícias Semanais do Debian em Português: http://www.debian.org/News/weekly
    > Alertas de Segurança Debian (DSA): http://www.debian.org/security



    so i have used the following code and have successfully saved a list of
    senders as a string. however, the string has much more information than
    just the email address and i am wondering what is the best way to parse
    the email address out of the entire string.

    sample string:
    >>> print status, senders

    OK [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
    <>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
    'From: Kun <>\r\n\r\n'), ')']

    how do i just get the two email addresses out of there?

    my code is:

    from imaplib import *
    import getpass
    m = IMAP4("xxxxxxxx")
    m.login('xxxxxx', 'xxxxxxx')
    m.select('Inbox')
    status, data = m.search(None,'(SUBJECT "BIKES")')
    assert status=='OK', "Error. Message: %s"%data
    data = data[0] #you get your results in a list and search returns only
    one result
    assert data,"No results"
    #cool, we have results, but IMAP's search command only returns IDs so we
    have to fetch
    #msgs now
    status,senders = m.fetch(data.replace(' ',','),'(BODY.PEEK[HEADER.FIELDS
    (FROM)])')
    assert status=='OK', "Error. Message: %s"%data
    print senders
     
    Kun, Mar 25, 2006
    #6
    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. Henrik Ormåsen

    Curier-IMAP and imap.create()

    Henrik Ormåsen, Aug 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    178
    Henrik Ormåsen
    Aug 19, 2006
  2. Jon Fi
    Replies:
    4
    Views:
    544
    Kashia Buch
    Oct 21, 2006
  3. jasonnaylor
    Replies:
    1
    Views:
    256
    jasonnaylor
    Apr 16, 2008
  4. Adam Akhtar
    Replies:
    1
    Views:
    179
    Eric Hodel
    Dec 15, 2008
  5. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    4
    Views:
    197
    Eric Hodel
    Jul 4, 2009
Loading...

Share This Page