Contents[Show]

1. What? 

To quickly test if the sendmail command is working correctly to then use it for example in shell scripts, via command line or even from PHP scripts (PHP supports sendmail to send emails from PHP scripts - you can set the sendmail path in your php.ini), you can issue the below command on your UNIX or Linux system:

# echo "Subject: sendmail test" | sendmail -v This email address is being protected from spambots. You need JavaScript enabled to view it.

This email address is being protected from spambots. You need JavaScript enabled to view it. is obviously the e-mail address you want the test email to be sent to. This sendmail command line example will send a blank email with the subject "sendmail test" to This email address is being protected from spambots. You need JavaScript enabled to view it. if the test is successful.

 

2. Useful tricks if no mail gets sent

# nano /etc/aliases

postmaster: root
postmaster: root
root: This email address is being protected from spambots. You need JavaScript enabled to view it.

# newaliases
/etc/aliases: 77 aliases, longest 17 bytes, 792 bytes total

# service sendmail restart
Redirecting to /bin/systemctl restart sendmail.service
  • Did you check that the firewall is letting the traffic through?
    Have a look at How to configure firewalld on CentOS 7 for pointers.
  • If problem persists, try gathering a few lost messages and run the following commands to see a log with the specific mails.
# mailq -qL
/var/spool/mqueue (1 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
wA9D2Iuf023631? 2252 Fri Nov 9 14:02 MAILER-DAEMON
                                     This email address is being protected from spambots. You need JavaScript enabled to view it.

# sendmail -v -qwA9D2Iuf023631 -d11

  • You may also want to check for a dead.letter file for any obvious errors. Location may vary depending on distro, so search for it.
  • Indications on the interweb suggests that mails don't get sent because the hostname is not set properly. Have a look at Changing the hostname in CentOS, Ubuntu for a check-up.
  •  ...

 

 

Sources

https://clients.javapipe.com/knowledgebase/132/How-to-Test-Sendmail-From-Command-Line-on-Linux.html

https://tecadmin.net/ways-to-send-email-from-linux-command-line/

https://www.jeffgeerling.com/blogs/jeff-geerling/configure-sendmail-centos

https://serverfault.com/questions/576405/redirect-mail-from-system-user-account-to-real-email-account

https://serverfault.com/questions/239261/sendmail-ignore-local-delivery/241703#241703