Sending email
Sending email with curl is done with the SMTP protocol. SMTP stands forSimple Mail TransferProtocol.
curl supports sending data to an SMTP server, which combined with the rightset of command line options makes an email get sent to a set of receivers ofyour choice.
When sending SMTP with curl, there are two necessary command line options thatmust be used.
You need to tell the server at least one recipient with
--mail-rcpt
. Youcan use this option several times and then curl tells the server that allthose email addresses should receive the email.You need to tell the server which email address that is the sender of theemail with
--mail-from
. It is important to realize that this emailaddress is not necessarily the same as is shown in theFrom:
line of theemail text.
Then, you need to provide the actual email data. This is a (text) fileformatted according toRFC5322. It is a set of headers and abody. Both the headers and the body need to be correctly encoded. The headerstypically includeTo:
,From:
,Subject:
,Date:
etc.
A basic command to send an email:
curl smtp://mail.example.com --mail-from myself@example.com --mail-rcpt \receiver@example.com --upload-file email.txt
An exampleemail.txt
could look like this:
From: John Smith <john@example.com>To: Joe Smith <smith@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.
Secure mail transfer
Some mail providers allow or require using SSL for SMTP. They may use adedicated 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 ofsmtp://, which uses the SMTP SSL port of 465 by default and requires the entireconnection to be SSL. For example smtps://smtp.gmail.com/.
However, if your provider allows upgrading from clear-text to secure transfersyou 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 totry but not require upgrading to secure transfers byadding--ssl
to the command:
curl --ssl smtp://mail.example.com --mail-from myself@example.com \ --mail-rcpt receiver@example.com --upload-file email.txt \ --user 'user@your-account.com:your-account-password'
You can tell curl torequire upgrading to using secure transfers by adding--ssl-reqd
to the command:
curl --ssl-reqd smtp://mail.example.com --mail-from myself@example.com \ --mail-rcpt receiver@example.com --upload-file email.txt \ --user 'user@your-account.com:your-account-password'
The SMTP URL
The path part of a SMTP request specifies the hostname to present duringcommunication with the mail server. If the path is omitted then curl attemptsto figure out the local computer's hostname and use that. However, this maynot return the fully qualified domain name that is required by some mailservers and specifying this path allows you to set an alternative name, suchas your machine's fully qualified domain name, which you might have obtainedfrom an external function such as gethostname or getaddrinfo.
To connect to the mail server atmail.example.com
and send your localcomputer's hostname in theHELO
orEHLO
command:
curl smtp://mail.example.com
You can of course as always use the-v
option to get to see theclient-server communication.
To instead have curl sendclient.example.com
in theHELO
/EHLO
commandto the mail server atmail.example.com
, use:
curl smtp://mail.example.com/client.example.com
No MX lookup
When you send email with an ordinary mail client, it first checks for an MXrecord for the particular domain you want to send email to. If you send anemail tojoe@example.com
, the client gets the MX records forexample.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 tosend an email to for a particular domain, we recommend you figure that outfirst and then call curl to use those servers. Useful command line tools toget MX records include 'dig' and 'nslookup'.