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 -vThis email address is being protected from spambots. You need JavaScript enabled to view it.
This sendmail command line example will send a blank email with the subject "sendmail test" to
Also, the -v flag will produce a mail status delivery report; you may or may not need it, but hte option is there.
Useful tricks if no mail gets sent
- Check the steps over at https://www.jeffgeerling.com/blogs/jeff-geerling/configure-sendmail-centos.
- Have a look in /etc/aliases so that you have a line like below. If not, add them, save and exit then run newaliases.
- If you don't run the newaliases command, sendmail won't pick up the change.
- For good measure you may want to restart your sendmail daemon too.
# nano /etc/aliases
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.
- ...
Postfix
Postfix is easy to setup if you only need to send status mails and such.
After installing it, open /etc/postfix/main.cf and check the relayhost line. It should normally have a mailserver name and possibly a port if your ISP requires that.
relayhost = [mailserver.example.com]:587
Restart the daemon after saving the file.
# systemctl restart postfix
Test it with this line.
# echo "This is the body of the email" | mail -s "This is the subject line"This email address is being protected from spambots. You need JavaScript enabled to view it.
Check your mailbox to confirm it got sent.
If you get an error regarding the mail command missing, try this and rerun the command above.
# apt install mailutils
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/239261/sendmail-ignore-local-delivery/241703#241703