使用 sendmail,如何将所有外发邮件发送到 /dev/null 或只是阻止电子邮件排队或发送?
在开发 nagios 框上,我想阻止发送邮件,这样通知就不会发出。停止出站邮件将允许我按原样测试 nagios 配置并防止虚假通知。
答案1
我在开发机上完全禁用了 sendmail,然后使用一个简单的 perl 脚本监听 SMTP 端口并将电子邮件转储到目录中,从而实现了这一点。我确信使用 sendmail 配置可以做到这一点,但使用 perl 脚本要容易得多。下面是精简到最基本的部分:
#!/usr/bin/perl -w
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
$server = new Net::SMTP::Server || die("$!\n");
while($conn = $server->accept()) {
my $client = new Net::SMTP::Server::Client($conn) ||
die("Unable to handle client connection: $!\n");
$client->process || next;
# Here's where you can write it out or just dump it. Set $filename to
# where you want to write it
open(MAIL,"> $filename") || die "$filename: $1";
print(MAIL "$client->{MSG}\n");
close(MAIL);
}
答案2
下面将所有内容发送到/dev/null:
LOCAL_RULE_0
R$* < @ $* > $* $#local $: bit-bucket
假设您的 /etc/aliases 中:
bit-bucket: /dev/null
答案3
在运行 Joomla 的开发虚拟机上,我将其配置为将电子邮件发送到/some/path/sendmail.bash
而不是 Sendmail 默认路径/usr/sbin/sendmail
。这是一种将邮件记录到文件中以供以后使用 bash/awk 脚本进行调试(包括解码 base64 部分)的简单方法,同时还可以阻止 CMS 发送任何电子邮件。
/some/path/sendmail.bash
:
#!/bin/bash
read -d '' awkScript << 'EOF'
BEGIN {
p=1 #print
multipart=0
boundary=0
part=0
text=0
encoded=0
debug=0
}
!multipart && tolower($0) ~ /^content-type:[ \t]*multipart/ {
#works!multipart && /^Content-Type: multipart/ {
multipart=$2
if (debug) { print "Multipart Email: " multipart }
}
multipart && !boundary && /boundary=/ {
split($1,s,"=")
boundary = s[2]
gsub(/"/, "", boundary) #remove the surrounding quote
if (debug) { print "With boundary: " boundary }
}
#start of new part or end of all parts
multipart && ($0 ~ ("^--" boundary "$") || $0 ~ ("^--" boundary "--") ) {
part=1
text=0
encoded=0
p=1
}
part && !text && tolower($0) ~ /^content-type: text/ {
text=$2
if (debug) { print "Text: " text }
}
part && encoded {
#https://stackoverflow.com/questions/66228621/base64-decode-command-linux-cli-command
if (/^\s*$/) { #skip decoding blank lines
print $0
} else {
system("echo '" $0 "' | base64 -d")
}
}
p #if p then print current line
part && text && !encoded && tolower($0) ~ /^content-transfer-encoding: base64/ {
encoded=$2
if (debug) { print "Encoded: " encoded }
p=0 #make sure this line prints so test after it has printed
}
END {
#print "END"
}
EOF
#echo $awkScript
awk "$awkScript" >> /tmp/sendmail.log
#awk "$awkScript"
答案4
尝试一下smtp-sink
Linux 上可用的 。例如:
$ smtp-sink -u postfix -c nynode.com:25 1000