Configuring Sendmail to send mail from command line

It can be very frustrating to try to send mail through the command line using sendmail. Although sendmail is preinstalled on most linux system, it is configured not to deliver any mail to the outside (probably to prevent spamming).

Configuring it is very simple. First you'll need the sendmail-cf package. Install it using yum:

[root@server ~]# yum install sendmail-cf

Edit the file /etc/mail/sendmail.mc and add the following lines. Make sure you set your mail server domain name where it's bolded:

MASQUERADE_AS(yourdomain.com)dnl
MASQUERADE_DOMAIN(yourdomain.com)dnl

In the same file /etc/mail/sendmail.mc remove the "dnl" from the beginning of the lines so it will look like this:

LOCAL_DOMAIN(`localhost.localdomain')dnl
FEATURE(masquerade_envelope)dnl
FEATURE(masquerade_entire_domain)dnl

Save the file and compile it using m4:

[root@server ~]# m4 /etc/mail/sendmail.mc > /etc/sendmail.cf

Send Sendmail a -HUP signal using kill or simply restart the daemon for the configuration changes to take effect:

[root@server ~]# service sendmail restart


Testing your configuration using sendmail

And that's it! you're done. Just send yourself a test email to make sure it is really working:

[root@server ~]# /usr/sbin/sendmail -t < mail.txt

Where the contents of the mail.txt file are:

Date: Thu Nov 11 08:41:54 2007
To: you@somewhere.com
Subject: The subject of the message
From: whatever@somewhere.com

Body of message goes here


Testing your configuration using mutt

You can also use mutt to test, which is a bit simpler (and you can also add the -a parameter for file attachment):

[root@server ~]# mutt -s "Test Email" you@somewhere.com < /dev/null


If this page has helped you and you would like to contribute to this web site, please donate. Small amounts like $5 are helpful and will be gratefully accepted. Thank you!

Comments

Anonymous:

Simple and effective! Thanks for this...

adambengur:

If you get the following error:
553 5.3.5 localhost.localdomain. config error: mail loops back to me (MX problem?)
Add the following line to the /etc/mail/sendmail.mc, compile with m4 and restart sendmail:
MODIFY_MAILER_FLAGS(`relay',`+k')

Anonymous:

Thanks, just what I was looking forward.

Had a brain freeze when for testing sendmail - I have been using postfix for so long. :)

Some mean people put this blog under a robot that submits spam comments, sometimes more than 100 spam posts a day. I am forced to disable commenting to this blog... Sorry guys...