Skip to main content
Python’s built-in smtplib module lets you send email through mySMTP without installing third-party packages. You can use STARTTLS on port 587 or implicit TLS on port 465 (Python’s SMTP_SSL class, which negotiates TLS despite its name).

Prerequisites

  • Python 3.8 or later
  • Your mySMTP username and password from the setup email
  • Your mySMTP hostname from the setup email (shown as relayxx.mysmtp.com in these examples)
Replace relayxx.mysmtp.com with your real hostname from the mySMTP setup email. Load credentials from environment variables, never hardcode them in source files.
If your network blocks port 587, use implicit TLS on port 465:

HTML body with attachment

Common errors

Your username or password is incorrect, or your account is not yet activated. Re-enter credentials exactly as shown in the setup email. See Setting up SMTP credentials.
Your network or host may block port 587. Try port 2525 with STARTTLS, or port 465 with SMTP_SSL (implicit TLS). See Supported SMTP ports and encryption.
The encryption method does not match the port. Use starttls() for ports 587 and 2525. Use SMTP_SSL for port 465.
Test your SPF, DKIM, and DMARC setup with the mySMTP Deliverability Toolbox before sending large volumes.