In this article, we shall see how to harness the power of the "curl" command line tool and Gmail SMTP server in order to send an email programmatically using a bash script, "curl" is a versatile and powerful utility command line tool for making HTTP requests. And is primarily known for transferring data from and to servers using various kinds of protocols such as HTTP, HTTPS, FTP, FTPS, and SCP. "curl" is an open-source tool and is available on various unix-based systems such as Linux and macOS. It is also available for the Windows operating system as well.
Features
- Makes use of HTTP requests: The tool can send HTTP and HTTPS requests, which is important while interacting with web services and websites.
- Secure: Offers secure data transfer through SSL/TLS encryption.
- Can be used in Automation: Many scripts and automation tools use curl to fetch and send data programmatically.
- Easy Integration: This can be easily integrated with scripts and automation workflows.
- File Upload: This can be used to upload files to a remote server.
- Can be used in Testing: Curl is a tool for testing API's because it provides detailed information about the response.
Installation of CURL
Step 1: Update and refresh the package repository using the following command.
$ sudo apt update

Step 2: Install the curl command line tool using the following command.
$ sudo apt-get install curl

Step 3: Check whether curl was successfully installed using the following command.
$ curl --help

The Approach
Step 1: Obtain the SMTP Gmail server details. Here we will be sending the email through the Gmail SMTP server. For this we might need to obtain the Gmail SMTP server details, which includes the following.
NOTE: Regular Gmail password for SMTP authentication is not recommended if you have two-factor authentication (2FA) enabled. Generating and using an App Password is a more secure way to authenticate your email client or script when sending emails through Gmail's SMTP server.

Step 2: Create a bash script using the following command, and open the file using a text editor of your choice.
$ touch email.sh
Step 3: Use the below shell script.
#!/bin/bash
# SMTP server settings for Gmail
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT="587"
SMTP_USERNAME="YOUR_GMAIL_ADDRESS"
SMTP_PASSWORD="YOUR_GMAIL_PASSWORD"
# Recipient email address
TO="RECIPIENT_EMAIL_ADDRESS"
# Email subject and body
SUBJECT="Hello from Curl"
BODY="This is the email body sent using Curl and SMTP."
# Construct the email message
MESSAGE="Subject: $SUBJECT\n\n$BODY"
# Send the email using Curl
curl --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
--ssl-reqd \
--mail-from "$SMTP_USERNAME" \
--mail-rcpt "$TO" \
--user "$SMTP_USERNAME:$SMTP_PASSWORD" \
--tlsv1.2 \
-T <(echo -e "$MESSAGE")
echo "Email sent to $TO"
Step 3: Modify the permissions to executable and then run the script.
chmod +x ./email.sh
Step 4: Execute the script using the following command
./email.sh

Output

Conclusion
In conclusion, utilizing the curl command to send emails is a powerful and efficient method for automating email communications within your applications or scripts but it is not necessarily the recommended way of sending an email via scripts due to security concerns. With the guidance provided in this article, you have learned how to configure curl to send an email with the help of the Gmail's SMTP server.
Similar Reads
How to Send Email using NodeJS? Sending emails programmatically is a common requirement in many applications, especially for user notifications, order confirmations, password resets, and newsletters. In this article, we will learn how to build a simple email-sending system using NodeJS. We will use Nodemailer, a popular module for
5 min read
How to Send Email Using Amazon SES? Amazon SES (Simple Email Service) is a cloud-based email service that allows businesses to send both transactional and mass emails. Whether you want to send a welcome email, an invoice, or a promotional offer, Amazon SES provides a reliable and cost effective solution.In this guide, you will learn h
6 min read
Sending an email using Perl Perl allows its users to send mails using the Perl code-script. There are various ways to send email using Perl. These emails can be simple emails, emails with attachment, HTML formatted emails, emails with multiple receivers, etc.. Perl provides some modules for doing the same as well. In this arti
7 min read
Spring Boot - Sending Email via SMTP Spring Boot provides the ability to send emails via SMTP using the JavaMail Library. Here we will be illustrating step-by-step guidelines to develop Restful web services that can be used to send emails with or without attachments. In order to begin with the steps, let us first create a Spring Boot p
5 min read
Send mails using a Bash Script Sending email via the command line can be quite a great feature to have especially on Linux which will allow some users to avoid using the GUI and installing all of the dependencies. We will look into the following article on how to write a BASH (shell) script that can send a custom email to any oth
9 min read