New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
SMTP and APIUpdated 4 Nov 2026

Send Email with curl: SMTP Command-Line Examples with TLS and Attachments

Working curl commands for sending email over SMTP: STARTTLS on 587, implicit TLS on 465, authentication, message files, HTML and attachments with MIME forms, verbose debugging, safe credential handling and exit codes.

JL
Written by Jonas Lindqvist
Security Engineer
Reviewed by Hannah Mitchell
  • Published14 May 2024
  • Read time7 min
  • Length1,370 words
Share this guide

curl can send email because it speaks SMTP. You give it a server URL, an envelope sender with --mail-from, one or more recipients with --mail-rcpt, and the message itself with -T (a file) or with MIME form options. Use smtp://host:587 with --ssl-reqd for STARTTLS, or smtps://host:465 for implicit TLS.

That makes curl a quick way to test a relay, send an alert from a shell script, or reproduce a problem without involving your application. This guide gives tested patterns for plain text, HTML, attachments and authentication, explains the flags that matter, and shows how to read curl's output when something fails.

STARTTLS (port 587)curl --ssl-reqd smtp://smtp.migosmtp.com:587 ...
Implicit TLS (port 465)curl smtps://smtp.migosmtp.com:465 ...
Required flags--mail-from, --mail-rcpt and a message via -T or -F
DebuggingAdd -v to see the full SMTP conversation

How curl maps to the SMTP conversation

curl performs the same steps as any SMTP client: connect, EHLO, optional TLS upgrade, optional AUTH, then MAIL FROM, RCPT TO and DATA. Two things trip people up. First, the envelope (--mail-from, --mail-rcpt) is separate from the headers inside the message. curl does not generate headers for you when you use -T; whatever is in the file is what recipients see. Second, the TLS flag decides whether credentials are protected: without --ssl-reqd, curl on a smtp:// URL tries an upgrade only if you use --ssl, and with --ssl it will continue in clear text if the server does not offer TLS.

FlagMeaning
smtp://host:587Plain connection that you upgrade with STARTTLS
--ssl-reqdRequire TLS; fail if the upgrade is not possible. Use this on 587
--sslTry TLS but continue without it if unavailable. Avoid for credentials
smtps://host:465TLS from the first byte (implicit TLS)
--mail-fromEnvelope sender (the bounce address)
--mail-rcptEnvelope recipient; repeat for several
-T fileUpload the file as the message body (headers plus body)
--user "u:p"Credentials for SMTP AUTH
-vVerbose: show the conversation and TLS details

For the port choices behind these URLs, see ports 25, 465 and 587, and for the transport security options see SSL, TLS and STARTTLS.

Send a plain-text message with STARTTLS

Create a message file that includes the headers. Include Date and Message-ID; many servers add them for you, but being explicit helps when you compare results. Lines end with CRLF on the wire, and curl converts line endings when it uploads a text file, so a normal editor file works.

cat > message.txt <<'EOF'
From: Alerts <[email protected]>
To: On-call <[email protected]>
Subject: Test from curl
Date: Tue, 14 May 2024 10:00:00 +0000
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Hello,
this message was sent with curl over SMTP.
EOF

curl --ssl-reqd smtp://smtp.migosmtp.com:587 \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --user 'smtp-username:smtp-password' \
  -T message.txt

A successful run prints nothing and exits with status 0. Add -v the first time to watch it work. Keep the From header on a domain you have authenticated, or receivers may reject the mail; see authenticating your sending domain.

Use implicit TLS on port 465

curl smtps://smtp.migosmtp.com:465 \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --user 'smtp-username:smtp-password' \
  -T message.txt

Do not combine smtps:// with port 587, and do not use smtp:// with 465. The scheme and the port must match the mode the server expects, or you get a handshake error.

Send HTML and attachments with MIME forms

Writing multipart MIME by hand is error-prone. Modern curl (7.56 and later) can build the MIME structure for SMTP with -F and a special syntax. The headers go in with -H, and the body parts are described with form options.

curl --ssl-reqd smtp://smtp.migosmtp.com:587 \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --user 'smtp-username:smtp-password' \
  -H 'From: Alerts <[email protected]>' \
  -H 'To: On-call <[email protected]>' \
  -H 'Subject: Monthly report' \
  -F '=(;type=multipart/mixed' \
  -F '=(;type=multipart/alternative' \
  -F '=Plain text version of the message;type=text/plain' \
  -F '[email protected];type=text/html' \
  -F '=)' \
  -F '[email protected];type=application/pdf;encoder=base64' \
  -F '=)'

The outer part is multipart/mixed, which carries the message and the attachment. The inner multipart/alternative carries the plain-text and HTML versions, and the order matters: put the simplest version first and the richest last. Here message.html is a local file; do not pass inline HTML that starts with a less-than sign after the equals sign, because curl would treat it as a file name. Each =) closes the part opened by the matching =(. Check your curl version with curl --version; older builds do not support this form. For the structure behind it, see multipart MIME explained, and keep attachments within the limits in email size limits.

Handle credentials safely

  • A password in --user appears in the process list and in shell history. For anything beyond a one-off test, move it out of the command line.
  • Put options in a config file readable only by you and pass it with -K: a file containing user = "name:password", with permissions set by chmod 600.
  • Alternatively use a .netrc file with --netrc, again with strict permissions.
  • In CI, inject the secret as a masked environment variable and write the config file at run time, then delete it.
  • Use a credential that can only send mail, and rotate it if it appears in a log. See API key security.
  • Never use -k or --insecure to get past a certificate error in production: it disables the check that protects the login.
# creds.conf  (chmod 600)
user = "smtp-username:smtp-password"

curl --ssl-reqd smtp://smtp.migosmtp.com:587 -K creds.conf \
  --mail-from '[email protected]' --mail-rcpt '[email protected]' -T message.txt

Several recipients, Bcc and scripting

Repeat --mail-rcpt for each recipient. The envelope decides who receives the message; the To and Cc headers are only display text. This is how Bcc works: add the address as an envelope recipient but leave it out of the headers. If a server rejects one recipient, curl may stop for the whole transaction, so check the exit status and the verbose output rather than assuming everyone was served. See what Bcc means for the semantics.

In scripts, test the exit status explicitly (if ! curl ...; then ...), set --connect-timeout and --max-time so a hung connection cannot block a cron job forever, and log the exit code. Do not retry in a tight loop: a rejected message will keep being rejected, while a temporary 4xx failure should be retried after a delay. For command-line sending with other tools, see Linux command-line sending.

Debug with verbose output and exit codes

Run with -v (or --trace-ascii trace.txt to save it). Lines starting with > are what curl sent and lines with < are the server's replies, such as 250 for success or 535 for a failed login. Reading these is the fastest way to find the failing step; SMTP transcripts explains each stage, and reading a bounce code helps with the numbers.

Troubleshooting

SymptomLikely causeFix
curl: (67) Login deniedWrong username or password, or wrong account typeVerify credentials. See fixing 535 errors
curl: (7) Failed to connectPort blocked or wrong hostTest other ports; check firewall rules. See SMTP and firewalls
curl: (28) Operation timed outNetwork filtering or a slow serverRetry with --connect-timeout 15; check egress rules
curl: (35) SSL connect errorsmtp:// used on an implicit TLS port, or the reverseMatch scheme and port: smtp with 587, smtps with 465
curl: (60) SSL certificate problemMissing CA bundle or an intercepting proxyFix the trust store; do not use --insecure
Recipient rejected in the outputThe server refused RCPT TORead the 5xx text. See 550 errors
Message arrives without a subject or senderHeaders missing from the fileAdd From, To and Subject to the message file or use -H

When to use curl and next steps

curl is ideal for diagnostics, cron jobs and single messages from scripts. For anything with volume, personalisation or retries, use a library in your application language and a queue. Once your command works, you know the host, port, TLS mode and credentials are correct, and you can copy those settings into your application. For wider tooling beyond curl, such as swaks and openssl s_client, see testing an SMTP connection.

Test your relay in one command Use curl with the MigoSMTP relay on 587 or 465 to confirm authentication and delivery.

Explore the SMTP relay

References and further reading

Frequently asked questions

How do I send an email with curl?

Use curl smtp://host:587 with --ssl-reqd, --mail-from, --mail-rcpt, --user and -T message.txt, where the file holds the headers and body.

How do I use TLS with curl SMTP?

Use --ssl-reqd with smtp:// on port 587 for STARTTLS, or the smtps:// scheme on port 465 for implicit TLS.

Can curl send attachments?

Yes. Recent curl versions build MIME messages with -F options, using multipart/mixed and encoder=base64 for the file.

Why does curl return error 67?

Error 67 means the login was denied. Check the username, password and whether the account allows SMTP authentication.

Is it safe to put the password on the command line?

It is visible in process lists and history. Use a config file with -K and strict permissions, or a .netrc file.

Topics#curl#smtp#command line#tls#attachments#debugging
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales