davidw88
asked on
Python: how to check mail status
Hi experts,
I am going to write a Python code to upload new e-mails from a specific user account into MySQL database. This Python code is scheduled to run every miniute.
This user account is generic so nobody will come to check e-mails except the running code. Whenever the code runs, it should only upload NEW e-mails into database. In other words, the code can not upload a e-mail content if this e-mail is in the database already. Therefore I need to know how to distinguish if a e-mail has been "read" by the code or not.
Comparing "From", "To" and "Body" of a e-mail with corresponding parts in the database is certainly a way to find if that e-mail has been loaded or not, however this method is too clumsy to use. Is there any smart way to find this?
Thanks so much.
I am going to write a Python code to upload new e-mails from a specific user account into MySQL database. This Python code is scheduled to run every miniute.
This user account is generic so nobody will come to check e-mails except the running code. Whenever the code runs, it should only upload NEW e-mails into database. In other words, the code can not upload a e-mail content if this e-mail is in the database already. Therefore I need to know how to distinguish if a e-mail has been "read" by the code or not.
Comparing "From", "To" and "Body" of a e-mail with corresponding parts in the database is certainly a way to find if that e-mail has been loaded or not, however this method is too clumsy to use. Is there any smart way to find this?
Thanks so much.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I see. Thanks ilalopoulos for your two replies.
I will follow your idea to test and let you know how it works later.
thanks again.
I will follow your idea to test and let you know how it works later.
thanks again.
So:
1. Decide what do you mean by unique, emails headers give you at minimum the from, to, subject, date fields so some of the combinations you can use to define uniqueness are the following:
to, from subject, date
to, from, subject, date, and part of the body
date and a hash of to,from,body
etc.
2. You will define the above as primary keys in the table but take care that depending on the MySQL engine that you will use, keys have a limit in size, so maybe the most effective way to use is the 3d option (a field and a hash of the rest)
In this example I will use subject and date which will be defined as the primary key for the table:
CREATE TABLE `emails` (
`to` varchar(255) NOT NULL,
`from` varchar(255) NOT NULL,
`subject` varchar(255) NOT NULL,
`date` datetime NOT NULL,
`body` text NOT NULL,
PRIMARY KEY (`subject`,`date`)
);
3. The test for duplicates is done by the database engine, I will give you two approaches:
a) Use the insert query with ON DUPLICATE KEY and a dummy update part - in case that the entry exist the database will not complain nor do anything else - this is the silent approach.
"""INSERT INTO emails (e_to, e_from, e_subject, e_date, e_body) VALUES ('%s', '%s', '%s', '%s', '%s')""" % (e_to, e_from, e_subject, e_date, e_body)
more info for ON DUPLICATE KEY at: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
b) Use a try/except clause to catch duplicate key errors from MySQL:
Open in new window