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.
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

# 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/576405/redirect-mail-from-system-user-account-to-real-email-account

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

 

 

 

 

 

 

 

Stop Spam Harvesters, Join Project Honey Pot

 

Get a free SSL certificate!

 

The leading nonprofit defending digital privacy, free speech, and innovation.

 

The Linux Foundation provides a neutral, trusted hub for developers and organizations to code, manage, and scale open technology projects and ecosystems.

 

Kubuntu is an operating system built by a worldwide community of developers, testers, supporters and translators.

 

 43ef5c89 CanonicalUbuntudarktext