SMTP Settings
NotesTo send email, the application connects to an SMTP server, which then delivers the message. You don’t need anything special for this: the same mail server you use for an ordinary mailbox will do. For example:
- Your web hosting provider. Most hosting plans include email for your domain, and the provider’s help pages list the SMTP server, usually something like
mail.your-domain.com. The username and password are those of a mailbox, such asnoreply@your-domain.com. - Gmail or Google Workspace. The server is
smtp.gmail.com, port587. The account needs 2-Step Verification turned on, and you sign in with an app password rather than your normal password. - Microsoft 365, Fastmail, Proton Mail and others publish their SMTP settings in the same way.
- A transactional email service such as Postmark, Mailgun, Brevo or Amazon SES. These are designed for sending application email in volume, and also give you an SMTP server and credentials.
Whichever you choose, you’ll need four things:
| Setting | Example |
|---|---|
| SMTP server | mail.your-domain.com |
| Port | 587 (STARTTLS) or 465 (SSL/TLS) |
| Username | noreply@your-domain.com |
| Password | the mailbox password, or an app password |
Port 587 is the standard for sending mail: the connection starts unencrypted, and is upgraded to TLS (with the STARTTLS command) before the password is sent. Some providers use port 465 instead, where the connection is encrypted from the start. The code in the next section handles both.
If you’ve written a PHP contact form, you’ll have used PHP’s mail() function, which hands the message to a mail program on the web server. Python’s equivalent is in its standard library: smtplib talks to an SMTP server directly, and email.message builds the message. There’s nothing to install, and it works wherever the application runs, including in a container, which usually has no mail program.
The “from” address should be at a domain you control, and your SMTP provider should be allowed to send for it. Mail providers check this (with records called SPF, DKIM and DMARC in your domain’s DNS), and messages that fail the checks are rejected or marked as spam. Your hosting or email provider will tell you which DNS records to add.