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.
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
The cat command view of this file shows us its resemblance to an email message format.
$ cat hello_curl.txt
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
On checking our mailbox, we should be able to see the sent email.
For an SMTP server like Gmail, the Less secure app access setting should be On for the Curl command to execute without any issues.
Knowing the SMTP server you are using and its associated port number makes it easier to send Emails via the Curl command in Linux.