Tag Archives: Linux Tutorials

Linux Tutorials and Unix Similar multi-user operating systems based on the Linux kernel and essentially on GNU software. Like CentOS, Debian, Ubuntu Fedora.

Send E-Mail using cURL

How to send e-mail over an SMTP gateway using cURL

How to Send E-Mail using cURL

cURL in combination with command-line options send an e-mail to a recipient. Originally the name cURL meant “see URL” and was later reinterpreted to the current Backronym cURL.

The basic command for send an e-mail using cURL.

$ curl smtp://mail.server.com --mail-from sender@domain.com --mail-rcpt
receiver@domain.com --upload-file email.txt

Example: cURL send e-mail using SMTP in the terminal emulator:

$ curl -v smtp://mail.server.com/email.com --mail-from don@email.com --mail-rcpt rosa@email.com --upload-file email.txt

The following is the output in the terminal:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 10.12.15.13...
* TCP_NODELAY set
* Connected to mail.server.com (10.12.15.13) port 25 (#0)
< 220 mail.server.com ESMTP
> EHLO email.com
< 250-mail.server.com
< 250-PIPELINING
< 250-SIZE 22000000
< 250-ETRN
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250 DSN
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0> MAIL FROM:<don@email.com> SIZE=355
< 250 2.1.0 Ok
> RCPT TO:<rosa@email.com>
< 250 2.1.5 Ok
> DATA
< 354 End data with <CR><LF>.<CR><LF>
} [355 bytes data]
* We are completely uploaded and fine
< 250 2.0.0 Ok: queued as B744B28008
100   355    0     0  100   355      0    355  0:00:01 --:--:--  0:00:01  1334
* Connection #0 to host mail.server.com left intact

Using -v parameter to generates verbose output.

email.txt is uploaded and sent e-mail by cURL over the SMTP gateway.

From: Don <don@email.com>
To: Rosa <rosa@email.com>
Subject: This is an example with cURL SMTP
Date: Sam, Jan 19, 2019 12:50:16 PM

Hello Rosa,
Welcome to cURL email, today is a beautiful day.

The content of email.txt take From, To, Subject, Date as data fields that serves the Simple Mail Transfer Protocol. To send e-mail by curl.

  The Mail Transfer Agent (MTA) must be configured to accept e-mail from the host that uses cURL on it, if it is not an MTA itself, a directive must be configured with smtpd_sender_restrictions, e.g. smtpd_client_restrictions.

What is cURL

curl is a command-line tool for getting or sending data including files using URL syntax. Since curl uses libcurl, it supports every protocol libcurl supports.

It was first released in 1996. It was originally named httpget and then became urlget before adopting the current name of cURL. The original author and lead developer is the Swedish developer Daniel Stenberg, who created cURL because he wanted to automate the fetching of currency exchange rates for IRC users.

Client URL (“curl”) is a command line tool that enables data exchange between a device and a server through a terminal. Using this command line interface (CLI), a user specifies a server URL (the location where they want to send a request) and the data they want to send to that server URL, like send an e-mail by curl.

API tools like Postman and Insomnia provide an interactive user interface (UI) that allows you to make different forms of requests to URLs, for receiving and processing requests. The cURL command does the same thing, except in your terminal. cURL works on Linux, Mac, and Windows.

The cURL command uses the libcURL client-side URL transfer library. This library supports many different transfer protocols including HTTPS, SMTP, and FTP. It also enables you to include cookies, set proxies, and add authentication credentials when making requests.

How to apply cURL in the Linux practice

cURL – Client for URLs Program Library and Command Line Program

cURL is a command line tool that allows you to transfer files from shell or bash scrips via protocols such as http, https, ftp, gopher, sftp, ftps or scp. cURL 1997 developed by Daniel Stenberg is very popular with website developers and is licensed under the open source MIT license.

Unique operating systems include cURL in the standard. Many Embedded Linux systems include cURL, as with Synology, cURL is also ported for Windows and is available in the 64 bit and 32 bit versions. PHP supports the cURL functions with the libcrul library.

cURL offers a wide range of applications, so websites can be remotely controlled and tested, forms can be filled out automatically, information from websites can be checked and processed into other processes. In the Linux Shell Console, the installation of wget is not mandatory, but cURL contains extensive parameters and options, and is already pre-installed.

The following are some examples of how to use cURL in the shell console, so web pages can be retrieved as follows.

$ curl http://example.com/seite.html

This reads the file page.html and outputs it on the standard output. If you want to save the file under the name mypage.html, the following command is used:

$ curl -o mysite.html http://example.com/site.html

Connections to IPv6 hosts are initialized as follows.

$ curl "http://[2001:1234:1234:1::40]/"

  The IPv6 address must be enclosed in square brackets.

View your own Internet IP address with cURL.

$ curl ipline.ch

When a URL has changed, the web page is often redirected to the new URL address, often also http to https redirects. Since cURL does not follow forwards in the standard configuration, you have to pass the parameter -L in this case.

For web pages with self-issued certificate, or outdated TLS v1.0 encryption algorithm, the -k parameter must be applied to allow insecure connections.

$ curl -k https://router.local

When a web site requests user authentication over HTTP, cURL can pass username and password, separated by a colon.

$ curl -u username:password http://www.example-shop.com/

Read the HTTP header with cURL.

$ curl --head http://www.google.com/

The –head parameter instructs cURL to give the output of the HTTP header to the default output. If you want to write the header to the header.txt file, you could do so by pipe operators in the shell, or without the support of the shell by the –dump-header parameter.

$ curl --dump-header headers.txt http://www.google.com/

The current version 7.63 supports the following protocols: DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP.

cURL’s FTP client can also interact with FTP servers.

$ curl ftp://username:password@ftp-server.com

cURL is instructed to output the index of the main directory. Username and password are passed directly with the URL. If you want to list the files subdirectory on the FTP server, it is sufficient to enter the directory to the URL:

$ curl ftp://username:password@ftp-server.com/files/

With cURL an upload is also possible, for this you pass the parameter -T followed by the file name and if necessary.dem path.

$ curl -T meineseite.html -u username:password ftp://ftp-server.com/docs/deineseite.html

Through this command, cURL copies the mypage file.html to the FTP server to the docs directory under the file names yourpage.html.

Preferably, you should use SFTP, where the file.txt is retrieved over an encrypted connection.

$ curl --ftp-ssl ftp://ftp-server.com/datei.txt

The file transfer also goes via SCP, here the file.txt transferred from the directory docs.

$ curl -u username scp://server.com/docs/datei.txt

For automated processes via scripts, the user login is not suitable, here you can authenticate with a private key.

$ curl -u username: --key ~/.ssh/id_rsa scp://server.com/~/datei.txt

The file .txt is transferred from the server’s home directory.

cURL can also be used with Server Message Block (SMB), such as MS LAN Manager and Samba.

$ curl -u "domain\username:passwd" smb://server.server.com/freigabe/datei.txt

Access to file.txt via sharing on a Samba server.

cURL can do even more, the following command line downloads a shell script file and runs it immediately, starting with a period (.).

$ . <(curl -s https://ipline.ch/echo/sysinfo.txt)

This command is executable in a Linux bash shell, system configurations and logs are read out and compiled in one file, and stored as a file under /tmp/sysinfo-*.html.

The following code sample for PHP, the result of search on Google is output with echo $result:

<?php
$cSession = curl_init(); 
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com/search?q=think tank");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;
?>

A look at man curl can be useful, here are all commands with examples of cURL.

curl(1) Curl Manual curl(1)

Name
       curl - transfer a URL

Synopsis
       Curl  

DESCRIPTION
       curl is a tool to transfer dat[options]a[URL...] from or to a server, using one of the supported protocols (DICT, FILE, FTP,
       FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET
       and TFTP).  The command is designed to work without user interaction.

       curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL
       connections, cookies, file transfer resume, Metalink, and more. As you will see below, the number of features
       will make your head spin!

       curl is powered by libcurl for all transfer-related features. Lake libcurl(3) for details.

Url
       The URL syntax is protocol-dependent. You'll find a detailed description in RFC 3986.

       You can specify multiple URLs or parts of URLs by writing part sets within braces as in:

http://site. "one,two,three".com

or you can get sequences of alphanumeric series by using[] as in:

ftp://ftp.numericals.com/file.txt
[1-100]        ftp://ftp.numericals.com/file.t[001-100]xt (with leading zeros)
        ftp://ftp.letters.com/file[a-z].txt
...

cURL Online Manual
Sources Link: curl.haxx.se