对所有传入邮件执行bash命令(Postfix)

时间:2016-04-13 08:29:17

标签: bash postfix-mta

我想在每个传入的postfix邮件的主体上执行命令。

sed ':a;N;$!ba;s/=\n//g' /path-to/message-file | sed 's/</\n\</g'  | sed -r '/'"$(sed -r 's/\\/\\\\/g;s/\//\\\//g;s/\^/\\^/g;s/\[/\\[/g;s/'\''/'\'"\\\\"\'\''/g;s/\]/\\]/g;s/\*/\\*/g;s/\$/\\$/g;s/\./\\./g' whitelist | paste -s -d '|')"'/! s/http/httx/g'

我认为Postfix After-Queue Content Filter可以实现,但我不知道如何做到这一点......

编辑:

afterqueue.sh

#!/bin/sh
# Simple shell-based filter. It is meant to be invoked as follows:
#       /path/to/script -f sender recipients...

# Localize these. The -G option does nothing before Postfix 2.3.
INSPECT_DIR=/var/spool/filter
SENDMAIL="/usr/sbin/sendmail -G -i" # NEVER NEVER NEVER use "-t" here.

# Exit codes from <sysexits.h>
EX_TEMPFAIL=75
EX_UNAVAILABLE=69

# Clean up when done or when aborting.
trap "rm -f in.$$" 0 1 2 3 15

# Start processing.
cd $INSPECT_DIR || {
    echo $INSPECT_DIR does not exist; exit $EX_TEMPFAIL; }

cat >in.$$ || { 
   echo Cannot save mail to file; exit $EX_TEMPFAIL; }

# Specify your content filter here.
sh /path/to/remove_links.sh <in.$$

$SENDMAIL "$@" <in.$$

exit $?

remove_links.sh

#!/bin/bash

sed ':a;N;$!ba;s/=\n//g' $1 | sed 's/</\n\</g'  | sed -r '/'"$(sed -r 's/\\/\\\\/g;s/\//\\\//g;s/\^/\\^/g;s/\[/\\[/g;s/'\''/'\'"\\\\"\'\''/g;s/\]/\\]/g;s/\*/\\*/g;s/\$/\\$/g;s/\./\\./g' /path/to/whitelist | paste -s -d '|')"'/! s/http/httx/g'

如果我手动调用它,它正在工作,但是如果我将它添加到/etc/postfix/master.cf中这样:

  # =============================================================
  # service type  private unpriv  chroot  wakeup  maxproc command
  #               (yes)   (yes)   (yes)   (never) (100)
  # =============================================================
  filter    unix  -       n       n       -       10      pipe
    flags=Rq user=filter null_sender=
    argv=/path/to/afterqueue.sh -f ${sender} -- ${recipient}

邮件没有变化。 我得到以下syslog:

Apr 13 15:14:08 rs211184 postfix/qmgr[7492]: 3FFDF23CB5F: from=<test@gmail.com>, size=4358, nrcpt=1 (queue active)
Apr 13 15:14:08 rs211184 postfix/pipe[7504]: 116E523CA8C: to=<example@example.de>, relay=filter, delay=0.2, delays=0.16/0/0/0.04, dsn=2.0.0, status=sent (delivered via filter service)
Apr 13 15:14:08 rs211184 postfix/qmgr[7492]: 116E523CA8C: removed
Apr 13 15:14:08 rs211184 postfix-local[7522]: postfix-local: from=test@gmail.com, to=example@example.de, dirname=/var/qmail/mailnames
Apr 13 15:14:08 rs211184 postfix/pipe[7521]: 3FFDF23CB5F: to=<dsehlhoff@lcdev1.de>, relay=plesk_virtual, delay=0.02, delays=0.01/0/0/0.01, dsn=2.0.0, status=sent (delivered via plesk_virtual service)
Apr 13 15:14:08 rs211184 postfix/qmgr[7492]: 3FFDF23CB5F: removed

1 个答案:

答案 0 :(得分:0)

您似乎期望文件中的消息,奇怪的是静态文件名,但这不是它的工作原理。消息到达标准输入。最低限度,只需删除/path/to/message-file - 但实际上,将sed加到sed通常是一个错误;你应该将它重构为一个sed脚本(或者awk,或Python,或者你有什么)。

sed -e ':a;N;$!ba;s/=\n//g' -e 's/</\n\</g' |
# This is too convoluted, really!
sed -r '/'"$(sed -r 's/\\/\\\\/g;s/\//\\\//g;s/\^/\\^/g;s/\[/\\[/g;s/'\''/'\'"\\\\"\'\''/g;s/\]/\\]/g;s/\*/\\*/g;s/\$/\\$/g;s/\./\\./g' whitelist |
paste -s -d '|')"'/! s/http/httx/g'