Sending emails from the command line is a valuable capability for system administrators and developers, particularly for automated notifications, system alerts, and application-generated messages. Linux provides several tools for this purpose, with ssmtp and sendmail being among the most commonly used.
This guide covers both tools:
ssmtp: A simple Mail Transfer Agent (MTA) designed to deliver mail from a local system to a mail hub (SMTP server). It's lightweight and ideal for systems that need to send mail but don't need to receive it.
sendmail: A more comprehensive mail transfer agent that has been a standard part of Unix/Linux systems for decades. The term "sendmail" can refer to both the original Sendmail MTA and the command-line interface that many MTAs provide for compatibility.
Both tools allow you to send emails directly from the command line or from scripts, making them essential for system automation and notification tasks.
ssmtp is a simple mail transfer agent designed to deliver mail from a local system to a mail hub (SMTP server). It's not a full mail server but rather a relay that forwards mail to a proper mail server for delivery.
Key features of ssmtp:
Extremely lightweight and simple to configure
No mail queues or daemons to run
Perfect for systems that only need to send occasional emails
Can use external SMTP servers (like Gmail, Office 365, etc.)
The main configuration file for ssmtp is /etc/ssmtp/ssmtp.conf. Here's how to configure it to use a Gmail account as a relay:
sudo nano /etc/ssmtp/ssmtp.conf
Add or modify the following lines:
# The mail server (SMTP)mailhub=smtp.gmail.com:587# The address where bounces are sentrewriteDomain=gmail.com# The full hostnamehostname=your_hostname# Use SSL/TLSUseSTARTTLS=YES# AuthenticationAuthUser=your_gmail_address@gmail.comAuthPass=your_app_passwordAuthMethod=LOGIN# Email address formatFromLineOverride=YES
Note: For Gmail, you'll need to use an "App Password" rather than your regular password if you have 2-factor authentication enabled.
echo -e "To: recipient1@example.com, recipient2@example.com\nFrom: sender@example.com\nSubject: Test Email\n\nThis is the body of the email." | ssmtp recipient1@example.com recipient2@example.com
#!/bin/bashTO="recipient@example.com"FROM="sender@example.com"SUBJECT="Automated Email"BODY="This is an automated email sent from a bash script."echo -e "To: $TO\nFrom: $FROM\nSubject: $SUBJECT\n\n$BODY" | ssmtp $TO
The sendmail command is a standard interface for sending mail that's implemented by various MTAs, including the original Sendmail MTA, Postfix, Exim, and others. When people refer to "sendmail," they might mean either:
The original Sendmail MTA (a full mail server)
The sendmail command-line interface provided by various MTAs for compatibility
In this guide, we focus on the command-line interface, which is available regardless of which MTA is installed.
echo -e "To: recipient1@example.com\nCc: recipient2@example.com\nFrom: sender@example.com\nSubject: Test Email\n\nThis is the body of the email." | sendmail -t
Like ssmtp, sendmail itself doesn't handle attachments directly. You can use mutt or mail commands:
# Using muttecho "Email body" | mutt -s "Subject" -a /path/to/attachment.pdf -- recipient@example.com# Using mail (mailutils)mail -s "Subject" -A /path/to/attachment.pdf recipient@example.com < body.txt
#!/bin/bashTO="recipient@example.com"FROM="sender@example.com"SUBJECT="Automated Email"BODY="This is an automated email sent from a bash script."echo -e "To: $TO\nFrom: $FROM\nSubject: $SUBJECT\n\n$BODY" | sendmail -t
Command-line email tools like ssmtp and sendmail provide essential functionality for system administrators and developers who need to send automated emails from Linux systems. While ssmtp offers a lightweight solution perfect for simple forwarding to a smart host, sendmail (or a modern alternative like Postfix) provides a more complete mail handling system.
For most modern systems that just need to send occasional emails, ssmtp is often the preferred choice due to its simplicity. However, for systems that need to handle more complex mail operations, a full MTA with the sendmail command interface is more appropriate.
By mastering these command-line email tools, you can effectively implement automated notifications, system alerts, and application-generated messages in your Linux environment.
Test Your Knowledge
Take a quiz to reinforce what you've learned
Exam Preparation
Access short and long answer questions for written exams