Datalumina

Signing

Gmail SMTP

Create a Gmail account for the demo, turn on 2-Step Verification, make an app password, and send email from FastAPI over SMTP.

By the end of this page your .env holds the SMTP settings of a Gmail account, and the backend can send an email through it.

The app sends two emails. One goes to the client with the signing link, and one goes to both sides with the signed PDF. Gmail is the sender here because everyone can create an account for free. A real company would usually use an email service, and only one function changes.

No email account yet?

You can skip this page. Without SMTP_HOST, the app prints every email in the API log instead of sending it, and you open the signing link from there or from the admin page.

Create the demo Gmail account

Create a new Gmail account for this project, for example pinewick.contracts@gmail.com (pick any free name). An app password gives full access to the mailbox, so do not use your personal account.

Turn on 2-Step Verification

Gmail only gives app passwords to accounts with 2-Step Verification.

  1. Open your Google Account with the demo account.
  2. Open Security, then 2-Step Verification.
  3. Turn it on and follow the steps. A phone number is enough.

Create an app password

  1. Open App passwords with the demo account.
  2. Name it contract-drafting and click Create.
  3. Copy the 16-character password. Google shows it only once.

Add it to the environment file

.env
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=pinewick.contracts@gmail.com
SMTP_PASSWORD=abcdefghijklmnop
EMAIL_FROM=pinewick.contracts@gmail.com

Use your own demo address in SMTP_USER and EMAIL_FROM, and paste the app password without spaces. Keep EMAIL_FROM the same as SMTP_USER, so the sender matches the account that logs in. The app also sends a copy of every signed contract to EMAIL_FROM.

Add the settings

Replace backend/app/config.py. The highlighted lines are new.

backend/app/config.py
import os
from pathlib import Path

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    # Local runs read .env in the project root. In Docker, Compose passes the same values.
    model_config = SettingsConfigDict(env_file=Path(__file__).parents[2] / ".env", extra="ignore")

    company_name: str = "Pinewick Systems"
    database_url: str
    anthropic_api_key: str = ""
    ai_model: str = "anthropic:claude-opus-5-5"  # "test" runs without a key
    base_url: str = "http://localhost:3000"
    render_url: str = "http://localhost:3000/api/internal/render"
    render_token: str = ""
    smtp_host: str = ""
    smtp_port: int = 587
    smtp_user: str = ""
    smtp_password: str = ""
    email_from: str = "contracts@pinewick.example.com"


settings = Settings()

# Pydantic AI reads the Anthropic key from the environment.
if settings.anthropic_api_key:
    os.environ["ANTHROPIC_API_KEY"] = settings.anthropic_api_key

The email settings are for this page. BASE_URL is the start of the signing link in the email, and the two render settings are for the signed PDF, two pages from now.

Write the email function

backend/app/email.py
import smtplib
from email.message import EmailMessage

from app.config import settings


def send_email(to: list[str], subject: str, body: str, pdf: bytes | None = None) -> None:
    """Send one email over SMTP. Swap this function to use an email service."""
    message = EmailMessage()
    message["From"], message["To"], message["Subject"] = settings.email_from, ", ".join(to), subject
    message.set_content(body)
    if pdf:
        message.add_attachment(pdf, maintype="application", subtype="pdf", filename="signed.pdf")

    if not settings.smtp_host:  # No SMTP settings: print it, so the app runs without email.
        print(f"--- Email to {message['To']}: {subject}\n{body}---", flush=True)
        return
    with smtplib.SMTP(settings.smtp_host, settings.smtp_port) as smtp:
        if settings.smtp_user:
            smtp.starttls()
            smtp.login(settings.smtp_user, settings.smtp_password)
        smtp.send_message(message)

send_email() is the one function to swap when you move to an email service. With SMTP_USER set, it upgrades the connection with STARTTLS and logs in. Without SMTP_HOST, it prints the email, so the app works without any email account.

Check

fastapi dev does not reload when .env changes. Stop the API with Ctrl+C and start it again, then send yourself a test email from a second terminal in the backend folder:

uv run --locked --no-sync python -c "from app.email import send_email; send_email(['you@example.com'], 'Test', 'It works.')"

Replace you@example.com with your personal address. The email arrives from the demo account within a few seconds.

ai.py
config.py
db.py
email.py
main.py
templates.py
.env

On this page