0

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.

EDIT:

md =  mailbox.Maildir('/home/abcd/Maildir')
message = md.iterkeys().next()
#print message
#for msg in md:
#    subject = msg.get('Subject',"")
#    print subject
print message
sender = message.get('From',"")
print sender

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?

4

1 回答 1

0

对此的一些提示:

  • 您可以使用 maildir.Maildir 类打开 Maildir(请参阅邮箱文档)
  • 您可以通过 itervalues 方法遍历 Maildir 中的所有邮件
  • 现在你得到了 Maildir 中的所有邮件。其中之一是最新的。
  • 邮件是 MaildirMessage 类的对象,MaildirMessage 是 Message 的子类。对于这些类,还存在一个文档(当前与邮箱在同一页面上)
  • 使用这些对象上的“get_date”方法,您可以找出哪一个是最新的。您仍然需要自己选择它。

初学者的帮助:一点点你也应该自己做。

你应该让自己熟悉 Python 文档——我同意,找到正确的包以及如何使用它们并不容易,但你可以直接在 Python shell 中尝试它们。

好的,这是另一个代码片段:

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

没有看到您的最后一个问题:get_date 不仅包含日期,还包含时间,因为它给出了自(通常)1970 年以来的秒数。

于 2015-02-19T05:00:01.687 回答