SMTP relay › Python

Send email from Python through Sendwax

The standard library, no dependencies.

The values

Hostsmtp.sendwax.com
Port587
EncryptionTLS / STARTTLS
Usernamesendwax
Passworda send-only API key from API keys in the dashboard
Froman address you have approved under Senders

The username is literally sendwax for everybody — the key is what identifies your account. Use a send-only key: it can send mail and nothing else, so a key sitting in a settings screen on shared hosting cannot read your contacts.

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg['From'] = 'you@yourdomain.com'
msg['To'] = 'them@example.com'
msg['Subject'] = 'Hello'
msg.set_content('Sent through Sendwax.')

with smtplib.SMTP('smtp.sendwax.com', 587) as s:
    s.starttls()
    s.login('sendwax', 'esk_send_your_key')
    s.send_message(msg)

Step by step

  1. starttls() before login(). We refuse AUTH on an unencrypted connection, which is the point of it.
  2. The From header must be an approved sender.
  3. Django: the same values go in EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_PORT = 587 and EMAIL_USE_TLS = True.

SMTPAuthenticationError says less than it looks like

We answer 535 for a wrong key, a revoked key, and an account without SMTP switched on, deliberately — telling you which would tell anyone guessing which guess was closer. If the key is definitely right, check that SMTP is enabled on the account.

Checking it worked

A successful send_message returns an empty dict. The message is in your activity log by the time it does.

What else to know

We do not relay your message byte for byte — we read it and build a new one that our DKIM signature covers, which is what makes the reporting and the unsubscribe handling work. It costs you inline image placement, threading and a few other things, all listed on the SMTP relay page. Worth two minutes before you point production at it.

If you are writing the code rather than configuring somebody else's, the API is the better door: a message id per recipient and an error you can read, instead of a three-digit reply your framework swallowed.

Other setups

  • WP Mail SMTP — The most installed SMTP plugin on WordPress. Takes about two minutes.
  • FluentSMTP — Free, open source, and happy to hold several connections at once.
  • Post SMTP — Keeps its own email log and can alert you when a send fails.
  • Easy WP SMTP — The plainest of them. Nothing to configure beyond the connection.
  • Gravity SMTP — From the Gravity Forms team, and the right choice if your forms are theirs.
  • WooCommerce — Order confirmations, shipping notices, password resets. The ones customers notice.
  • Postfix — Everything on the box — cron, monitoring, mail(1) — goes out properly signed.
  • Laravel — Two lines of .env. Or use our package and skip SMTP entirely.
  • Nodemailer — The usual way a Node app sends mail.