Learn how to configure SMTP in Linux machines. Know configuration file locations, parameters in config files, and testing commands.
Simple Mail Transfer Protocol. SMTP is one of the important services in any production system. Nowadays, mail notifications became important to monitor systems in real-time. Hence, it becomes essential to know how to configure mail utilities on servers. To enable any mail program on the system, SMTP is a basic component. It’s a very easy and quick procedure to setup SMTP on servers.
- Hostname and IP address of SMTP server should be known (e.g. we will use mailserver.xyz.com 10.10.2.5)
- The client should be able to reach SMTP server over a network
Its 5 steps procedure to setup SMTP client on Linux.
Add SMTP server entry in /etc/hosts file using vi editor or concatenate using echo command below.
# echo "10.10.2.5 mailserver.xyz.com" >> /etc/hosts # cat /etc/hosts |grep -i mail 10.10.2.5 mailserver.xyz.com
We need to edit the macro configuration file
/etc/mail/sendmail.mc. Search and edit below parameters in this file :
define(`SMART_HOST',`mailserver.xyz.com'): Smart relay server name
define(`confDOMAIN_NAME',`xyz.com')dnl: Valid domain name
FEATURE(always_add_domain)dnl: always masquerades email address
FEATURE(`allmasquerade')dnl: rewrite both from/to relative to the local machine.
MASQUERADE_DOMAIN(`xyz.com.')dnl: Domain for masquerading emails
MASQUERADE_AS(`xyz.com')dnl: All mails masquerades as came from the defined domain
Once the above changes are done, you need to compile the
mc file so that all these changes of the macro (
mc) file will be transferred to the config (
cf) file using
# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
Edit below parameters in
/etc/mail/submit.cf so that mails will be submitted to defined domain.
Djxyz.com: Define this in case of sendmail won’t able to determine your domain.
DSmailserver.xyz.com: Smart relay host
Finally, restart mail services to take up this new configurations :
# service sendmail restart # service postfix restart
You are done! Send a test email with the below command and check if you receive it.
# echo test |sendmail -v email@example.com [<-] 220 mailserver.xyz.com ESMTP Postfix [->] HELO testsrv2 [<-] 250 mailserver.xyz.com [->] MAIL FROM:<firstname.lastname@example.org> [<-] 250 2.1.0 Ok [->] RCPT TO:<email@example.com> [<-] 250 2.1.5 Ok [->] DATA [<-] 354 End data with <CR><LF>.<CR><LF> [->] Received: by testsrv2 (sSMTP sendmail emulation); Fri, 23 Dec 2016 02:29:07 +0800 [->] From: "root" <firstname.lastname@example.org> [->] Date: Fri, 23 Dec 2016 02:29:07 +0800 [->] test [->] [->] . [<-] 250 2.0.0 Ok: queued as 19F75822B8 [->] QUIT [<-] 221 2.0.0 Bye
If you don’t receive an email, check logs under
/var/log/maillog for troubleshooting.