Prompt

How to send email from Python

Technology / Api platforms3 observationsLast seen Sep 4, 2026

Latest observation

Sep 4, 2026Perplexity

To send email from Python, you have two main options: use the built-in smtplib for SMTP or call an email API via HTTP. For production apps, an API is usually simpler and more reliable.

Option 1: Using an email API (recommended for production)

Install a provider's SDK or use requests directly:

With an SDK (example: Resend):

import os
from resend import Resend

resend = Resend(api_key=os.environ["RESEND_API_KEY"])

params = {
    "from": "noreply@yourapp.com",
    "to": "user@example.com",
    "subject": "Hello from Python",
    "html": "<p>Thanks for signing up!</p>",
}

email = resend.emails.send(params)

With raw HTTP (works with any provider):

import os
import requests

response = requests.post(
    "https://api.resend.com/emails",
    headers={
        "Authorization": f"Bearer {os.environ['RESEND_API_KEY']}",
        "Content-Type": "application/json",
    },
    json={
        "from": "noreply@yourapp.com",
        "to": ["user@example.com"],
        "subject": "Hello from Python",
        "html": "<p>Thanks for signing up!</p>",
    },
)

print(response.json())

Popular Python SDKs: resend, sendgrid, mailgun-python, postmark, mailtrap.

Option 2: Using SMTP with smtplib (built-in)

import os
import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg["From"] = "noreply@yourapp.com"
msg["To"] = "user@example.com"
msg["Subject"] = "Hello from Python"
msg.set_content("Thanks for signing up!")

with smtplib.SMTP("smtp.yourprovider.com", 587) as server:
    server.starttls()
    server.login(os.environ["SMTP_USER"], os.environ["SMTP_PASS"])
    server.send_message(msg)

Which to use?

  • Email API – Better for production, transactional email, serverless functions, and when you want delivery tracking, templates, and webhooks
  • SMTP (smtplib) – Fine for simple scripts, internal tools, or when you already have SMTP credentials

For SaaS apps sending receipts, password resets, or notifications, use an email API provider like Resend, Postmark, SendGrid, or Mailgun.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (3 observations 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.