如何在python procmail管道脚本中获取UIDNEXT?

2024-03-29 10:48:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,用于将原始电子邮件插入db。不要问我为什么要将原始邮件插入数据库。在

import sys
from DB import *
import email

full_msg = sys.stdin.readlines()
j =  ''.join(full_msg)
msg = email.message_from_string(j)

sql = '''INSERT INTO Email(Id, Message) VALUES (NULL, %s)'''
db = DB()
db.query(sql, (msg, ))

如果我能得到那个消息的uid就太好了,所以如果我从数据库中删除消息,我也可以删除imap服务器上的消息my uid。在

我不想登录到imap服务器然后按uid删除邮件,因为我不知道用户密码,因为它是加密的。在

例如,我想获取msg['Message-Id'],然后在用户maildir的grep文件中获取该消息Id并删除实际的文件,但这听起来完全不对。在

我知道在python中,imaplib中有类似UIDNEXT的东西,但这是在假设我已经登录的情况下,而我没有登录。在

更新:

有了这个,我可以获取下一个uid,但我必须登录。如何在不登录的情况下获取UIDNEXT? 顺便说一下,我在mysql中使用了postfix/dovecot。在

^{pr2}$

新的更新

dovecot uidlist样本

16762 W105493 S104093 :1417408077.2609_1.zumance
16763 S18340 W18608 :1417429204.3464_1.zumance

dovecot uidlist uid的最后一行代码:

l = open("dovecot-uidlist").readlines()
print l[-1].split(" ")[0]

邮件管道的脚本已完成:

import sys
import email
import re
from DB import *
full_msg = sys.stdin.readlines()
j =  ''.join(full_msg)

msg = email.message_from_string(j)
match = re.search(r'[\w\.-]+@[\w\.-]+', msg['to'])

address = match.group(0)
address = address.split("@")

with open("/var/vmail/"+address[1]+"/"+address[0]+"/dovecot-uidlist", 'r') as f:
  first_line = f.readline()
  nextuid = first_line.split(" ")
  nextuid = re.findall(r'\d+', nextuid[2])

sql = '''INSERT INTO Email(Id, Message, Uid, Domain, Mbox) VALUES (NULL, %s, %s, %s, %s)'''
db = DB()
db.query(sql, (msg, nextuid[0], address[1], address[0],  ))

包含文件位于https://pregmatch.org/read/python-procmail的博客文章


Tags: fromimportid消息dbsqluidaddress
2条回答

Dovecot在文件dovecot-uidlist中维护UID和filename之间的映射。每行包含一个文件头,然后每行包含一个消息头。在

标题行如下所示:

1 1173189136 20221

第一位是版本,第二位是IMAP uidvality,最后一位是将要使用的下一个UID。在

之后,每条消息都有自己的行,如下所示:

^{pr2}$

第一个单词是UID,下一个是文件名。在

the dovecot wiki有更多信息。在

由于解析dovecot uidlist将无法工作,因为列表将不会更新,直到您与您的电子邮件客户端检查电子邮件,我决定使用其他解决方案。这个解决方案就是dovecot预授权机制。在我的python procmail管道脚本中,我决定这样做:

import subprocess
p = subprocess.Popen( "/usr/libexec/dovecot/imap -u "+user, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write("b select INBOX\n")
p.stdin.write("e logout\n")
(stdoutdata, stderrdata) = p.communicate()
print stdoutdata

print stderrdata

stdoutdata给出的输出如下所示:

^{pr2}$

现在我要做的就是解析输出的这一部分:

^{3}$

这个预授权解决了我的问题。解析和我的解决方案将在今天稍后的博客中完成。在

解决方案:

import subprocess
pSub = subprocess.Popen( "/usr/libexec/dovecot/imap -u "+username+"@"+domain_parsed, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
pSub.stdin.write("b select INBOX\n")
pSub.stdin.write("e logout\n")
(stdoutdata, stderrdata) = pSub.communicate()
dovecotStream = open("/var/www/domain.com/scripts/server/dovecot","w")
dovecotStream.write(stdoutdata)
dovecotStream.close()


nextuidNo = []
with open("/var/www/domain.com/scripts/server/dovecot") as dovecotFile:
    dovecotFilelines = dovecotFile.read()
for dovecotFileline in dovecotFilelines.split('\n'):
    matchCheck = re.findall(r'\[UIDNEXT.+\]', dovecotFileline)
        if len(matchCheck):
            nextuidNo = re.findall(r'\d+', matchCheck[0])

print nextuidNo #this is list

完成博文:https://pregmatch.org/read/dovecot-preauth-python

相关问题 更多 >