Amazon SES (Simple Email Service) is a popular SMTP service provider similar to Sendgrid, Mailchimp, etc. In order to use SES, you need to signup for an Amazon Web Services account. Which is the leading Cloud-based service provider. Post signup you need to add your credit card for the billing. The default SES allows sending 2000 emails/day freely. After the default limit, you will be charged as pay-per-use.
In this blog post, you will learn to send emails via Amazon SES or any other SMTP provider from a bash shell or script.
Pre-Requisiteis
- In this tutorial, we used the SendEmail command line SMTP client for sending emails. So you must have installed SendMail on your system.
- You must have verified the email address or the domain name under Verified Identities in Amazon SES. When the domain is verified, you can use any email address while sending emails
- All the new accounts in the Amazon SES are in sandbox mode for security purposes. You need to submit a request to support converting the SES account to production mode.
Shell Script for Sending Emails via SMTP
I have written a small shell script that sends emails via the remote SMTP servers. It uses the SendEmail SMTP client. Use any of the popular SMTP providers (like Sendgrid, Amazon SES, and Mailchimp) with this shell script. You can also integrate this shell script code into your existing shell scripts for sending emails properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/usr/bin/env bash ## SMTP configuration details SMTP_HOST="email-smtp.us-east-1.amazonaws.com" SMTP_PORT="587" SMTP_USER="XXXXXXXXXXXXXXX" SMTP_PASS="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" EMAIL_FROM="[email protected]" EMAIL_TO="[email protected]" SUBJECT="WARNING: Github Public Repo Found" ## Sending email cat Mailcontent.txt | sendemail -l /tmp/email.log \ -f ${EMAIL_FROM} \ -u ${SUBJECT} \ -t ${EMAIL_TO} \ -s ${SMTP_HOST}:${SMTP_PORT} \ -o tls=yes \ -xu ${SMTP_USER} \ -xp ${SMTP_PASS} |
In the above script, the Mailcontent.txt file contains the mail body content.
Conclusion
Shell scripts are an important part of system administration. It helps us to automate tasks quickly like scheduling backups, archiving logs and collecting data, etc. Sometimes we are also required to send emails from shell scripts. In this tutorial, you have learned to send emails via the Amazon SES server. Even you can also use this script with any other SMTP providers.