blog
Send Email with JavaScript: Browser, Node.js, and Email API Patterns
Learn how to send email with JavaScript safely. Compare mailto, Node.js SMTP, and JavaScript email API approaches with code examples and testing tips.
If you searched for "javascript send email", "send email with javascript", "send email js", "javascript email api", or "send email api javascript", the key decision is where email is actually sent: browser, server runtime, or API backend.
This guide shows the safest approach for each pattern, with examples you can run today.
Executive summary
- Browsers cannot securely send SMTP email directly with raw sockets.
mailto:works for simple contact actions, but it depends on the user's local email client.- For production, send email from a backend using Node.js SMTP or a JavaScript email API.
- Keep credentials out of frontend bundles and test full send-receive flows in CI.
Which JavaScript email pattern should you use?
| Need | Best pattern | Where to go next |
|---|---|---|
| Simple user-triggered contact link | mailto: link |
HTML mailto guide |
| Server-side SMTP send from Node.js | Node.js SMTP with secrets in server env vars | Node SMTP email guide |
| App-triggered send through an HTTP API | Backend route that calls an email API | Axios email API example |
| Test send-and-receive behavior in CI | MailSlurp inboxes plus wait assertions | Email integration testing |
Use the browser as the trigger, not the place where credentials live. The safest JavaScript email API pattern is a server-side route that validates the request, sends through MailSlurp, and records the result for debugging.
Quick answer: can JavaScript send email directly?
Not securely from the browser.
Frontend JavaScript can trigger email flows, but actual delivery should happen on a trusted backend where SMTP credentials or API keys stay private.
If you need SMTP background first, read SMTP protocol explained and SMTP authentication.
Option 1: use mailto: for lightweight flows
mailto: opens the default email client with pre-filled fields.
<a href="mailto:support@example.com?subject=Support%20request">Email support</a>
Use it when:
- you only need a quick user-triggered email draft
- you do not need delivery guarantees or analytics
Limitations:
- no server-side delivery control
- inconsistent behavior across devices
- not suitable for transactional automation
Option 2: send email with JavaScript on Node.js (SMTP)
Server-side JavaScript can send SMTP mail safely with nodemailer.
npm install nodemailer
import nodemailer from "nodemailer";
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: 587,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});
await transporter.sendMail({
from: "no-reply@example.com",
to: "user@example.com",
subject: "Welcome",
text: "Your account is ready.",
});
Use this when your app already has backend infrastructure and SMTP requirements.
For the full server-side implementation path, use the Node SMTP email guide.
Related: Nodemailer tutorial and SMTP relay guide.
Option 3: frontend JavaScript calling a backend email API
A clean pattern is: browser JS calls your backend endpoint, backend sends the email.
Frontend:
await fetch("/api/send-email", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
to: "user@example.com",
template: "welcome",
}),
});
Backend route (example):
export default async function handler(req, res) {
// Validate input, apply auth/rate limits, then send via SMTP or provider API.
res.status(200).json({ ok: true });
}
This gives you:
- secret management on the server
- validation and abuse controls
- observability and retries
For a runnable HTTP example, see the Axios email API example. For API-level sending options, start with the MailSlurp send email API and the JavaScript SendEmailOptions guide.
Recommended MailSlurp JavaScript email API workflow
MailSlurp gives JavaScript teams both the send path and the proof that the message arrived. A dependable workflow is:
- Create a MailSlurp inbox for the sender, recipient, or test run.
- Send from a backend route, Node.js worker, or CI job using your MailSlurp API key.
- Wait for the received message with a MailSlurp wait helper.
- Assert the subject, body, links, OTP codes, attachments, and headers.
- Store the MailSlurp message ID with your test or release record.
This is useful for signup emails, password resets, magic links, invoice messages, support notifications, and any workflow where a successful send call is not enough.
Common JavaScript email errors (and fixes)
535 Authentication failed
- verify SMTP username/password
- verify STARTTLS/TLS mode and port
- check provider policy (app passwords, tenant settings)
More detail: SMTP authentication errors.
CORS failures when calling send endpoints
- allow only trusted origins
- keep send endpoint server-side
- avoid direct third-party key usage in browser code
Exposed secrets in frontend bundles
- never embed SMTP credentials in client JavaScript
- use server-only env vars
- rotate keys if exposure is suspected
Testing JavaScript email flows end to end
Sending is only half the workflow. You should also verify:
- the message arrives
- subject/body content is correct
- links and OTP codes are valid
- timing is within expected SLA
MailSlurp supports on-demand inboxes and wait-for-email assertions for integration tests. Start with sending emails, the JavaScript SDK quickstart, and email integration testing.
Launch checklist for JavaScript email features
Before release, pair send logic with operational checks:
- Validate sender auth and policy posture with SMTP authentication and DMARC monitoring.
- Test render and delivery quality using email client testing and email deliverability testing.
- Keep end-to-end regression coverage in Email Sandbox so flows are verified on every deploy.
FAQ
What is the best way to send email in JavaScript?
For production applications, send from a backend service (Node.js SMTP or API). Avoid direct credential-based sending from browser JavaScript.
What is a JavaScript email API?
A JavaScript email API is an HTTP or SDK-based way to create inboxes, send messages, and inspect received mail from JavaScript. Use it from trusted backend code or CI, not from public browser bundles.
Can I send email from JavaScript without a backend?
Only limited patterns like mailto: are practical without a backend. They are not reliable for transactional email delivery.
Is "send email js" the same as SMTP?
Not always. "Send email js" can mean browser-triggered flows, backend SMTP sending, or API-based delivery.
Final take
If your goal is reliable email in production, treat frontend JavaScript as the trigger and backend infrastructure as the sender.
For testable send-and-receive workflows with real inboxes, create a free account at app.mailslurp.com.