Frameworks

How to send email using an API with cURL

Short answer

To send email using an API, make an HTTPS POST with a JSON body containing from, to, subject and html or text, and an Authorization: Bearer header with your API key. With cURL that is one command. The API replies with an id straight away, and delivery results arrive later by webhook. The same request works from any language, and curl can also send over SMTP if you need it.

Last checked 16 November 2026

To send email using an API, you make one HTTPS request: a POST with a JSON body describing the email and an Authorization header carrying your API key. cURL is the quickest way to try it, and the same request translates directly into any language. This guide uses Mailcandor’s REST API; most email APIs follow the same pattern with different field names.

Your first email with cURL

Put your key in an environment variable so it does not end up in your shell history:

export MAILCANDOR_KEY=mc_live_xxxxxxxxxxxxxxxx

Then send:

curl -sS https://api.mailcandor.com/v1/emails \
  -H "Authorization: Bearer $MAILCANDOR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": "Acme <hello@acme.com>",
    "to": "sam@example.com",
    "subject": "Hello from the command line",
    "html": "<p>It works.</p>",
    "text": "It works."
  }'

The response comes back immediately:

{ "id": "...", "status": "queued" }

The HTTP status is 202 Accepted. That means the email is queued, not delivered. Whether it was delivered, bounced or marked as spam arrives later as a webhook that carries the same id.

The fields you can send

Field Type Notes
from string Name <address> on a verified domain
to string or list One or more recipients
cc, bcc string or list Optional
reply_to string Where replies go
subject string Required
html, text string Send one or both
headers object Custom headers, such as {"X-Entity-Ref-ID": "1042"}
tags list [{"name": "type", "value": "receipt"}] for filtering events
attachments list filename, base64 content, optional content_type
scheduled_at string ISO date or text like "in 2 hours", up to 30 days ahead

Handling errors

When something is wrong, the API returns a 4xx status and a JSON error naming the problem:

{ "error": { "type": "...", "message": "...", "field": "from" } }

Add -w '\n%{http_code}\n' to the curl command to print the status code under the body. In scripts, check the status before you trust the body. Common causes are a from address on a domain you have not verified, a malformed address, or a missing subject.

Safe retries with an idempotency key

If a request times out, you do not know whether the email was accepted. Send an Idempotency-Key header that is unique to this email:

curl -sS https://api.mailcandor.com/v1/emails \
  -H "Authorization: Bearer $MAILCANDOR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042-receipt" \
  -d '{"from":"Acme <billing@acme.com>","to":"sam@example.com","subject":"Receipt for order 1042","text":"Thanks for your order."}'

Running the same command again returns the first response instead of sending a second receipt.

Attachments

Attachments are base64 strings inside the JSON. The base64 command produces one; strip the line breaks it adds:

PDF=$(base64 < invoice-1042.pdf | tr -d '\n')

curl -sS https://api.mailcandor.com/v1/emails \
  -H "Authorization: Bearer $MAILCANDOR_KEY" \
  -H "Content-Type: application/json" \
  -d "{
    \"from\": \"Acme <billing@acme.com>\",
    \"to\": \"sam@example.com\",
    \"subject\": \"Invoice 1042\",
    \"text\": \"Your invoice is attached.\",
    \"attachments\": [{\"filename\": \"invoice-1042.pdf\", \"content\": \"$PDF\", \"content_type\": \"application/pdf\"}]
  }"

For large files, write the JSON to a file and pass it with -d @email.json instead of building it on the command line.

Sending a batch

POST /v1/emails/batch takes an array of up to 100 emails in one request. Batch emails cannot carry attachments or a schedule.

curl -sS https://api.mailcandor.com/v1/emails/batch \
  -H "Authorization: Bearer $MAILCANDOR_KEY" \
  -H "Content-Type: application/json" \
  -d '[
    {"from":"Acme <hello@acme.com>","to":"sam@example.com","subject":"Your weekly summary","text":"..."},
    {"from":"Acme <hello@acme.com>","to":"alex@example.com","subject":"Your weekly summary","text":"..."}
  ]'

Scheduling an email

Add scheduled_at with an ISO 8601 date or plain text:

-d '{"from":"Acme <hello@acme.com>","to":"sam@example.com","subject":"Your trial ends tomorrow","text":"...","scheduled_at":"in 2 hours"}'

Rate limits

Each team has a requests-per-second limit that depends on its plan. Every response includes ratelimit-* headers; add -i to curl to see them. If you get a 429, wait and retry, or move bulk sends to the batch endpoint.

Sending over SMTP with curl

curl can also speak SMTP, which is useful for testing an SMTP relay. Write the message, headers first, into a file:

From: Acme <hello@acme.com>
To: sam@example.com
Subject: SMTP test

It works over SMTP too.

Then send it over implicit TLS on port 465:

curl --url "smtps://smtp.mailcandor.com:465" \
  --user "mailcandor:$MAILCANDOR_KEY" \
  --mail-from "hello@acme.com" \
  --mail-rcpt "sam@example.com" \
  --upload-file message.txt

For port 587, use smtp://smtp.mailcandor.com:587 with --ssl-reqd, which makes curl upgrade with STARTTLS and refuse to continue without it.

Common curl mistakes

On Windows, single quotes do not delimit strings in cmd.exe; put the JSON in a file and use -d @email.json. If you get 401, check that the variable is set with echo ${#MAILCANDOR_KEY}, which prints its length without showing the key.

API or SMTP?

REST API SMTP
Request One HTTPS POST with JSON Multi-step SMTP conversation
Works through most firewalls Yes (port 443) Ports 465 or 587 must be open
Safe retries Idempotency keys Not built in
Batching and scheduling Built in No
Best for Apps you write Tools and devices that only speak SMTP

The SMTP vs API guide goes deeper. To turn the cURL request into code, see the guides for Go or Python.

Before you send: verify your domain

Receiving servers check SPF, DKIM and DMARC on every message, and mail from an unauthenticated domain is likely to land in spam. Add your domain, publish the DNS records you are given, and wait for verification. The SPF, DKIM and DMARC guide explains each one.

Questions

What do I need to send email through a REST API?

An API key, a verified sending domain, and an HTTPS client. The request is a POST with a JSON body holding the sender, recipients, subject and content.

What does a 202 response mean?

The email was accepted and queued. It does not mean it was delivered. Delivery, bounces and spam complaints are reported afterwards, usually by webhook.

How do I send an attachment with an email API?

Base64-encode the file and include it in an attachments array with its filename. The API decodes it and attaches it to the message.

Can curl send email over SMTP?

Yes. curl supports smtp:// and smtps:// URLs with --mail-from, --mail-rcpt and --upload-file pointing at a file that holds the headers and body.

Is it safe to retry a failed API request?

Only with an idempotency key. Send the same Idempotency-Key header on the retry and the API returns the first result instead of sending a duplicate.

Related

Send with someone on your side.

Unlimited domains on every plan, a person before any block, and 3,000 emails a month free. Tell us what you send and we set you up within one business day.