Top Menu

Thursday, February 5, 2015

How to configure sendmail with SMTP relay to your ISP

This is a very quick tutorial to show you how to configure sendmail (the default MTA for RHEL/CentOS) to relay mails to your ISP SMTP server (smarthost), allowing email out only. Expect a few technical terms in this document.

Why I need to do this?

To reduce the number of Spam mail on the Internet, many ISPs have blocked the outgoing SMTP port (25) access. Since all the SMTP servers in the cloud are communicating through port 25 only, you cannot send emails to any public domain when the port is blocked. But, there is always an alternative.

Install sendmail configuration tools

yum install sendmail-cf

Edit /etc/mail/sendmail.mc

#declare the ISP SMTP server
define('SMART_HOST', 'your-smtp-server')
FEATURE(authinfo)dnl

#listen to localhost only
dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

#masquerade the from address
FEATURE(masquerade_envelope)dnl
FEATURE(genericstable)dnl
GENERICS_DOMAIN(`localhost.localdomain')dnl

Apply the config

make -C /etc/mail

Input authentication information

Edit /etc/mail/authinfo

AuthInfo: "U:" "P:" "M:PLAIN"
#*my ISP requires auth info to be transferred in plain text

Apply the config
makemap hash /etc/mail/authinfo < /etc/mail/authinfo

Masquerade the from address

Edit /etc/mail/genericstable

root admin@example.com
userA userA@example.com

Apply the config
makemap hash /etc/mail/genericstable < /etc/mail/genericstable

Allow relaying from these addresses
Edit /etc/mail/access

Connect:localhost.localdomain RELAY
Connect:localhost RELAY
Connect:127.0.0.1 RELAY
Connect:192.168 RELAY

Apply the config

makemap hash /etc/mail/access < /etc/mail/access

Restart the service

service sendmail restart
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]

Test
Locally:

echo "This is a testing email for smarthost" | mail -s "Testing email from me" root

To read it, su as root and issue the command mail

To a public address:

echo "I send this one with the Linux command" | mail -s "Testing email from Sourav" userA@example.com

Troubleshoot
If you encounter any errors while testing, refer to /var/log/maillog  for details.