Introduction
Linux by default comes with a mail command which can be used to sendmail from command line. Here in this tutorial let's understand all the pre-requisites for how to test sendmail from Linux Command Line and everything else that you need to know before testing a Sendmail.
Prerequisites:
You need to just ensure that there is a Mail Transfer Agent (MTA) installed e.g. sendmail or postfix and configured on your server.
By default, mail command won't be able to send mails. It can just accept your mail sending request and the request then can be forwarded to the MTA to sendmail.
Optional Step: In case you don't have sendmail or postfix installed
You can follow the below links to install and configure a Mail Transfer Agent:
Postfix is definitely the most powerful open source to send mail, but if you just want to send a simple test mail, then try ssmtp or msmtp.
SSMTP is actually depreciated and hence it is recommended to use its alternative like msmtp or OpenSMTPD.
These are lighter version MTAs and doesn't necessarily require you to keep running their daemon programs like it is there in case of postfix. It has most of the feature support like secure mail sending or sending mail with Gmail.
Quick Sendmail Test from the Linux command line
Once the installation is done, just run the following command and you will be able to send a test mail from your command line:
echo "My message" | mail -s subject user@gmail.com
Using echo with pipe avoids the unnecessary prompts or confirmation from getting displayed.
Optional Read
If you have installed postfix or Sendmail, then you can directly do sendmail test using the below command:
/usr/sbin/sendmail user@domain.com < /filecontent/to/send
For Ubuntu users: You need to first install mailutils and then set up a mail sending server or MTA.
Use the below command to install mailutils:
sudo apt-get install mailutils
For CentOS users: You need to first install mailx and then set up a mail sending server or MTA.
sudo yum install -y mailx
Note: If you have set up a mail sending server or Mail Transfer Agents like Postfix or sendmail and trying to send test mail using Gmail SMTP and getting some authentication error then no need to worry because there are few additional security-related configurations which you need to be done on Gmail.
To sendmail from Gmail, you need to turn on the "Access for Less Secure Apps". For more help, refer to THIS link.