Framework guides
Every framework can reach ScrubiMail two ways: the HTTP API with one fetch or requests call, or the SMTP relay with the mail settings the framework already has. Pick the one that needs less change in your app.
Related:Email sendingSMTP relayTransactional email
Checked against the API source on
HTTP API or SMTP?
| HTTP API | SMTP relay | |
|---|---|---|
| Best when | You write the sending code yourself, or want {{tags}}, tags and templates. | The framework or a plugin already sends mail and you only want to change the transport. |
| Credentials | X-API-Key header with a key that has the send scope. | Username is your account email, password is an API key with the send scope. |
| Personalisation tags | Rendered for you. | Not rendered. Send the finished message. |
| Unsubscribe link | Added when you set list_unsubscribe or use {{unsubscribe_url}}. | Not added. Add your own List-Unsubscribe header for marketing mail. |
| Response | 202 with a message_id you can poll. | The SMTP reply. The message appears in GET /send/history/ with metadata.source set to smtp. |
For both you need a verified domain first: see the Quickstart or Sending setup. The From address must be on that domain. The HTTP examples below read three environment variables: SCRUBIMAIL_API_KEY, SCRUBIMAIL_DOMAIN_ID (the id of your domain from GET /send/domains/) and, in the SMTP examples, SCRUBIMAIL_ACCOUNT_EMAIL. The API base URL is https://api.scrubimail.com/scrubimail/api/v1.
| Setting | Value |
|---|---|
| Host | smtp.scrubimail.com |
| Port | 587 |
| Encryption | STARTTLS (not SSL/TLS on 465) |
| Username | Your ScrubiMail account email |
| Password | An API key with the send scope |
Next.js
Use a route handler for an endpoint your front end calls, or a server action for a form. Both run on the server, so the key stays private. These examples use the App Router.
import { NextResponse } from "next/server";
export async function POST(request: Request) {
const { email, name } = await request.json();
const res = await fetch("https://api.scrubimail.com/scrubimail/api/v1/send/", {
method: "POST",
headers: {
"X-API-Key": process.env.SCRUBIMAIL_API_KEY!,
"Content-Type": "application/json",
},
body: JSON.stringify({
sending_domain_id: process.env.SCRUBIMAIL_DOMAIN_ID,
from_email: "[email protected]",
from_name: "Your Company",
to_email: email,
to_name: name,
subject: "Welcome, {{first_name|there}}!",
html_content: "<p>Hi {{first_name|there}}, thanks for signing up.</p>",
}),
});
const data = await res.json();
if (!res.ok) {
return NextResponse.json({ error: data.error?.message ?? data.error }, { status: res.status });
}
return NextResponse.json({ messageId: data.message_id }, { status: 202 });
}A server action can validate the address first and only then send, which keeps dead mailboxes out of your list. mode=fast returns in well under a second but never confirms a mailbox, so use it to reject typos and disposable addresses at the form, and the default deep check when you need a yes or no.
"use server";
const BASE = "https://api.scrubimail.com/scrubimail/api/v1";
const headers = {
"X-API-Key": process.env.SCRUBIMAIL_API_KEY!,
"Content-Type": "application/json",
};
export async function signUp(formData: FormData) {
const email = String(formData.get("email") ?? "");
// 1. Is this address real? (1 validation credit)
const check = await fetch(BASE + "/validate/", {
method: "POST",
headers,
body: JSON.stringify({ email }),
});
const result = await check.json();
if (check.ok && !result.is_valid) {
return { error: "That email address does not look deliverable." };
}
// 2. Send the welcome email (1 sending credit)
const send = await fetch(BASE + "/send/", {
method: "POST",
headers,
body: JSON.stringify({
sending_domain_id: process.env.SCRUBIMAIL_DOMAIN_ID,
from_email: "[email protected]",
from_name: "Your Company",
to_email: email,
subject: "Confirm your email",
html_content: "<p>Welcome aboard.</p>",
}),
});
if (!send.ok) return { error: "We could not send the email. Try again." };
return { ok: true };
}is_valid is true only when the mailbox was confirmed. Some real addresses come back as unknown or catch-all because the receiving server will not say; decide per verdict whether to accept them. See verdicts.
Node.js and Express
Node 18 and later has fetch built in, so no SDK is needed.
import express from "express";
const app = express();
app.use(express.json());
app.post("/password-reset", async (req, res) => {
const response = await fetch("https://api.scrubimail.com/scrubimail/api/v1/send/", {
method: "POST",
headers: {
"X-API-Key": process.env.SCRUBIMAIL_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
sending_domain_id: process.env.SCRUBIMAIL_DOMAIN_ID,
from_email: "[email protected]",
from_name: "Your Company",
to_email: req.body.email,
subject: "Reset your password",
html_content: '<p><a href="' + req.body.resetUrl + '">Reset your password</a></p>',
}),
});
if (response.status === 429) {
// Daily sending limit or rate limit: Retry-After is in seconds.
res.set("Retry-After", response.headers.get("Retry-After") ?? "60");
return res.status(503).json({ error: "Try again shortly." });
}
if (!response.ok) return res.status(502).json({ error: "Could not send email." });
res.status(202).json({ ok: true });
});
app.listen(3000);Nodemailer
Nodemailer needs secure: false because the connection starts in plain text and upgrades with STARTTLS. requireTLS: true refuses to send if the upgrade fails.
import nodemailer from "nodemailer";
export const transporter = nodemailer.createTransport({
host: "smtp.scrubimail.com",
port: 587,
secure: false,
requireTLS: true,
auth: {
user: process.env.SCRUBIMAIL_ACCOUNT_EMAIL,
pass: process.env.SCRUBIMAIL_API_KEY,
},
});
await transporter.sendMail({
from: '"Your Company" <[email protected]>',
to: "[email protected]",
subject: "Your receipt",
text: "Thanks for your order.",
html: "<p>Thanks for your order.</p>",
});If sendMail rejects with a 550 5.7.1 response, the text after the code names the cause, for example a From domain that is not verified. See the SMTP error codes.
Django
SMTP backend
Django's built-in SMTP backend works unchanged. Every send_mail, EmailMessage and password-reset email then goes through ScrubiMail.
import os
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.scrubimail.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True # STARTTLS. Leave EMAIL_USE_SSL unset (False).
EMAIL_HOST_USER = os.environ["SCRUBIMAIL_ACCOUNT_EMAIL"]
EMAIL_HOST_PASSWORD = os.environ["SCRUBIMAIL_API_KEY"]
DEFAULT_FROM_EMAIL = "Your Company <[email protected]>"
SERVER_EMAIL = DEFAULT_FROM_EMAIL # error emails to ADMINS must also come from a verified domainHTTP API
Use the API when you want {{tags}}, tags or the message id back. A small helper keeps the call in one place.
import os
import requests
BASE = "https://api.scrubimail.com/scrubimail/api/v1"
def send_email(to_email, subject, html, to_name=""):
res = requests.post(
BASE + "/send/",
headers={"X-API-Key": os.environ["SCRUBIMAIL_API_KEY"]},
json={
"sending_domain_id": os.environ["SCRUBIMAIL_DOMAIN_ID"],
"from_email": "[email protected]",
"from_name": "Your Company",
"to_email": to_email,
"to_name": to_name,
"subject": subject,
"html_content": html,
},
timeout=30,
)
res.raise_for_status()
return res.json()["message_id"]Laravel
SMTP mail driver
Set the mail variables in .env. Laravel's Mail::to(...)->send(...), notifications and queued mail all use them.
MAIL_MAILER=smtp
MAIL_HOST=smtp.scrubimail.com
MAIL_PORT=587
MAIL_ENCRYPTION=tls
[email protected]
MAIL_PASSWORD=your_scrubimail_api_key
[email protected]
MAIL_FROM_NAME="Your Company"MAIL_ENCRYPTION=tls means STARTTLS on port 587. Do not use ssl or port 465. After changing .env, run php artisan config:clear if the config is cached.
HTTP API
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class ScrubiMail
{
public static function send(string $to, string $subject, string $html): string
{
$response = Http::withHeaders(['X-API-Key' => config('services.scrubimail.key')])
->timeout(30)
->post('https://api.scrubimail.com/scrubimail/api/v1/send/', [
'sending_domain_id' => config('services.scrubimail.domain_id'),
'from_email' => '[email protected]',
'from_name' => 'Your Company',
'to_email' => $to,
'subject' => $subject,
'html_content' => $html,
])
->throw();
return $response->json('message_id');
}
}Add 'scrubimail' => ['key' => env('SCRUBIMAIL_API_KEY'), 'domain_id' => env('SCRUBIMAIL_DOMAIN_ID')] to config/services.php.
Ruby on Rails
Action Mailer talks SMTP, so point it at the relay. Rails negotiates STARTTLS on port 587 automatically.
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: "smtp.scrubimail.com",
port: 587,
authentication: :plain,
user_name: ENV["SCRUBIMAIL_ACCOUNT_EMAIL"],
password: ENV["SCRUBIMAIL_API_KEY"],
enable_starttls_auto: true,
}class ApplicationMailer < ActionMailer::Base
default from: "Your Company <[email protected]>"
endSet raise_delivery_errors to true while you set up so a 535 or 550 5.7.1 shows up as an exception instead of a silent drop. Keep in mind that Rails views are rendered by Rails, not by ScrubiMail: {{tags}} are not applied over SMTP.
WordPress
WordPress sends with PHP's mail() unless a plugin replaces it. Install an SMTP plugin (WP Mail SMTP, FluentSMTP, Post SMTP and similar), choose the generic "Other SMTP" or custom provider option, and enter these values. Labels vary a little between plugins.
| Plugin field | Value |
|---|---|
| SMTP host | smtp.scrubimail.com |
| Encryption | TLS (STARTTLS) |
| SMTP port | 587 |
| Authentication | On |
| SMTP username | Your ScrubiMail account email |
| SMTP password | An API key with the send scope |
| From email | An address on your verified domain, for example [email protected] |
| Force from email | On, so plugins that set their own From address do not get refused |
Use the plugin's test email to check the setup. If a plugin or form sends from an address on a domain you have not verified, the relay refuses it with 550 5.7.1. WordPress and its plugins do not add an unsubscribe link, so use a dedicated newsletter tool for marketing mail, or the Sending API with list_unsubscribe.
If it does not work
- `535` on login. The username must be your account email, and the password an active API key with the
sendscope. See SMTP error codes. - `550 5.7.1`. The From domain (or envelope sender) is not a verified sending domain on your account, or the recipient is suppressed, or you are out of credits. The text after the code says which.
- `400` from the HTTP API.
from_emailmust be on the domain you pass insending_domain_id, and that domain must be verified. - `429` with `code: "daily_send_limit"`. You reached your plan's daily sending limit. See Limits and quotas.
- Connection timeout. Your host blocks outbound port 587. Use the HTTP API, which only needs HTTPS.
Something missing or wrong? Email [email protected].