Disabling relay for localhost SMTP server (Sendmail) on Linux

Why would one want to do that?

Well, I am in a situation where I need to load test a server and one of the tasks accomplished by that server is to send out emails. I want the test to be as close to a production scenario without actually sending out the emails. Many options came to my mind but none of them would have provided what I was looking for:
– turning off the SMTP server: the application would have to wait for the connection to timeout which skews our tests
– using random invalid email addresses as destination: that would result in a bunch of bounce backs which is not, again, what is happening in production

The best way was to have my application point at the localhost SMTP server (Sendmail) and disable relaying to the emails never actually get sent to their destination. The description below assumes that you are using the default configuration of Sendmail which manages relaying permissions using the access file.

To accomplish what I wanted, all I had to do was edit the access file located in /etc/mail/
In that file, for each source, there are 4 possible values: OK, RELAY, REJECT and DISCARD. OK relays even if other rules are not satisfied, RELAY enables relaying for this source and REJECT rejects the emails and sends a message back. The one we are interested in is DISCARD which just silently discards the message without generating an error message. It almost turns out SMTP server into a mock server.

So, the 3 lines we had to modify were:
Connect:localhost        DISCARD
Connect:127                  DISCARD
Connect:[IPv6:::1]          DISCARD

This will cover all local requests using IPv4 and IPv6.

Final step is to restart sendmail for those changes to take effect: sudo /etc/init.d/sendmail restart

Note: you will need sudo to edit the access file.