首页 > 解决方案 > 后缀 - 访问 python 脚本中的传入邮件信息

问题描述

postfix用于邮件服务器并procmail用于做一些事情,例如提取附件并为每条消息创建目录并将所有数据发送到另一台服务器。

这是我的后缀配置:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = imiMailServerHost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, XXX.YYYY.com, splnx, localhost.localdomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.100.0/24
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
home_mailbox = Maildir/

这是我的 .procmailrc 配置,我调用 bash 脚本来做一些事情:

VERBOSE=on
LOGFILE=/home/ali/procmail.log

:0
| /home/ali/Maildir/a.sh

这是 a.sh :

#!/usr/bin/bash
/usr/bin/ripmime -i - -d Maildir/ --prefix;
/usr/bin/python3 /home/ali/Maildir/a.py;

提取已成功完成,但我无法访问我的邮件信息,如主题或正文或...在我的 python 脚本中。

如何在 python 脚本中访问到达的邮件信息?

有没有更好的解决方案来为 postfix 中的每条回执消息创建文件夹?

标签: python-3.xprocmail

解决方案


Python 本身包含一个email库,它当然允许您从标题和正文中提取文本,其精度比您使用 Procmail 限制的纯正则表达式要高一些。(特别是 MIME 结构在 Procmail 中处理起来很棘手。)

如果您不希望这样,并且您希望每条消息都有一小部分静态标头,那么正则表达式可能就足够了。Procmail 允许您使用\/特殊标记提取正则表达式匹配的一部分。捕获的文本在特殊变量中可用MATCH

:0 # The whitespace between [...] is a space and a tab
* ^Subject:[    ]*\/.+
{ SUBJECT=$MATCH }

:0
| /home/ali/Maildir/a.sh -s "$SUBJECT"

您自己定义的 Procmail 变量将被导出,因此您实际上可以直接$SUBJECT从内部访问a.sh;这会产生不同的复杂性,因为您必须在测试脚本时显式设置此变量,但对于您的场景而言,它可能比实现选项解析更简单。

第三种选择是对脚本参数进行硬编码,使其$1成为主题、$2发送者等;但从可用性的角度来看,这太可怕了,所以我不想推荐。(如果你仍然这样做,你至少需要在脚本源中记录它,为了你自己的理智。)

事实上,我会将其重构为ripmime直接python从 Procmail 调用。

:0
* ^Subject:[    ]*\/.+
{ SUBJECT=$MATCH }

:0c
| ripmime -i - -d Maildir/ --prefix
:0
| python3 Maildir/a.py -s "$SUBJECT"

(顺便说一句,不要硬编码到ripmimeand的路径python3。相反,要确保你PATH是理智的。可能还要确保a.py有一个正确的 shebang 行并将其标记为可执行,然后取出python3.)

您当前的解决方案通过从 Procmail 调用的 shell 脚本调用 Python 不必要地使问题复杂化。如果 Python 不是实际问题的核心,也许类似于

:0
| b.sh

在哪里b.sh替换a.sha.py用类似的东西

#!/bin/sh
t=$(mktemp -t -d procmbXXXXXXXXX) || exit
# clean up when done or if interrupted
trap 'rm -rf "$t"' EXIT HUP INT QUIT TERM
cat >"$t"/00input.msg
ripmime -i "$t"/00input.msg -d "$t" --prefix
# as per your updated requirement in a comment
scp "$t" remotehost:messages/"$(basename "$t")"

如果您真正的问题实际上是“Procmail 将这条消息保存在哪里?”,则 Procmail 变量$LASTFOLDER包含文件名。


推荐阅读