2013年3月

postfix自动回复配置

增加hook

修改/etc/postfix/master.cf

myhook unix - n n - - pipe
  flags=F user=nobody argv=/usr/bin/python /path/to/script.py ${sender} ${size} ${recipient}
# 第二行前面一定要有空格

修改/etc/postfix/transport

example.com myhook:
# 最后是冒号

执行命令postmap /etc/postfix/transport

修改/etc/postfix/main.cf

transport_maps = hash:/etc/postfix/transport

自动回复脚本

这里我用的是python

#!/usr/bin/env python

import sys, smtplib, re

content = sys.stdin.read()
# 如果postfix设置了virtual,${sender}将不是我们所需要的,所以这里从mail的内容中获取
froms = re.findall('^From:\s+(.*?)\n', content, re.I | re.M)
tos = re.findall('^To:\s+(.*?)\n', content, re.I | re.M)
subjects = re.findall('^Subject:\s+(.*?)\n', content, re.I | re.M)

msg = 'From: %s\r\nTo: %s\r\nReply-To:%s\r\n\r\n' % (tos[0], froms[0], froms[0])

msg += 'this is a test reply.'

mail = smtplib.SMTP('localhost')
mail.sendmail(tos[0], froms[0], msg)
mail.quit()