12

With sendmail, how would you send all outgoing mail to /dev/null or just prevent email from being queued up or sent at all?

On a development nagios box I want prevent sending of mail so that notifications don't go out. Stopping outbound mail will allow me to test the nagios config as is and prevent spurious notifications.

1
  • Removed solaris tag, as the question, and the solution isn't OS specific at all. Dec 13, 2011 at 9:38

4 Answers 4

8

I did this on my development box by disabling sendmail completely and then having a simple perl script listen on the SMTP port and dump the emails into a directory. I'm sure it's possible to do with the sendmail configuration, but the perl script was much easier. Here's it stripped down to the essentials:

#!/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
  • This is an even better solution than I was thinking. Thanks.
    – cwebber
    Feb 23, 2011 at 21:51
  • +1 What an elegant solution, I was about to ask the same question.
    – Kev
    Mar 27, 2011 at 12:53
7

The following sends everything to /dev/null:

LOCAL_RULE_0
R$* < @ $* > $*       $#local $: bit-bucket

This assumes that in your /etc/aliases:

bit-bucket: /dev/null
3
  • 1
    Be careful not to just type this in -- there is a TAB between the two sides of the rule.
    – Erica Kane
    Apr 15, 2016 at 13:32
  • What email system are you using and where do you add that rule?
    – teknopaul
    Jan 10, 2017 at 16:53
  • Standard sendmail from sendmail.org. The rule gets added to sendmail.mc and then sendmail.cf is produced according to your OS / distribution's specifics.
    – adamo
    Jan 11, 2017 at 8:49
0

On a development vm running Joomla, I configured it to send emails to /some/path/sendmail.bash instead of the Sendmail default path /usr/sbin/sendmail. This was an easy way to log mails to a file for later debugging (including decoding base64 parts) using a bash/awk script while also preventing any emails being send by the 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"

0

Try smtp-sink, which is available on Linux. For example:

$ smtp-sink -u postfix -c nynode.com:25 1000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.