Sending emails from the command line is a handy skill for system administrators and developers. SSMTP (Simple SMTP) is a lightweight solution for sending emails from the command line using an SMTP server. In this guide, we will explore how to set up and use SSMTP to send emails from a Linux system, specifically Ubuntu, using Gmail and Amazon AWS as SMTP servers.
What is SSMTP?
SSMTP is a simple and lightweight utility that allows you to send emails from your Linux command line. It is designed to be easy to configure and use, making it an ideal choice for sending automated emails or notifications from scripts and applications.
Step-by-Step Guide to Sending Emails Using SSMTP
1. Installing SSMTP
First, you need to install SSMTP on your Ubuntu system. Open your terminal and run the following command:
sudo apt-get update
sudo apt-get install ssmtp
2. Configuring SSMTP
Once installed, you need to configure SSMTP to use your SMTP server details. The configuration file is located at /etc/ssmtp/ssmtp.conf
. Open this file in a text editor:
sudo nano /etc/ssmtp/ssmtp.conf
3. Configuring SSMTP for Gmail
To use Gmail’s SMTP server, add the following configuration to ssmtp.conf
:
hostname=YOUR-EmailServer
root=username@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=username@gmail.com
AuthPass=yourpassword
FromLineOverride=YES
UseSTARTTLS=YES
Replace username@gmail.com
with your Gmail address and yourpassword
with your Gmail password. For enhanced security, consider using an App Password generated from your Google account.
4. Configuring SSMTP for Amazon AWS SES
To use Amazon AWS SES as your SMTP server, add the following configuration:
root=your-aws-email@example.com
mailhub=email-smtp.us-east-1.amazonaws.com:587
AuthUser=your-aws-smtp-username
AuthPass=your-aws-smtp-password
UseSTARTTLS=YES
Replace your-aws-email@example.com
with your verified AWS SES email address, and your-aws-smtp-username
and your-aws-smtp-password
with your AWS SMTP credentials.
5. Sending an Email
To send an email using SSMTP, you can use the following command:
echo "Subject: Test Email" | ssmtp recipient@example.com
You can also send a more detailed email by including the email body:
echo -e "Subject: Test Email\n\nThis is the body of the email." | ssmtp recipient@example.com
6. Using SSMTP in Scripts
SSMTP can be integrated into your scripts to automate the sending of emails. Here is a simple example of a script that sends an email notification:
#!/bin/bash
TO="recipient@example.com"
SUBJECT="Backup Completed"
BODY="The backup process has completed successfully."
echo -e "Subject: $SUBJECT\n\n$BODY" | ssmtp $TO
Save this script, make it executable, and run it:
chmod +x send_email.sh
./send_email.sh
Troubleshooting
- Authentication Errors: Ensure your SMTP credentials are correct. For Gmail, consider using an App Password.
- Network Issues: Verify that your server can connect to the SMTP server by checking your network settings and firewall rules.
Sending emails from the Linux command line using SSMTP is a straightforward process. Whether you are using Gmail or Amazon AWS as your SMTP server, this guide provides the necessary steps to configure and use SSMTP for sending emails. Automating email notifications can be a powerful tool for system administration and development workflows.