Published on 2019-10-17· Updated on 2023-09-06
The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.
Linux has a default command for sending mail from the command line. This tutorial covers prerequisites and important information to get started.
You need to install and configure a Mail Transfer Agent (MTA) such as Sendmail or Postfix on your server. The MTA is responsible for transmitting email messages between different hosts or systems. It is a critical component of the email delivery process.
When you send an email, the MTA routes the message from the sender's system to the recipient's system. The mail command can only accept your mail-sending request and forward it to the MTA to send the mail.
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:
It is recommended to use lighter MTAs like msmtp or OpenSMTPD instead of Postfix. They support secure mail sending with Gmail.
After completing the installation, use the below command to send a test email from your command line:
echo "My message" | mail -s subject [email protected]
Using echo with pipe avoids unnecessary prompts or confirmation from getting displayed, and -s denotes the mail’s subject.
If you have installed Postfix or Sendmail, you can test Sendmail using the command below:
/usr/sbin/sendmail [email protected] < /filecontent/to/send
Ubuntu users need to install mailutils and set up a mail-sending server or MTA. Use the command below to install mailutils:
sudo apt-get install mailutils
For those using CentOS, installing mailx before configuring a mail-sending server or MTA is necessary.
sudo yum install -y mailx
Due to authentication errors, you might face trouble while sending test mail using Gmail SMTP with a mail-sending server or Mail Transfer Agents like Postfix or Sendmail. In such a case, you will need to complete additional security-related configurations on Gmail. Enable ‘Access for Less Secure Apps’ to send mail from Gmail. For more assistance, refer to this link.
We are a community of email enthusiasts. If you have further queries, feel free to drop an email to [email protected] , and we would be happy to help you.
Netcorecloud's toolkit is the solution to all your email problems.
Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.