I've configured postfix on the email server with .forward file which saves a copy of email and invokes a python script. These emails are stored in Maildir format.
I want to use this python script to send a reply to the sender acknowledging that the email has been received. I was wondering if there is any way I can open/access that e-mail, get the header info and sender address and send email back.
I looked at several examples of Maildir functions of python, but they mostly add/delete e-mails. How can I open the latest e-mail received in Maildir/new and get the required information?
Thanks in advance. Apologies for the dumb question, but I am new to Python.
md = mailbox.Maildir('/home/abcd/Maildir')
message = md.iterkeys().next()
#for msg in md:
# subject = msg.get('Subject',"")
# print subject
sender = message.get('From',"")
When I execute this, I do get the sender name.. but It is rather the oldest email arrived in Maildir/new folder not the latest one.
Also, if I use get_date function, what if two (or more) e-mails arrive on the same day?
Some hints for this:
So much as beginners help: A little bit you should also do by yourself.
You should make yourself familiar with the Python documentation - I agree, that it is not easy to find the right packages and how to use them, but you can try them directly in the Python shell.
Ok, here another code snippet:
newest = None for message in md.itervalues(): if newest == None or message.get_date() > newest.get_date(): newest = message # now newest should contain the newest message
Did not see your last question: get_date does not only contain the date, but also the time, because it gives the number of seconds since (normally) 1970.