New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
Guides

Code examples

Use any HTTP client or SMTP library. These examples call the REST API directly, and you can swap in your preferred library.

How to integrate

There are two ways to send: the REST API over HTTPS, or standard SMTP. The API suits new code and gives structured results. SMTP suits existing applications and tools. Both authenticate with an API key.

Node.js

Uses the built-in fetch of current Node versions.

const res = await fetch("https://api.migosmtp.com/v1/send", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MIGOSMTP_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ from, to, subject, text }),
});
if (!res.ok) throw new Error(`send failed: ${res.status}`);
const { id } = await res.json();

Python

Uses the requests library.

import os, requests

r = requests.post(
    "https://api.migosmtp.com/v1/send",
    headers={"Authorization": f"Bearer {os.environ['MIGOSMTP_KEY']}"},
    json={"from": sender, "to": to, "subject": subject, "text": text},
    timeout=10,
)
r.raise_for_status()

PHP

Uses cURL, which ships with most PHP installs.

$ch = curl_init("https://api.migosmtp.com/v1/send");
curl_setopt_array($ch, [
  CURLOPT_POST => true,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("MIGOSMTP_KEY"), "Content-Type: application/json"],
  CURLOPT_POSTFIELDS => json_encode(["from" => $from, "to" => $to, "subject" => $subject, "text" => $text]),
]);
$body = curl_exec($ch);

Good habits for any language

Set a timeout on every request. Retry only on 429 and 5xx responses, with exponential backoff and jitter. Never retry a 4xx other than 429, because the request will fail the same way. Log the message id returned by each send so you can trace it later.

SMTP libraries

Any SMTP library works: Nodemailer, PHPMailer, Python smtplib, Java Mail and the SMTP support built into most frameworks. See the SMTP integration guide for connection settings.

Was this page helpful?Contact support

Start sending in minutes.

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

Get started Talk to sales