Prompt
How do I use SMTP relay for transactional email?
Latest observation
Nov 21, 2026
OpenAI APIWeb search: off
SMTP relay lets your app send email through a trusted mail server instead of connecting directly to every recipient’s mail server. For transactional email, the usual flow is:
- Your app creates the email
- Your app authenticates to an SMTP relay
- The relay delivers the message to the recipient’s mailbox provider
Why use an SMTP relay
- Better deliverability than sending directly from your app/server
- Handles retries, queues, and reputation management
- Usually easier than running your own mail server
- Good for password resets, receipts, alerts, verification emails, etc.
Basic setup steps
1) Choose an SMTP relay provider
Common options:
- SendGrid
- Mailgun
- Amazon SES
- Postmark
- Gmail/Google Workspace SMTP for light use
Pick one that supports:
- Authenticated SMTP
- TLS/STARTTLS
- Good deliverability tooling
- DKIM/SPF support
2) Verify your sending domain
Set up DNS records so recipients trust your mail:
- SPF: authorizes the relay to send for your domain
- DKIM: signs messages cryptographically
- DMARC: tells receivers how to handle spoofing
Your provider will give exact DNS records.
3) Configure SMTP connection details
You’ll typically need:
- SMTP host, e.g.
smtp.provider.com - Port:
587for STARTTLS, most common465for implicit TLS
- Username
- Password or API-specific SMTP credentials
- From address, e.g.
no-reply@yourdomain.com
4) Send mail from your application
Example with Node.js using Nodemailer:
import nodemailer from "nodemailer";
const transporter = nodemailer.createTransport({
host: "smtp.provider.com",
port: 587,
secure: false, // use STARTTLS
auth: {
user: "your-smtp-username",
pass: "your-smtp-password"
}
});
async function sendReceipt() {
await transporter.sendMail({
from: '"Acme" <no-reply@yourdomain.com>',
to: "customer@example.com",
subject: "Your receipt",
text: "Thanks for your order!",
html: "<p>Thanks for your order!</p>"
});
}
sendReceipt().catch(console.error);
Example with Python using smtplib:
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["Subject"] = "Your receipt"
msg["From"] = "Acme <no-reply@yourdomain.com>"
msg["To"] = "customer@example.com"
msg.set_content("Thanks for your order!")
with smtplib.SMTP("smtp.provider.com", 587) as smtp:
smtp.starttls()
smtp.login("your-smtp-username", "your-smtp-password")
smtp.send_message(msg)
Best practices for transactional email
- Use a dedicated sending subdomain like
mail.yourdomain.com - Use a
no-reply@or monitored address depending on your UX - Keep transactional and marketing email separate
- Implement retries for temporary failures
- Log message IDs and delivery errors
- Add unsubscribe handling only where applicable; transactional mail usually doesn’t need unsubscribe, but must remain clearly transactional
- Use templates and track bounces/complaints
- Throttle sending to avoid spikes if your app scales
Security tips
- Store SMTP credentials in environment variables or secrets manager
- Use TLS only
- Rotate credentials periodically
- Limit permissions if your provider supports scoped keys
- Never hardcode credentials in source control
Common issues
- Auth failure: wrong username/password or provider requires app password/API credential
- Spam folder: missing SPF/DKIM/DMARC or poor domain reputation
- Connection refused: wrong host/port or outbound firewall blocks SMTP
- Sending limits: provider quota exceeded
- Mail rejected: invalid sender domain or unverified from address
When to use SMTP relay vs email API
Use SMTP relay if:
- Your app already supports SMTP
- You want simple integration
- You need compatibility with legacy systems
Use an email API if:
- You want easier error handling and observability
- You need webhooks, template management, analytics
- You want simpler auth and better developer experience
If you want, I can also show:
- a production-ready SMTP relay setup checklist
- examples for Node, Python, PHP, or Java
- how to configure SPF, DKIM, and DMARC for a specific provider