Prompt
How do I send emails programmatically?
Latest observation
Sep 23, 2026
OpenAI APIWeb search: off
You usually send emails programmatically in one of these ways:
1) Use an email-sending service or SMTP server
Common options:
- SMTP with providers like Gmail, Outlook, your company mail server
- Transactional email APIs like SendGrid, Mailgun, Amazon SES, Postmark
2) Basic SMTP flow
At a high level:
- Connect to the SMTP server
- Authenticate
- Set sender, recipient(s), subject, and body
- Send the message
3) Example in Python using SMTP
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["Subject"] = "Test Email"
msg["From"] = "you@example.com"
msg["To"] = "recipient@example.com"
msg.set_content("Hello from Python!")
with smtplib.SMTP("smtp.example.com", 587) as smtp:
smtp.starttls()
smtp.login("you@example.com", "your_password")
smtp.send_message(msg)
4) Example in Node.js using Nodemailer
const nodemailer = require("nodemailer");
async function main() {
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false,
auth: {
user: "you@example.com",
pass: "your_password"
}
});
await transporter.sendMail({
from: "you@example.com",
to: "recipient@example.com",
subject: "Test Email",
text: "Hello from Node.js!"
});
}
main().catch(console.error);
5) Best practices
- Use app passwords or OAuth when available instead of your main password
- Prefer TLS/STARTTLS
- For production, use a transactional email service
- Handle bounce/errors and rate limits
- Don’t send spam; make sure recipients opted in
6) If you want a quick recommendation
- Small scripts / testing: SMTP
- Production apps: SendGrid / Mailgun / SES / Postmark
If you tell me your language (Python, JavaScript, Java, etc.) and which email provider you want to use, I can give you a working example.