Home Linux Commandline Tips How to Send Email Using Curl Command in Linux

How to Send Email Using Curl Command in Linux

When you find yourself in a Linux operating system environment that mimics the behavior of a server machine, graphical options like desktop environments are mostly not an option. Therefore, the reliance of your server-based operations and executions depend on the Linux terminal or command-line environment in front of you.

Data transfer is an important Linux OS needs for all users. When trying to meet this objective of data transfer under Linux; especially when we are confined to a terminal-based or command-line OS environment.

We resort to tools like Wget and Curl. However, Curl has an upper hand over Wget such that it can do more than just transfer/get data via URL as we shall see in this article.

Install Curl Command in Linux

Curl is not installed by default on our Linux OS distributions. To install curl on our various Linux OS distributions, reference one of the following installation commands.

$ sudo apt install curl         [On Debian, Ubuntu and Mint]
$ sudo yum install curl         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a net-misc/curl  [On Gentoo Linux]
$ sudo pacman -S curl           [On Arch Linux]
$ sudo zypper install curl      [On OpenSUSE]    

Sending Email via Curl Command in Linux

Before we demonstrate how to send our mail, we first need to understand the structure of a mail scheduled for dispatch in reference to the Curl command syntax we will use.

For this purpose, we will create a sample text file called hello_curl.txt.

$ nano hello_curl.txt
Create Email File in Linux
Create Email File in Linux

The cat command view of this file shows us its resemblance to an email message format.

$ cat hello_curl.txt
View Email Content in Linux
View Email Content in Linux

The curl command syntax for sending an email that we need to reference is as follows:

$ curl --ssl-reqd \
  --url 'smtps://smtp.domain_name:smtp_port' \
  --user 'sender@domain_name:sender_password' \
  --mail-from 'sender@domain_name' \
  --mail-rcpt 'recepient@domain_name' \
  --upload-file file_name.txt

The hello_curl.txt file we created will be specified as an attachment under the --upload-file command option. The command option --ssl-redq forces us to use SSL (Secure Socket Layer) for mail transfer. The --url command option points to the SMTP parameters we are using for mail transfer.

For instance, let us assume we want to use the Gmail SMTP for mail transfer, our executable Curl command will look like the following:

$  curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user '[email protected]:sender_password' \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --upload-file hello_curl.txt
Send Email Using Curl Command
Send Email Using Curl Command

On checking our mailbox, we should be able to see the sent email.

View Email in Gmail
View Email in Gmail

For an SMTP server like Gmail, the Less secure app access setting should be On for the Curl command to execute without any issues.

On Less Secure App Access on Gmail
On Less Secure App Access on Gmail

Knowing the SMTP server you are using and its associated port number makes it easier to send Emails via the Curl command in Linux.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.