Prompt
How do I send emails programmatically?
Latest observation
Oct 27, 2026
OpenAI APIWeb search: off
You generally send emails programmatically in one of two ways:
-
Use an email-sending API/service
Examples: SendGrid, Mailgun, Amazon SES, Postmark, Mailtrap (testing).
This is usually the easiest and most reliable option for production. -
Use SMTP directly
Your app connects to an SMTP server (like Gmail, Outlook, or your own mail server) and sends the message.
Typical steps
- Create an email account or choose an email service.
- Get credentials/API keys.
- Build the email content:
- recipient
- subject
- body (plain text or HTML)
- Send it from your code.
- Handle errors, retries, and logging.
Example: Python with SMTP
import smtplib
from email.mime.text import MIMEText
sender = "you@example.com"
password = "your_password_or_app_password"
recipient = "someone@example.com"
msg = MIMEText("Hello from Python!")
msg["Subject"] = "Test Email"
msg["From"] = sender
msg["To"] = recipient
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender, password)
server.sendmail(sender, [recipient], msg.as_string())
Example: Node.js with Nodemailer
const nodemailer = require("nodemailer");
async function sendEmail() {
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: "you@example.com",
pass: "your_password_or_app_password",
},
});
await transporter.sendMail({
from: '"You" <you@example.com>',
to: "someone@example.com",
subject: "Test Email",
text: "Hello from Node.js!",
});
}
sendEmail().catch(console.error);
Important notes
- Use app passwords or API keys, not your normal password when possible.
- For production email, pay attention to:
- SPF, DKIM, DMARC
- spam filtering
- rate limits
- bounce handling
- If you only want to test, use a sandbox like Mailtrap.
If you want, I can show you how to do this in Python, JavaScript, Java, C#, PHP, or Go.