Prompt

How do I send order confirmation emails from code?

Technology / API Platforms1 observationLast seen Oct 4, 2026

Latest observation

Oct 4, 2026OpenAI APIWeb search: off

To send order confirmation emails from code, you usually do this:

  1. Trigger it when an order is successfully created/paid.
  2. Build an email template with the order details.
  3. Send it through an email provider or SMTP.
  4. 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#.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.