473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

imap folder scanner

Kun
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.
Mar 24 '06 #1
5 4504
Kun
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.

Mar 24 '06 #2
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'] =f********@example.com'
mail['Subject'] = "Spam machine"
mail['To'] = to = 't*@example.com'
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 <ne*******@gmail.com> 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

Mar 24 '06 #3
Kun
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'] =f********@example.com'
mail['Subject'] = "Spam machine"
mail['To'] = to = 't*@example.com'
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 <ne*******@gmail.com> 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
Mar 24 '06 #4
On 3/24/06, Sebastjan Trepca <tr****@gmail.com> 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
Mar 24 '06 #5
Kun
Marco Carvalho wrote:
On 3/24/06, Sebastjan Trepca <tr****@gmail.com> 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
<an**@anon.com>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
'From: Kun <ne*******@gmail.com>\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
Mar 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: F. GEIGER | last post by:
Hi all! As I saw Alex Martelli's post about a mbox cleaner based on POP3, I thought, it could be possible to do that based on IMAP too. That way I could ask the server for mails having attached...
2
by: nh_capricorn | last post by:
Hi, I am fairly new to .NET (coming from a Java background) and I am trying to port an application that I originally wrote in Java, to .NET / C#. My problem is that I cannot find a C# analog for...
0
by: Kevin F | last post by:
I'm trying to use the following code to get my remote server's folder size information. Unfortunately, i'm getting the error: Traceback (most recent call last): File...
1
by: Kevin F | last post by:
I'm trying to use the following code to get my remote server's folder size information. Unfortunately, i'm getting the error: Traceback (most recent call last): File...
2
by: J Huntley Palmer | last post by:
I am having a horrific time integrating uw-imap's c-client for imap support in php. The problem is a whole bunch of "Text relocation remains referenced against symbol" errors during linking....
2
by: pmarg212 | last post by:
Greetings, I currently have incoming email piped to a php script using a .forward file. I would like to be able to parse the incoming mail, perform operations, and then fire back an email...
1
by: CodeSeeker | last post by:
I have an application, which uses pop3 to read the messages from the mailbox, and it has been working fine for so many year. We recently have started changing this application to use java mail IMAP 4...
0
by: Hemant | last post by:
Hi, I am working on asp.net c# mailing application, which deals IMAP server. Its a simple mailing application where user can check mail & send new mail. I am able to read all the mail from...
3
by: kpfunf | last post by:
I have the following code that works in accessing my personal folder inbox and subfolders. Option Explicit Sub SaveAttachmentsToFolder() On Error GoTo SaveAttachmentsToFolder_err ' Declare...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.