Node.js Email Delivery: SMTP Relay vs Email API
In this article
For a Node.js app, an email API (like SendGrid, Postmark, or Amazon SES) is almost always the better choice than a direct SMTP relay for sending transactional email, because it offers better deliverability, built-in analytics, and simpler setup. However, an SMTP relay can be the right option if you already run your own mail server or need to send via an existing corporate SMTP service. This article explains the key differences, costs, and how to choose.
Background: How Email Sending Works in Node.js
In Node.js, sending email typically involves one of two paths:
- Direct SMTP: Your app connects to an SMTP server (either your own or a third-party relay) and issues SMTP commands to send the message. The
nodemailerlibrary is the standard tool for this. - Email API: Your app makes an HTTP request (usually REST or JSON) to a service that handles the SMTP delivery on your behalf. The service exposes an endpoint like
POST /v1/mail/sendand returns a JSON response.
Both methods ultimately deliver email over SMTP, but the difference is who manages the infrastructure, reputation, and retries.
How SMTP Relay Works
An SMTP relay is a server that accepts your outgoing email and forwards it to the recipient’s mail server. In Node.js, you configure a transport with nodemailer:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.yourprovider.com',
port: 587,
auth: { user: 'user', pass: 'pass' }
});
await transporter.sendMail({
from: 'you@example.com',
to: 'user@example.com',
subject: 'Hello',
text: 'Test'
});The relay handles the SMTP conversation, but you are responsible for monitoring bounces, maintaining your sending reputation, and handling retries. If you use a shared relay (like your ISP’s SMTP server), you may be limited in volume and subject to throttling.
How Email APIs Work
An email API abstracts away SMTP entirely. You send an HTTP request with the message details, and the provider takes care of the rest. A typical Node.js call using the fetch API:
const response = await fetch('https://api.sendgrid.com/v3/mail/send', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
personalizations: [{ to: [{ email: 'user@example.com' }] }],
from: { email: 'you@example.com' },
subject: 'Hello',
content: [{ type: 'text/plain', value: 'Test' }]
})
});Most providers offer official Node.js SDKs, so you don’t even need to write raw HTTP. The provider handles retries, bounce processing, and often provides a webhook to notify your app of delivery events.
Key Differences: Deliverability, Features, and Control
- Deliverability: Email APIs have established IP reputations and often use dedicated IPs (at a cost) or shared pools that are carefully managed. SMTP relays from your own server may have poor reputation if you send from a residential IP or a new domain.
- Features: APIs provide open/click tracking, bounce categorization, spam reporting, and template management. SMTP relays typically only offer basic sending, with no built-in analytics.
- Control: With SMTP, you control the exact headers and envelope, which is useful for advanced use cases like sending via a specific IP or using custom DKIM signing. APIs may limit some low-level options.
- Setup: SMTP requires you to configure DNS records (SPF, DKIM, DMARC) yourself. APIs often provide these records for you to add, but they handle the actual signing.
Cost Comparison
Email APIs are usually priced per thousand emails sent, with a free tier (e.g., 100 emails/day on SendGrid, 100/day on Postmark’s trial). SMTP relays are often included with your web hosting or domain provider, but may have daily limits. If you send high volume, APIs can get expensive; running your own SMTP server has infrastructure costs (VPS, maintenance) but no per-email fee. For small projects, the free tiers of APIs are more than enough, and the time saved is valuable.
How to Choose: A Decision Guide
- If you are just starting or sending under 10,000 emails/month: Use an email API. The free tier is sufficient, and you get analytics out of the box.
- If you already have a corporate SMTP server (e.g., Office 365, Gmail SMTP): Use SMTP relay via nodemailer. It’s simpler than migrating to an API.
- If you need advanced deliverability features like dedicated IP, webhooks, or A/B testing: Choose an email API.
- If you have a dedicated server and want full control: Set up your own SMTP relay (e.g., Postfix) and use nodemailer. Be prepared to handle bounces and blacklists.
Common Problems and Misconceptions
Misconception: SMTP is always cheaper. Not if you count your time and infrastructure costs. A VPS for Postfix costs money, and you must monitor deliverability. APIs have predictable pricing.
Problem: Emails going to spam. This is often due to missing SPF/DKIM records. With an API, you add their records; with your own SMTP, you must set them up correctly. Also, avoid sending from a free email address.
Problem: Rate limiting. SMTP relays may throttle you, causing timeouts. APIs have rate limits but they are documented and you can request increases.
Problem: Bounce handling. With SMTP, you get a bounce message in your inbox, but you must parse it. APIs provide a webhook that tells you the exact reason (hard bounce, spam complaint).
Alternatives and Hybrid Approaches
You can also use a hybrid: send via SMTP for low-volume internal emails and switch to an API for high-volume campaigns. Some providers offer both an SMTP endpoint and an API (e.g., Amazon SES). In Node.js, you can use nodemailer with an SMTP transport that points to SES’s SMTP endpoint, or use the AWS SDK to call the API. The choice is often about which SDK you prefer.
Frequently Asked Questions
Is nodemailer an SMTP relay?
No, nodemailer is a library that lets you send email via SMTP. It is not a relay itself; it connects to an SMTP server (your own or a third-party) to deliver the message.
Can I use Gmail SMTP for my Node.js app?
Yes, but Gmail has strict limits (500 emails/day for free accounts) and requires an app password. It’s fine for testing, but not for production volume. Use an email API or a dedicated SMTP relay for real apps.
What is the best email API for Node.js?
The “best” depends on your needs: SendGrid is popular for its free tier, Postmark for high deliverability, and Amazon SES for low cost at scale. All have Node.js SDKs. Choose one and test it.
How do I set up DKIM for my own SMTP relay?
Generate a DKIM key pair, add the public key as a TXT record in your DNS, and configure your mail server to sign outgoing messages. The exact steps vary by server software (e.g., Postfix with OpenDKIM).
Do email APIs guarantee inbox delivery?
No. No service can guarantee delivery because the recipient’s server decides. APIs improve your chances by maintaining IP reputation and offering authentication, but they cannot force inbox placement.