Send Email with an API: Code Examples in TypeScript, Python, C#, Go, Rust, PHP, Java, Ruby and Swift
How to send email with an API from any language: one HTTP call or an SDK for TypeScript, Python, C#, Go, Rust, PHP, Java, Ruby or Swift, with replies included.

To send email with an API, your code makes one HTTPS request with a sender, a recipient, a subject and a body, and the email service delivers it. No mail server to run, no SMTP ports or TLS settings, and it works the same from every language. This guide shows that request with Mailcycle, then links to a full, tested tutorial for each language we publish an SDK for: TypeScript, Python, C#, Go, Rust, PHP, Java, Ruby and Swift.
Mailcycle is also two-way. The address you send from is a real inbox, so replies come back to the same API, which a send-only email service doesn’t do.
Quick answer: send an email with one API call
Send a POST request to https://api.mailcycle.email/messages/send with your API key as a Bearer token and a JSON body naming from (an address on your account), to, subject and text. Mailcycle sends it and returns the Message-ID it went out with.
curl https://api.mailcycle.email/messages/send \
-H "Authorization: Bearer $MAILCYCLE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"from": "receipts-x7k2@example-pool.email",
"to": "someone@example.com",
"subject": "Your order has shipped",
"text": "Order 1042 left our warehouse today."
}'
{ "sent": true, "messageId": "<…>", "sentCopyId": "msg_…" }
That’s the whole protocol. Everything below is detail: HTML, attachments, replies, errors, and the same call in your language.
Send email in your language
Pick your language for a complete tutorial: install, a framework endpoint, HTML email, attachments, error handling and reading the reply. Every SDK implements the same encryption format, so mail one of them seals opens in any other and in the Mailcycle apps.
| Language | Tutorial | Install | SDK docs |
|---|---|---|---|
| TypeScript / JavaScript | Send email from Node.js and TypeScript | npm install mailcycle-sdk |
TypeScript SDK |
| Python | Send email from Python | pip install mailcycle |
Python SDK |
| C# / .NET | Send email from C# and ASP.NET Core | dotnet add package Mailcycle |
.NET SDK |
| Go | Send email from Go | go get mailcycle.email/go |
Go SDK |
| Rust | Send email from Rust | cargo add mailcycle |
Rust SDK |
| PHP | Send email from PHP and Laravel | composer require mailcycle/mailcycle |
PHP SDK |
| Java | Send email from Java and Spring Boot | email.mailcycle:mailcycle |
Java SDK |
| Ruby | Send email from Ruby and Rails | gem install mailcycle |
Ruby SDK |
| Swift | Send email from Swift | Swift Package Manager | Swift SDK |
The PHP package comes from Mailcycle’s own Composer repository, so add it first with composer config repositories.mailcycle composer https://mailcycle.email/php. The SDKs page on our site has an overview of each, and the developers page shows the API as a whole.
Using a language without an SDK? Plain HTTP works for sending, as in the cURL example above. The SDKs earn their place on the receiving side: an address only receives mail once it has a public key derived from your recovery phrase, and the SDKs derive it, and open what arrives, on your machine. See Email addresses.
What is an email API?
An email API is a web service that sends (and, with Mailcycle, receives) email on your behalf over HTTPS. Instead of opening an SMTP connection to a mail server, logging in, and speaking the SMTP protocol, your code sends one JSON request and gets one JSON response.
That shift matters in practice:
- No mail server. You don’t run Postfix, rent an SMTP relay, or keep a mailbox password in your app’s config.
- No blocked ports. Many cloud hosts restrict outbound SMTP ports. An email API only needs outbound HTTPS on 443.
- Errors you can act on. A failed send comes back as an HTTP status and a stable error code like
invalid_addressorrate_limited, not an SMTP reply string to parse. - The same call from every language. HTTPS and JSON look the same in Python, Go or Swift, so a team with several services sends mail one way.
SMTP vs an email API
| SMTP library | Mailcycle email API | |
|---|---|---|
| What you configure | Host, port, TLS mode, username, password | One API key |
| Network needs | Outbound SMTP ports, often 587 or 465 | Outbound HTTPS (443) |
| Code | A protocol library per language | One HTTPS call, or an SDK |
| Errors | SMTP reply codes and text | HTTP status plus a named error code |
| Replies | Need a separate IMAP or POP3 setup | Arrive in the same account, readable through the same API |
| Sent copy | Up to your server | Filed in the address’s Sent folder, encrypted |
| Attachments and HTML | Build the MIME message yourself | JSON fields: html, attachments |
SMTP is still the right tool when existing software only speaks SMTP. For code you’re writing now, an API is simpler to run and simpler to debug.
How sending works in Mailcycle
Every language goes through the same endpoint, POST /messages/send. Its fields, from the sending mail guide:
| Field | What it’s for |
|---|---|
from |
Required. An address on your account. Any other returns 403 not_your_address. |
to |
Required. One recipient per call. |
subject |
Required. Up to 998 characters. |
text |
Required. The plain-text body. |
html |
Optional. The HTML body, shown by mail clients that render HTML. |
attachments |
Optional. Up to 10 files, base64-encoded. |
inReplyTo |
Optional. The Mailcycle id of the message you’re answering. |
inReplyToHeader |
Optional. That message’s Message-ID, so the reply threads in the recipient’s mail client. |
fromName |
Optional. A display name, like Support at Acme. |
The response gives you messageId, the Message-ID header the email went out with, and sentCopyId, the id of the copy kept in the address’s Sent folder. Sent copies are sealed to the address’s public key before they’re stored, like received mail.
Send HTML email with an API
Always send text and add html alongside it. Mail clients that render HTML show the HTML part; the rest, and many screen readers and spam filters, read the text part.
{
"from": "receipts-x7k2@example-pool.email",
"to": "someone@example.com",
"subject": "Your receipt",
"text": "Thanks for your order. Total: £42.00",
"html": "<h1>Thanks for your order</h1><p>Total: <strong>£42.00</strong></p>"
}
Send an email with an attachment through an API
Attachments are JSON objects with the file’s bytes base64-encoded. A contentId turns a file into an inline image that the HTML references with cid:.
{
"from": "receipts-x7k2@example-pool.email",
"to": "someone@example.com",
"subject": "Invoice 1042",
"text": "Your invoice is attached.",
"attachments": [
{ "filename": "invoice-1042.pdf", "mimeType": "application/pdf", "content": "JVBERi0xLjQK…" }
]
}
Each language tutorial shows how to read a file and encode it idiomatically, from File.ReadAllBytesAsync in C# to base64.StdEncoding in Go.
Set up once, send from any language
These steps are the same whichever language you write in.
-
Create an account at app.mailcycle.email. There’s no email or password: your account is a 12-word recovery phrase made on your device. Save it somewhere safe.
-
Start the free trial. In the app, open Plans & Billing and choose Start free trial. Sending with an API key is part of the Scale plan, and the 7-day Scale trial includes it without a card.
-
Create an API key. Open API, then the Keys tab, name the key and choose Create key. Copy the token, which starts with
mak_and is shown once, into an environment variable:export MAILCYCLE_API_KEY="mak_..." -
Create an address to send from. Roll a name like
maya.holt@in the app, or create one from an SDK. On Scale you can also type any name, likesupport@, and use your own domain. -
Send. Use the cURL call above, or follow the tutorial for your language.
Check the key works before writing any code:
curl https://api.mailcycle.email/accounts/me \
-H "Authorization: Bearer $MAILCYCLE_API_KEY"
The response names your account and its plan. Keep the key on your server: anyone holding it can send as your addresses. Authentication explains what a key can and can’t do.
Receive replies with the same API
Most email APIs stop at sending. When a customer answers your shipping notice, that reply goes somewhere else: a support mailbox, a no-reply black hole, or a separate inbound-parsing product.
With Mailcycle, the address you sent from is a real inbox. Replies arrive in the same account, and your code can:
- List and open messages with the SDK. Mail is stored encrypted, and the SDK opens it on your machine with keys derived from your recovery phrase. See Reading mail.
- Get a webhook when mail arrives, to wake a worker or a queue. See Webhooks.
- Listen on the event stream for new mail the moment it lands. See Event stream.
- Reply in the thread by passing
inReplyToandinReplyToHeader, so the conversation stays together in the recipient’s mail client.
That loop (send, wait for the answer, read it, reply) is what makes the API useful for sign-in and verification flows, support tooling, and AI agents.
Handle email API errors
Errors come back as an HTTP status and a named code. The ones you’ll meet when sending:
| Status and code | What it means | What to do |
|---|---|---|
400 invalid_address |
The to address isn’t valid |
Fix the input; don’t retry |
403 not_your_address |
from isn’t an address on your account |
Use one of your addresses |
403 plan_required |
The account’s plan doesn’t include sending with an API key | Start the Scale trial or choose Scale |
409 domain_not_sending |
Sending isn’t on yet for your own domain | Check the domain under Domains |
413 attachments_too_large |
The files are too big | Send smaller files or a link |
429 rate_limited |
Too many requests | Wait for Retry-After seconds, then retry |
429 daily_send_limit |
The plan’s daily sending limit is reached | Retry after 00:00 UTC |
The full list is in Errors, and the numbers behind the 429s are in Rate limits. Every SDK surfaces the code as a typed error, and each language tutorial shows how to branch on it.
Which plan sends from the API
Sending is gated by plan, and the rule is simple. A signed-in session can send from Personal up, and an API key can send on Scale and up. Scale is $19 a month and allows 250 sends a day, and it adds the full API and 10 custom domains, so your code can send from support@yourdomain instead of a shared domain. Free receives only. If your backend only needs to receive and read mail, Operator at $15 a month gives API keys and webhooks without sending from a key. The pricing page lists every plan side by side.
Planning for replies and threading
Each call carries one recipient, which suits the mail people expect and answer: receipts, replies and account notices. For replies, pass both inReplyTo and inReplyToHeader, so the message appears under the original in the recipient’s mail client. Keep a plain-text body even when you send HTML, because it is what a client falls back to and what search previews show.
Mail you send from an address of your own also lands in that address’s Sent folder, sealed like received mail, so a support team can see what went out.
What to build with an email API
Mailcycle sends from addresses you own, one recipient per call, which fits mail people expect and may answer:
- Transactional email: receipts, shipping notices, password resets and alerts from your backend.
- Sign-in and verification flows: send a code, then read the user’s reply or a service’s confirmation in the same inbox.
- Support and operations: a
support@address your code can triage, answer and file. - AI agents: give an agent its own address so it can wait for mail, read it and reply. Claude Code, Claude Desktop, Cursor or any other MCP client can use the Mailcycle MCP server. Start with how to give Claude Code its own email address, or see AI agents.
Next steps
- Pick your language from the table above and follow the full tutorial.
- Read the sending mail guide and the quickstart.
- Compare plans on the pricing page.
FAQ
What is an email API?
An email API is an HTTP endpoint that sends email for you. Your code makes one authenticated request with the sender, recipient, subject and body, and the provider handles the mail servers, TLS and delivery. With Mailcycle that request is POST /messages/send, and the same account also receives the replies.
Is an email API better than SMTP?
For application code, usually yes. SMTP needs a mail server, credentials, the right port and TLS settings, and a library that speaks the protocol; an API is one HTTPS request that works from any language and any host that allows outbound HTTPS. SMTP still makes sense when existing software only speaks SMTP.
Which programming languages can send email with Mailcycle?
Any language that can make an HTTPS request can send with POST /messages/send. Mailcycle also publishes SDKs for TypeScript, Python, C# and .NET, Go, Rust, PHP, Java, Ruby and Swift, which also open the mail that arrives.
How do I send an email with cURL?
Send a POST to https://api.mailcycle.email/messages/send with an Authorization: Bearer header holding your API key and a JSON body with from, to, subject and text. The response has sent, the messageId header the mail went out with, and sentCopyId for the copy in the Sent folder.
Can I send HTML email and attachments through the API?
Yes. Add html next to text, and up to 10 files in attachments, each with a filename, an optional mimeType and base64 content. A file with a contentId becomes an inline image for <img src="cid:…">. See Sending mail.
Can I receive email replies through the same API?
Yes, and that's the difference from send-only APIs. Every address on your account receives mail, and you can list and open messages, get a webhook when one arrives, or listen on the event stream. Mail is stored encrypted, and the SDKs open it on your machine.
Which plan do I need to send email with an API key?
Sending with an API key is part of the Scale plan and above. The free 7-day Scale trial includes it, with no card needed. See pricing for what each plan includes.
Can I send email from my own domain?
Yes. Add your domain under My domains in the app; Scale includes up to 10 of your own domains. Once sending is on for the domain, any address on it can be the from. See Your own domains.
Can an AI agent send and receive email with this API?
Yes. An agent gets its own addresses and credential, and starts reply-only so mail it reads can't send it anywhere else. Claude Code, Claude Desktop, Cursor or any other MCP client can connect through the Mailcycle MCP server; see how to give Claude Code its own email address.
Send email from any language
- How to Send Email from C# and an ASP.NET Core Web API (C# / .NET)
- How to Send Email from Go (Golang): HTML Templates, a Mailer Package and an API (Go)
- How to Send Email from Java: A Plain Java Program, Spring Boot, HTML, Attachments and Replies (Java / Spring Boot)
- How to Send Email from PHP with an API (Laravel and Plain PHP) (PHP / Laravel)
- How to Send Email from Python with an API: Scripts, FastAPI and Django (Python)
- How to Send Email from Ruby and Rails with an Email API (Ruby / Rails)
- How to Send Email from Rust with an API: HTML, Attachments, Axum and Replies (Rust)
- How to Send Email From Swift: Vapor, iOS and Server-Side Swift (Swift / Vapor)
- How to Send Email from Node.js and TypeScript (Express and Next.js) (TypeScript / Node.js)