research/sending-email-over-an-http-api-instead-of-smtp

Sending Transactional Email Over an HTTPS API Instead of SMTP: Round Trips, Timeouts and Duplicates Tested

Engineering Note · December 3, 2026 · 9 minute read · Email · Node.js · Reliability · Forms

For the email a website sends on its own (a booking confirmation, a receipt, a “new enquiry” alert), an HTTPS API is usually the simpler road. In our local test, sending one message over SMTP took seven server replies, so any delay between the app and the mail server was paid seven times; the same message over an HTTP API was one request. The bigger point is shared by both: when a server stores a message and its reply is late, the sender times out, retries and the message goes out twice. An Idempotency-Key header let our HTTP stub recognize the retry and keep one copy. Plain SMTP has nothing like it, and our SMTP sink stored two.

Everything below was run on Linux 6.18 with Node.js 22.23.5, against a small SMTP sink and a small HTTP “send email” server, both written for this test and running on the same machine. No real mail provider was called, and no TLS was involved. The outputs are real.

What are the two ways to hand off a message?

SMTP (Simple Mail Transfer Protocol) is how mail servers talk to each other, and most email providers also accept it from apps, usually on port 587 with a username and password. It is a back-and-forth conversation of short text commands.

An HTTP email API is a provider’s web endpoint: you POST a JSON body with the sender, recipient, subject and content, plus an API key, and get back a message ID. It travels on port 443 like any other web request.

Both end up in the same place. The provider queues the message and delivers it to the recipient’s mail server over SMTP either way. The choice is only about the hop between your code and the provider, which is the hop that breaks when your form handler has a bug or a bad day.

How many round trips does each one take?

We wrote the smallest SMTP client that could send a message: send a command, wait for the reply, send the next.

const steps = [null, `EHLO app.local`, `MAIL FROM:<${from}>`, `RCPT TO:<${to}>`, 'DATA', data + '\r\n.', 'QUIT'];
// on each reply: stop on a 4xx/5xx code, otherwise send the next step

The sink answers each command and can wait a set number of milliseconds before every reply, which stands in for network distance. The HTTP stub waits the same amount before its one reply. Then we timed one send each way at three delays:

$ node run-latency.mjs
added delay  0 ms per reply | SMTP: 7 replies, 14 ms | HTTP API: 1 reply, 26 ms
added delay 20 ms per reply | SMTP: 7 replies, 150 ms | HTTP API: 1 reply, 25 ms
added delay 80 ms per reply | SMTP: 7 replies, 565 ms | HTTP API: 1 reply, 85 ms

At zero added delay the HTTP call was slower, because it was the first fetch() in the process and paid for setting up Node’s HTTP client. Once each reply carries any delay, the count of replies decides it: seven waits against one.

A real connection adds more on both sides. An SMTP submission on port 587 normally upgrades to encryption with STARTTLS and then repeats EHLO, and an HTTPS request pays for its TLS handshake. Neither was part of this test. A client that keeps connections open and sends many messages per session pays the SMTP setup once, which narrows the gap for bulk sending. For a form handler that sends one message and exits, the conversation length is the cost.

What happens when the reply is late?

This is the failure that matters more than speed. A message has three states from the sender’s point of view: not sent, sent, and “I asked and never heard back.” In the third state the sender cannot tell whether the server stored it.

RFC 5321, the SMTP standard, names the problem in section 6.1: a receiving server “MUST seek to minimize the time required to respond to the final . end of data indicator” to avoid duplicate messages caused by timeouts, and it points to RFC 1047, which traces duplicate mail to exactly this gap.

We reproduced it. Both test servers store the message at once and then wait 1.5 seconds before saying so. The client gives up after 1 second and retries once with a 2.5-second timeout:

// The server stores the message, then takes 1.5 s to say so.
// The client gives up at 1 s, then retries once with a 2.5 s timeout.
async function attemptTwice(label, send) {
  for (let attempt = 1; attempt <= 2; attempt++) {
    try { const r = await send(attempt === 1 ? 1000 : 2500); console.log(`  ${label} attempt ${attempt}: ok ${r.id ?? ''}`); return; }
    catch (e) { console.log(`  ${label} attempt ${attempt}: ${e.name === 'TimeoutError' ? 'timeout' : e.message}`); }
  }
}
$ node run-duplicate.mjs
  SMTP attempt 1: timeout after step 5
  SMTP attempt 2: ok 
  SMTP sink stored 2 message(s)
  API, no key attempt 1: timeout
  API, no key attempt 2: ok msg_2
  API stored 2 message(s)
  API, same key attempt 1: timeout
  API, same key attempt 2: ok msg_1
  API stored 1 message(s)

“Timeout after step 5” means the SMTP client was waiting for the reply to the final dot, the exact moment RFC 1047 describes. The retry worked, and the customer would have received the confirmation twice.

The HTTP call without a key did the same thing. Switching to HTTP does not fix duplicates on its own.

The third run sent an Idempotency-Key header with a random UUID, and the same key on the retry. The stub keeps a map from key to message ID. On the retry it found the key, skipped storing and answered with the first message’s ID, msg_1. One message stored, and the sender got a real ID to log.

Where does the idempotency key come from?

The key must be created once per message you intend to send, before the first attempt, and reused on every retry of that message. A new key per attempt is the same as no key.

The natural place is the record that caused the email. For a booking form, use the booking’s ID or a UUID saved with the booking: “confirmation for booking 1042” is one message no matter how many times the sender retries. That is the same idea we used on the form side in idempotency keys for a booking form, applied one hop further down.

The header is described in an IETF Internet-Draft, draft-ietf-httpapi-idempotency-key-header, at version 09 (published November 21, 2026) and not yet a standard as of December 2, 2026. It recommends a UUID as the key, a 409 reply when a retry arrives while the first request is still being processed, and a 422 when a key is reused with a different body. Providers that support idempotency each document their own header name and how long they remember a key, so check the one you use. We did not test any provider’s implementation here.

Which failures should be retried?

Both protocols tell you whether an error is worth retrying, if the code reads the reply instead of treating every failure the same:

ReplySMTP (RFC 5321)HTTP APIWhat the sender should do
Accepted250 after the final dot200/202 with a message IDLog the ID and stop
Temporary4yz, e.g. 421, 451429 (RFC 6585), 500, 502, 503, 504Retry with backoff, honoring Retry-After on HTTP
Permanent5yz, e.g. 550, 553400, 401, 403, 422Do not retry; fix the request or the credentials
No replyTimeout or dropped connectionTimeout or dropped connectionRetry only if a duplicate is acceptable, or with an idempotency key

RFC 5321 defines a 4yz reply as one where “the error condition is temporary, and the action may be requested again,” and says a client “SHOULD NOT repeat the exact request” after a 5yz. RFC 6585 defines 429 Too Many Requests as rate limiting and allows a Retry-After header. Our note on retrying a flaky HTTP call with a deadline has a tested helper for the backoff side.

The last row is the one most code gets wrong. A timeout is not a failure; it is an unknown. Over SMTP, the honest choices are to retry and accept the occasional duplicate, or to not retry and accept the occasional missing message. Over an HTTP API with idempotency, there is a third choice that avoids both.

When is SMTP still the better choice?

  • The tool only speaks SMTP. Many off-the-shelf apps, printers and older CMS plugins have an SMTP settings screen and nothing else.
  • You want to switch providers by changing settings. SMTP is the same everywhere. An API means a code change, or a small wrapper, to move providers.
  • A duplicate costs little. A “new enquiry” alert to the owner that occasionally arrives twice is a nuisance. A payment receipt is more worth protecting.

And when the code runs somewhere that limits outbound connections, the choice may be made for you. Cloudflare’s documentation for the Workers TCP sockets API says Workers cannot create outbound connections on port 25 and suggests Email Workers instead. An HTTPS call to an email API is an ordinary fetch() there.

Whichever you use, save the record first and send the email separately, as in isolating failures in a form pipeline, and test that the message is actually sent with a local SMTP sink or a stub like the one above.

Limitations

  • Both servers were written for this test and ran on the same machine. They show the protocol shape, not how any real provider behaves under load.
  • The “network delay” was a pause before each server reply. It did not delay TCP setup, and there was no TLS, STARTTLS or authentication, all of which add round trips in real use.
  • The SMTP client sends one message per connection. Clients that keep a connection open and send many messages pay the setup once, and SMTP pipelining (RFC 2920) can batch some commands; we tested neither.
  • The idempotency behavior is our stub’s, following the IETF draft. Providers differ in header names, how long keys are kept and what they return for a repeat, and we did not test any of them.
  • Duplicates were produced with a deliberately slow acknowledgment. How often this happens in production depends on the provider and the network, and we have no numbers for that.

Sources

Frequently asked questions

Is it better to send transactional email over an API or SMTP?

For a website or app sending receipts, booking confirmations and form notifications, an HTTPS API is usually simpler. It is one request per message, it travels on port 443 like any other web call, and many providers accept an idempotency key so a retry does not send twice. SMTP is still the right choice when a tool only speaks SMTP, or when you want to switch providers by changing a host name and password.

Why are SMTP sends slower than API sends?

SMTP is a conversation. The client sends a command, waits for a reply, then sends the next one: greeting, EHLO, MAIL FROM, RCPT TO, DATA, the message and QUIT. In our local test that was seven replies per message, so a delay added to each reply was paid seven times. An HTTP API call is one request and one reply.

Can a retry send the same email twice?

Yes, over both SMTP and HTTP. If the server has stored the message but its reply is late or lost, the sender sees a timeout and tries again. RFC 5321 names this problem and asks receiving servers to answer the final dot quickly to make it rare. In our test both a plain SMTP retry and an HTTP retry without a key stored the message twice.

What is an Idempotency-Key header?

A random value, such as a UUID, that the client sends with a request and repeats on every retry of that same request. A server that supports it stores the first result under that key and returns it again for a retry instead of acting twice. It is described in an IETF Internet-Draft that is still in progress as of December 2026, and several API providers support their own version of it.

Can Cloudflare Workers send email over SMTP?

Not directly on port 25. Cloudflare's documentation for the Workers TCP sockets API says Workers cannot open outbound connections on port 25 and points to Email Workers instead. An HTTPS email API call from a Worker avoids that limit.

More from Research

← All research