How to Send emails with Bash and SSMTP

To send emails using Bash and SSMTP (Simple SMTP), you can follow these steps:

  1. Install SSMTP: First, make sure SSMTP is installed on your system. You can install it using your package manager. For example, on Ubuntu, you can run:
sudo apt-get install ssmtp

2. Configure SSMTP: Next, configure SSMTP to use your SMTP server for sending emails. Open the SSMTP configuration file for editing:

sudo nano /etc/ssmtp/ssmtp.conf

Update the configuration with your SMTP server details. Here’s an example configuration for using Gmail’s SMTP server:

root=your-email@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=your-email@gmail.com
AuthPass=your-gmail-password
UseSTARTTLS=YES

3. Write the Bash script: Create a Bash script that uses SSMTP to send emails. Here’s an example script:

#!/bin/bash

recipient="recipient@example.com"
subject="Test Email"
body="This is a test email sent using Bash and SSMTP."

echo "To: $recipient" > /tmp/email.txt
echo "Subject: $subject" >> /tmp/email.txt
echo -e "$body" >> /tmp/email.txt

ssmtp "$recipient" < /tmp/email.txt

rm /tmp/email.txt

Replace recipient@example.com with the actual email address of the recipient. Update the subject and body of the email as needed.

4. Make the script executable: Make the Bash script executable by running:

chmod +x send_email.sh

5. Send the email: Run the Bash script to send the email:

./send_email.sh

The script will create a temporary email text file, populate it with the recipient, subject, and body, and then use SSMTP to send the email. Finally, it removes the temporary file.

Make sure to secure your email credentials and keep them confidential. Test the script and verify that you receive the email successfully.

Leave a Comment

Your email address will not be published. Required fields are marked *