SMTP

Overview

The SMTP module enables the sending of email through a generic SMTP server. If you have an email server other than Gmail, this is likely the best way to send emails with Parsons.

Credentials

Credentials are required to use the class. You’ll need to provide a valid username and password for the SMTP server you are using.

Quickstart

To initialize the SMTP class you will need to tell it how to connect to the SMTP server:

Configure server connection
from parsons import SMTP
smtp = SMTP(
   host="fake.host.com",
   port=9999,
   username="my_username",
   password="dont_use_this_password",
)

Environment Variables

Instead of passing in values to initialize an instance of the SMTP class, you can set environment variables to hold the values. The names of the environment variables are the names of the arguments capitalized and prefixed with SMTP_. For example, SMTP_HOST or SMTP_PASSWORD. If both an environment variable and an initialization argument are present, the argument will take precedence.

Send an email
smtp.send_email(
   "sender@email.com",
   "recipient@email.com",
   "The Subject",
   "This is the text body of the email",
)
Send an email with HTML and/or attachment(s).
smtp.send_email(
   "sender@email.com",
   "recipient@email.com",
   "An html email with attachments",
   "This is the text body of the email",
   html="<p>This is the html part of the email</p>",
   files=['file1.txt', 'file2.txt'],
)

API

class parsons.notifications.smtp.SMTP(host=None, port=None, username=None, password=None, tls=None, close_manually=False)[source]

Create a SMTP object, for sending emails.

Parameters:
  • host – str The host of the SMTP server

  • port – int The port of the SMTP server (Default is 587 for TLS)

  • username – str The username of the SMTP server login

  • password – str The password of the SMTP server login

  • tls – bool Defaults to True – pass “0” or “False” to SMTP_TLS to disable

  • close_manually – bool When set to True, send_message will not close the connection

send_email(sender: str, to: str | list[str], subject: str, message_text: str, message_html: str | None = None, files: str | list[str] | None = None, *, check_deliverability: bool = False, dns_resolver: Resolver | None = None)

Send an email message.

Parameters:
  • sender (str) – Email address of the sender.

  • to (str | list[str]) – Email address(es) of the receiver(s). Must be in correct email string syntax. For example, name@email.com or "Name" <email@email.com>.

  • subject (str) – The subject of the email message.

  • message_text (str) – The text of the email message.

  • message_html (str | None) – The html formatted text of the email message. If ommitted, the email is sent a text-only body.

  • files (str | list[str] | None) – The path to the file(s) to be attached.

  • check_deliverability (bool)

  • dns_resolver (Resolver | None)

Keyword Arguments:
  • check_deliverability – Query DNS to ensure that the domain name(s) can receive mail.

  • dns_resolver – Caching dns resolver to reuse in each call. You can create one with email_validator.caching_resolver(timeout=10) If check_deliverability is not True, this has no effect. If resolver is not provided, but check_deliverability is True, one will be created with a 10 second timeout.

Raises:

EmptyListError – If to parameter is an empty list.

class parsons.notifications.sendmail.EmptyListError[source]

Throw when a list is empty that should contain at least 1 element.

add_note(object, /)

Exception.add_note(note) – add a note to the exception

with_traceback(object, /)

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.