Sendmail via Relay from Command Line on Linux

Published by Torry Crass on

Every now and then there arises a need to send e-mail from a command prompt.  In this case, the system is Red Hat Enterprise Linux and the desire is to have mailx send e-mail to a 3rd party, external system using a relay server.  To do this, you need to modify the /etc/mail/ file as follows.

At approximately line 26 modify the following appropriately and remove the "dnl" from the front of the line.

define(`SMART_HOST', `')dnl

At approximately line 160 you may also want to modify the masquerade settings and remove the "dnl" from the front of the line.



Once this file has been modified to the appropriate settings, you'll also need to rebuild your sendmail configuration which requires sendmail-cf so make sure that is installed, if not, use yum to install it with yum install sendmail-cf.  Once this is installed, change to the /etc/mail directory and run make such as below.

cd /etc/mail

Restart sendmail by issuing the following.

service sendmail restart

Now, you should then be able to use mail servers to execute this relaying.  A few options are below.


Here's how to use the mailx command

mailx -s "Testing The Mail" [email protected]
This is the body of the message, just enter text here and when you're complete follow it with a . on the following line like so.
Cc: [email protected]


Another method is to use the mutt mail program and modify the sending header to make the message more crafted to what you need, otherwise the message will be tagged as the account you are sending from on the console.

echo "This is the body of the message" | mutt -s "This is the subject" -e 'my_hdr From:Your Name <[email protected]>' -- [email protected]

With any luck, you'll start seeing messages appear in your inbox as you anticipate.  If not, check your /var/log/maillog file for errors and troubleshoot further.


Leave a Reply