Sending email with curl is done with the SMTP protocol. SMTP stands for Simple Mail Transfer Protocol.
curl supports sending data to an SMTP server, which combined with the right set of command line options makes an email get sent to a set of receivers of your choice.
When sending SMTP with curl, there are two necessary command line options that must be used.
You need to tell the server at least one recipient with
--mail-rcpt. You can use this option several times and then curl will tell the server that all those email addresses should receive the email.
You need to tell the server which email address that is the sender of the email with
--mail-from. It is important to realize that this email address is not necessarily the same as is shown in the
From: line of the email text.
Then, you need to provide the actual email data. This is a (text) file formatted according to RFC 5322. It is a set of headers and a body. Both the headers and the body need to be correctly encoded. The headers typically include
A basic command to send an email:
curl smtp://mail.example.com --mail-from firstname.lastname@example.org --email@example.com --upload-file email.txt
From: John Smith <firstname.lastname@example.org>To: Joe Smith <email@example.com>Subject: an example.com example emailDate: Mon, 7 Nov 2016 08:45:16Dear Joe,Welcome to this example email. What a lovely day.
Some mail providers allow or require using SSL for SMTP. They may use a dedicated port for SSL or allow SSL upgrading over a clear-text connection.
If your mail provider has a dedicated SSL port you can use smtps:// instead of smtp://, which uses the SMTP SSL port of 465 by default and requires the entire connection to be SSL. For example smtps://smtp.gmail.com/.
However, if your provider allows upgrading from clear-text to secure transfers you can use one of these options:
--ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)--ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)
You can tell curl to try but not require upgrading to secure transfers by adding
--ssl to the command:
curl --ssl smtp://mail.example.com --mail-from firstname.lastname@example.org email@example.com --upload-file email.txt--user 'firstname.lastname@example.org:your-account-password'
You can tell curl to require upgrading to using secure transfers by adding
--ssl-reqd to the command:
curl --ssl-reqd smtp://mail.example.com --mail-from email@example.com firstname.lastname@example.org --upload-file email.txt--user 'email@example.com:your-account-password'
The path part of a SMTP request specifies the host name to present during communication with the mail server. If the path is omitted then curl will attempt to figure out the local computer's host name and use that. However, this may not return the fully qualified domain name that is required by some mail servers and specifying this path allows you to set an alternative name, such as your machine's fully qualified domain name, which you might have obtained from an external function such as gethostname or getaddrinfo.
To connect to the mail server at
mail.example.com and send your local computer's host name in the HELO / EHLO command:
You can of course as always use the
-v option to get to see the client-server communication.
To instead have curl send
client.example.com in the
EHLO command to the mail server at
When you send email with an ordinary mail client, it will first check for an MX record for the particular domain you want to send email to. If you send an email to firstname.lastname@example.org, the client will get the MX records for
example.com to learn which mail server(s) to use when sending email to example.com users.
curl does no MX lookups by itself. If you want to figure out which server to send an email to for a particular domain, we recommend you figure that out first and then call curl to use those servers. Useful command line tools to get MX records include 'dig' and 'nslookup'.