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:
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
--sslto the command:
You can tell curl to require upgrading to using secure transfers by adding
--ssl-reqdto the command:
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.comand send your local computer's host name in the HELO / EHLO command:
You can of course as always use the
-voption to get to see the client-server communication.
To instead have curl send
EHLOcommand 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
[email protected], the client will get the MX records for
example.comto 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'.