我经常使用 的mutt
别名功能来向更大的人群发送消息(合法的,而不是垃圾邮件!)。通常,我将别名放入该Bcc:
字段,以消除垃圾邮件收集器,并出于隐私原因(并非所有收件人都互相认识)。
有时,我无意中将别名放入To:
orCc:
字段中,在该字段中别名会展开,显示所有收件人的所有地址。这是非常糟糕的,因为一旦发送,信息就会泄露,并且无法返回。
如果该字段中有超过 1 个条目或 5 个条目,我该如何
mutt
拒绝发送邮件?To:
Cc:
mutt
改进:如果我添加用户定义的标头,例如,如何才能发送邮件X-limit-cc: 50
?
设置单独的邮件列表服务不是我所要求的,这宁愿是问题的单独解决方案。
答案1
AFAIK,Mutt 本身无法进行此类检查,但有一个解决方法!这个想法是重新定义$sendmail
变量以使用一个包装器,该包装器将在发送邮件之前检查邮件。例如,在您的.muttrc
文件中:
set sendmail=$HOME/scripts/sendmail-mutt
该脚本可以终止并出现错误(非零退出状态),也可以成功终止,如下所示:
exec /usr/sbin/sendmail -oem -oi "$@"
在 shell 脚本中,或者:
exec qw(/usr/sbin/sendmail -oem -oi), @ARGV;
在 Perl 中。
答案2
#!/bin/bash
set -u -e -C;
shopt -s nullglob;
# This script can be used as a `sendmail` replacement in `mutt`. It
# performs several checks on the mail before sending ist, and it's
# easy to modify these checks, or to add more. Currently, these are:
#
# attachment — fail on non-multipart message if text contains
# keywords referring to an attachment (English, German).
#
# size — refuse to send excessively large mails.
#
# fixme — refuse to send mail containing the word `FIXME`.
#
# longline — refuse to send mails wit long lines, unless paths
# (URLs) containing two slashes, or quoted.
#
# limit-to, limit-cc — limit number of recipients in the according
# header. Better use Bcc field.
#
# Each of the tests can be disabled by puttig its name in an
# `X-checks` header field. E.g., `X-checks: longline limit-cc` allows
# to send mails with long lines to unlimited number of recipients in
# the Cc field.
#
# To use this script, install it where it can be found, and configure
# mutt accordingly. I have stored this script as `~/.mutt/sendmail`,
# and my `~/.mutt/muttrc` contains the line
#
# set sendmail = "~/.mutt/sendmail"
#
# When sending a mail, it is stored as `mutt-sendmail.*` in a
# temporary location, from where it is accessible for checks. On
# success, the mail is sent, otherwise this script fails. The
# temporary file is deleted.
# ---- Configuration of this script ----------------------------------
# Mail is sent using `${sendmail} "$@" <"${wholemail}"`, see last line
# of the script.
sendmail=/usr/bin/msmtp;
# You may explicitly skip some tests by putting their name in a header
# field of your mail. On checking, these names are printed to stderr.
# The name of the header is:
myhdr='X-checks';
# ---- The script ----------------------------------------------------
# print message to stderr, maybe fail
function err { echo "$@" >&2; exit 1; }
function warn { echo "$@" >&2; }
# store the mail in a temporary file
wholemail="$(mktemp -t mutt-sendmail.XXXXXXXXXX)" &&
trap "rm -f '${wholemail}'" EXIT &&
cat >| "${wholemail}" ||
err 'cannot create temporary file';
# get the header, with indented lines joined
header="$(sed -rn ':a;/^$/q;$!N;s/\n\s+/ /;ta;P;D' "${wholemail}")";
# get values of one particular header, each occurence on one line
function getHeader { sed -rn "s/^${1}:\s+//p" <<< "$header"; }
# use this function to read body of message
function getBody { sed '1,/^$/d' "${wholemail}"; }
# get list of checks to be ignored for this particular mail
checks="$(getHeader "${myhdr}")";
function requested {
if grep -q "${1}" <<< "${checks}"; then
warn "${myhdr}: ${1} — skipping";
return 1;
else
warn "no ${myhdr}: ${1}";
return 0;
fi;
}
# --- do all tests here ----------------------------------------------
#
# `$wholemail` contains the file name of the whole mail, as passed to
# sendmail.
# `getBody` writes the body of the mail to stdout.
# `getHeader foo` writes one line for each value of a `foo:` header.
# `requested bar` fails if a `$myhdr` header contains `bar`.
# make a keyword search on the whole email, if it is not multipart.
requested 'attachment' &&
getHeader "Content-Type" | grep -vq multipart &&
grep -v '^>' "$wholemail" | grep -Eni 'attach|anhang|hängt|unten' &&
err "no multipart message, but hints to attachment.";
# reject emails greater than $maxsize bytes
maxsize='32768';
requested 'size' &&
test "$(stat -c%s "${wholemail}")" -gt "${maxsize}" &&
err "bigger then ${maxsize} bytes.";
# reject emails containing FIXME
requested 'fixme' &&
getBody | grep -n FIXME &&
err "FIXME keyword found.";
# reject emails with long lines
maxline=78;
requested 'longline' &&
test "$(getBody | grep -Ev '^>|/.*/' | wc -L)" -gt "$maxline" &&
err "lines longer than $maxline chars found.";
# count number of recipients
requested 'limit-to' &&
test "$(getHeader "To" | grep -o ',' | wc -l)" -gt 2 &&
err 'Too many recipients in the To field.';
requested 'limit-cc' &&
test "$(getHeader "Cc" | grep -o ',' | wc -l)" -gt 5 &&
err 'Too many recipients in the Cc field.';
# --- now send the mail ----------------------------------------------
#err 'debugging mode: not sending';
${sendmail} "$@" <"${wholemail}";