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 service in any production system. Now a days, 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 system, SMTP is basic component. Its 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)
- Client should be able to reach SMTP server over 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 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 defined domain
Once above changes are done, you need to compile mc file so that all these changes of macro (mc) file will be transferred to config ( cf) file using m4 utility.
# 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 sendmail wont 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 test email with below command and check if you receive it.
# echo test |sendmail -v firstname.lastname@example.org [<-] 220 mailserver.xyz.com ESMTP Postfix [->] HELO testsrv2 [<-] 250 mailserver.xyz.com [->] MAIL FROM:<email@example.com> [<-] 250 2.1.0 Ok [->] RCPT TO:<firstname.lastname@example.org> [<-] 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" <email@example.com> [->] 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 dont receive email, check logs under /var/log/maillog for troubleshooting.