The Wayback Machine - https://web.archive.org/web/20240720005818/https://www.geeksforgeeks.org/using-curl-to-send-email/
Open In App

Using curl to send email

Last Updated : 10 Oct, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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

Using curl to send email

Step 2: Install the curl command line tool using the following command.

$ sudo apt-get install curl

Using curl to send email

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

$ curl --help

Using curl to send email

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.

  • You will need your Gmail email address, which serves as your sender’s email address. If you don’t have a Gmail account, you can create one for free at https://mail.google.com/.
  • You’ll need the password for your Gmail account. If you have two-factor authentication (2FA) enabled for your Gmail account, you should use an “App Password” instead of your regular Gmail password. You can use this link https://myaccount.google.com/apppasswords to generate an app password for your account.

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.

apppassword-min

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

geek3333

Output

Using curl to send email

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 in Android App Without Using Intent?
It is crucial to establish an email connection in the Android application. This enables us to send important messages to users. For example, when the user joins the app first time sending a welcome email is a common practice. Additionally in some applications if a user signs in from a new location a notification is sent to inform them of this activ
5 min read
Fakemailer - Send email anonymously in Kali Linux
Anonymity plays an important role in the life of a cybersecurity researchers and law and enforcement agencies. Sometimes a situation occurs in which you have to send an email anonymously to another security researcher in order to maintain integrity which is a third important part of the CIA model. So here Fakemailer plays an important role to send
2 min read
Shell script to send email
In today's fast-paced digital world, email remains one of the most essential communication tools. Whether it's for business correspondence, personal communication, or automated notifications, sending emails efficiently is crucial. Shell scripting offers a powerful way to automate various tasks, including sending emails. In this comprehensive guide,
4 min read
Using Curl to make REST API requests
REST APIs are essential for modern web applications, enabling programmatic interaction with data and functionality. Curl is a command-line tool for making web requests, often used directly from the terminal. For example, curl -L ip.ba3a.tech fetches IP address details in JSON format, just like visiting the site in a browser. Essentially, Curl print
5 min read
Difference Between wget VS curl
wget and curl are the commands that are used to HTTP requests without any GUI or software, rather we use the Terminal in Linux that provides the respective output or message. The commands are very useful for web crawling, web scraping, testing RESTful APIs, etc. Curl Curl is a free and open-source command-line utility tool that allows the users as
3 min read
Testing REST API with Postman and curl
In the world of API testing, there are many tools available. Postman and cURL are two of the most popular tools for it. Let's look at how to use these tools for testing them. We will send some HTTP requests and explore the basic syntax for both of them in this article. The article focuses on using a fake REST API named JSON placeholder. What is RES
7 min read
How to Hide the CURL Progress Bar
CURL or client for URL is a popular command line tool for sending and obtaining data and files using the URL. CURL can easily be installed on multiple operating systems. It is also trendy and is used by lots of people around the world. But sometimes cURL's progress bar can be annoying or might take precious screen real-estate, this can be resolved
3 min read
curl Command in Linux with Examples
In the realm of Linux, mastering the command line is essential for efficient and powerful usage of the operating system. Among the number of command line tools available, `curl` stands out as one of the most versatile and powerful utilities. Originally developed by Daniel Stenberg, `curl` is a command-line tool and library for transferring data wit
6 min read
CURL to Access Page that Required Login from Different Page
In this article, I have illustrated how you can utilize curl, a versatile command-line tool, to mimic the login process and access confidential web pages. I have also shown you how to perform the necessary steps to send POST requests using the correct credentials, no doubt, how to deal with authorization cookies and enable you to send GET requests
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 other email using the SMTP server. Creating Google Ap
9 min read
three90RightbarBannerImg