By default, the sendmail runs locally on localhost.localdomain and tries to send the mail with the [email protected] to the remote smtp server, which would get rejected as the localhost.localdomain will resolve to the Invalid IP.
You must configure SendMail as SmartHost to resolve this issue.
In this guide, we will explain how to configure a sendmail server to forward all mails generated from localhost to another SMTP server in order to send mail to remote recipients.
What is sendmail?
Sendmail is a MTA (Mail Transfer Agent), which is used to route email over the Internet to a specific recipient. Sendmail uses SMTP (Simple Mail Transfer Protocol) protocol for the email transmission, just like other mail servers.
Configuring Sendmail as a Smart Host
Follow the instructions below to set up the SMTP relay host for Sendmail.
find the word “SMART_HOST”, uncomment (remove ‘dnl’ from the beginning of the line) and edit the following line if your outgoing mail needs to be sent out through an external mail server:
define(SMART_HOST',smtp.your.provider’)dnl
Now, configure the SMTP relay host as follows, add your smtp server instead of ours ‘smtp-relay.gmail.com’.
vi /etc/mail/sendmail.mc define(SMART_HOST',smtp-relay.gmail.com’)dnl
Please note that any custom changes to sendmail.cf are lost when you regenerate this file, so take a backup of below files.
- sendmail.cf
- sendmail.mc
- submit.cf
- submit.mc
The ‘sendmail-cf’ package is required in order to regenerate the ‘/etc/mail/sendmail.cf’ file.
yum install sendmail-cf
The ‘sendmail.mc’ is the sendmail macro config file for m4. If you make changes to /etc/mail/sendmail.mc, you will need to regenerate the /etc/mail/sendmail.cf file.
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
You can double confirm if the new SMTP relay has been updated correctly by using the below command.
cat /etc/mail/sendmail.cf | grep gmail
DSsmtp-relay.gmail.com
Restart the sendmail service in order to take this changes effect. For RHEL 5 & 6 systems, run:
service sendmail restart
For RHEL 7 & 8 systems, run:
systemctl restart sendmail
When you’ve completed your configuration, send a test mail using the mail command to confirm that your outbound mail is flowing.
echo "This is a test mail for SMTP relay" | mail -v -s "2daygeek" [email protected] -r [email protected] [email protected],-r... Connecting to [127.0.0.1] via relay... 220 server.2daygeek.com ESMTP Sendmail 8.14.7/8.14.7; Mon, 22 Nov 2021 10:46:06 -0600 >>> EHLO server.2daygeek.com 250-server.2daygeek.com Hello localhost [127.0.0.1], pleased to meet you 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-8BITMIME 250-SIZE 250-DSN 250-ETRN 250-AUTH GSSAPI 250-DELIVERBY 250 HELP >>> MAIL From:<[email protected]> SIZE=293 [email protected] 250 2.1.0 <[email protected]>... Sender ok >>> RCPT To:<[email protected]> >>> RCPT To:<[email protected]> >>> DATA 250 2.1.5 <[email protected]>... Recipient ok 550 5.1.1 <[email protected]>... User unknown 354 Enter mail, end with "." on a line by itself >>> . 250 2.0.0 1NG24xwR329028 Message accepted for delivery [email protected]... Sent (1NG24xwR329028 Message accepted for delivery) >>> RSET 250 2.0.0 Reset state root... Using cached ESMTP connection to [127.0.0.1] via relay... >>> MAIL From:<> SIZE=1317 250 2.1.0 <>... Sender ok >>> RCPT To:<[email protected]> >>> DATA 250 2.1.5 <[email protected]>... Recipient ok 354 Enter mail, end with "." on a line by itself >>> . 250 2.0.0 1AG4X6s0027291 Message accepted for delivery root... Sent (1AG4X6s0027291 Message accepted for delivery) Closing connection to [127.0.0.1] >>> QUIT 221 2.0.0 server.2daygeek.com closing connection
Closing Notes
In this guide, we’ve shown you how to configure Sendmail as a Smart Host to forward all mails generated from localhost to another SMTP server.
This procedure was tested on the RHEL 7 server, but the same procedure woul work other RHEL versions such RHEL 5,6 & 8. Also, this procedure works with their clones like CentOS, AlmaLinux and Rocky Linux.
If you have any questions or feedback, feel free to comment below.
Thank you Magesh, excellent write up. This was very helpful and tested successfully in a lab environment.
Can you do the same article for postfix ?
Sure.