To send emails using Bash and SSMTP (Simple SMTP), you can follow these steps:
- 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:
email@example.com mailhub=smtp.gmail.com:587 AuthUserfirstname.lastname@example.org 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="email@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
firstname.lastname@example.org 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:
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.