私はしばしばmutt
エイリアス機能を使用してより多くの人にメッセージを送信します(スパムではなく正当です!)。通常、Bcc:
スパムコレクタを削除し、プライバシーのために(すべての受信者が互いに知っているわけではありません)、このフィールドにエイリアスを入れます。
時々、私は誤ってエイリアスをまたはTo:
フィールドCc:
に入力し、ここで展開され、すべての受信者のすべてのアドレスが表示されます。一度送れば情報が流出して元に戻せないので、これは非常に残念です。
そのフィールドに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 "$@"
シェルスクリプトからまたは:
exec qw(/usr/sbin/sendmail -oem -oi), @ARGV;
パールから。
答え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}";