Prompt
How do I send order confirmation emails from code?
Latest observation
Oct 4, 2026
OpenAI APIWeb search: off
To send order confirmation emails from code, you usually do this:
- Trigger it when an order is successfully created/paid.
- Build an email template with the order details.
- Send it through an email provider or SMTP.
- Handle failures so the order flow doesn’t break if email sending fails.
Typical approach
1) Use an email service
Common options:
- SendGrid
- Mailgun
- Amazon SES
- Postmark
- Or plain SMTP from your own mail server
These are usually easier and more reliable than sending directly from your app server.
2) Prepare the message
Include:
- Order number
- Customer name
- Items purchased
- Total price
- Shipping/billing info
- Expected delivery info
- Support/contact link
3) Send from your backend
Usually from server-side code, not frontend code, so your API keys stay private.
Example: Node.js with Nodemailer (SMTP)
const nodemailer = require("nodemailer");
async function sendOrderConfirmationEmail(order, customerEmail) {
const transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});
const itemsHtml = order.items
.map(item => `<li>${item.name} x ${item.quantity} - $${item.price}</li>`)
.join("");
const html = `
<h2>Thanks for your order!</h2>
<p>Your order <strong>#${order.id}</strong> has been confirmed.</p>
<p><strong>Total:</strong> $${order.total}</p>
<ul>${itemsHtml}</ul>
`;
await transporter.sendMail({
from: '"Your Store" <orders@yourstore.com>',
to: customerEmail,
subject: `Order Confirmation #${order.id}`,
html,
});
}
Usage:
await sendOrderConfirmationEmail(
{
id: "12345",
total: "49.99",
items: [
{ name: "T-Shirt", quantity: 2, price: "19.99" },
{ name: "Sticker Pack", quantity: 1, price: "10.01" },
],
},
"customer@example.com"
);
Example: Python with SMTP
import smtplib
from email.mime.text import MIMEText
def send_order_confirmation_email(order, customer_email):
body = f"""
Thanks for your order!
Order #{order['id']} has been confirmed.
Total: ${order['total']}
"""
msg = MIMEText(body)
msg["Subject"] = f"Order Confirmation #{order['id']}"
msg["From"] = "Your Store <orders@yourstore.com>"
msg["To"] = customer_email
with smtplib.SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login("SMTP_USER", "SMTP_PASS")
server.send_message(msg)
Best practices
- Send asynchronously if possible, so checkout isn’t slowed down.
- Retry on failure using a queue/job system.
- Use templates for clean, reusable emails.
- Log email errors separately from order errors.
- Verify emails before sending if possible.
- Don’t send from the browser with secret credentials.
If you want a production-ready setup
A common pattern is:
- Order is saved to DB
- A background job/queue is created
- Worker sends email via email provider
- If it fails, it retries
If you want, I can show you a full working example in JavaScript/Node, Python, PHP, or C#.