localhost as a smart SMTP relay

You have a production server which is not configured to relay mails externally, but there is a application running in that server which mails its log to the given email address regarding its status and incase of any service failures. since the sendmail runs locally on localhost.localdomain name it tries to forward the mail with root@localhost.localdomain to the remote smtp server, which would get rejected because of the reason that localhost.localdomain resolves to Invalid IP. So in order to overcome that we have to rewrite the from address of root@localhost.localdomain to user@example.com. so that the other smtp server relays the mail from user@example.com to the admin@example.com or whatever address you want to send the service status mail to.

Step-1

Check for installed sendmail packages –

apt-get install sendmail-base sendmail-bin sendmail-cf sendmail

Step-2

First add the remote server ip address and its domain name to the /etc/hosts

vi /etc/hosts

10.1.1.10 smtp.example.com

You don’t have to do the above step if your local dns can resolve the domain name to the ip address of the smtp server which is configured to relay mail.

Step-3

Edit the Sendmail configuration file for forwarding mails

vi /etc/mail/sendmail.mc

LOCAL_CONFIG
CPexample.com
EXPOSED_USER(root uucp)dnl # users exempt from masquerading
LOCAL_USER(root)dnl
dnl # Masquerading options
MASQUERADE_AS(`example.com’)dnl
FEATURE(`allmasquerade’)dnl
FEATURE(`masquerade_envelope’)dnl
FEATURE(`nullclient’, smtpservername)dnl

LOCAL_CONFIG

Step-4

run the ‘make’ command on /etc/mail directory then reload the sendmail server
/etc/init.d/sendmail reload

Step-5
Finally, test your settings –
telnet localhost 25
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
220 clinet1.example.com ESMTP Sendmail 8.14.3/8.14.3/Debian-5; Tue, 2 Feb 2010 13:41:34 +0600; (No UCE/UBE) logging access from: localhost(OK)-localhost [127.0.0.1]
helo smtp
250 client1.example.com Hello localhost [127.0.0.1], pleased to meet you
mail from:root@client1.example.com
250 2.1.0 root@client1.example.com… Sender ok
rcpt to:tarique@yahoo.com
250 2.1.5 tarique@yahoo.com… Recipient ok
data
354 Enter mail, end with “.” on a line by itself
hello from example1
.
250 2.0.0 o127fY1s008960 Message accepted for delivery
[type quit to exit].

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: