anvil.email Module
The anvil.email module contains functions for sending and receiving email in your Anvil app.
Classes
Address
DeliveryFailure
Message
Message.Addressees
Message.DKIM
Message.Envelope
SendFailure
SendReport
Functions
send
Classes
Address
Address Attributes
address - string
The email address this object represents.
name - string
The name associated with the address this object represents.
raw_value - string
The full string value of this address.
DeliveryFailure
(more info)
While handling an error, you can raise a DeliveryFailure exception to reject email delivery. Optionally, you may specify a message and SMTP error code with the rejection.
Constructor
DeliveryFailure(message=None, smtp_code=554)
Message
Represents an incoming email message.
In a test you can construct a Message to pass to an @anvil.email.handle_message function, eg incoming_email(Message(from_address="Alice <alice@example.com>", text="Hi")).
Constructor
Message([from_address=], [to=], [cc=], [subject=], [text=], [html=], [attachments=], [inline_attachments=], [headers=], [envelope_from=], [envelope_recipient=], [dkim=True])
from_address- The From address. Can be bare ('alice@example.com') or include a display name ('Alice <alice@example.com>'). Defaults to 'Sender <sender@example.com>'.to- The To address or addresses. Defaults to 'App <app@example.com>'.cc- The Cc address or addresses.subject- The subject line.text- The plain-text content.html- The HTML content.attachments- A list of Media objects.inline_attachments- A dictionary of Content-IDs to Media objects.headers- Extra headers, as (name, value) pairs or a dictionary. They replace the From, To, Cc, Subject and Message-ID headers the message would otherwise have.envelope_from- The sender according to the SMTP envelope. Defaults to the From address.envelope_recipient- The recipient according to the SMTP envelope, eg for a message received as a Bcc. Defaults to the first To address.dkim- If False, the message isn't signed by its sender's domain, so require_dkim=True handlers reject it. Defaults to `True`
Instance Methods
get_header(header_name, [default=None])
Return the value of the specified header, or default value if it is not present.
Case-insensitive. If the header is specified multiple times, returns the first value.
list_header(header_name)
Return a list containing every value of the specified header. Case-insensitive.
reply([cc=], [bcc=], [from_address=], [from_name=], [text=], [html=], [attachments=])
Reply to this email.
Message Attributes
addressees - anvil.email.Message.Addressees instance
The addresses this email was sent from and to, according to the headers.
attachments - list(anvil.Media instance)
A list of this email’s attachments.
dkim - anvil.email.Message.DKIM instance
Object describing whether this message was signed by the sending domain
envelope - anvil.email.Message.Envelope instance
The sender and receipient of this email, according to the SMTP envelope.
headers - list
All the headers in this email, as a list of (name, value) pairs.
html - string
The HTML content of this email, or None if there is no HTML part.
inline_attachments - dict(string,anvil.Media instance)
A dictionary of this email’s inline attachments. Keys are ContentID headers, values are the attachments as Media Objects.
subject - string
The subject of this email, or None if there is no subject.
text - string
The plain-text content of this email, or None if there is no plain-text part.
Message.Addressees
Message.Addressees Attributes
cc_addresses - list(anvil.email.Address instance)
The addresses this message was copied to.
from_address - anvil.email.Address instance
The address this message was sent from.
to_addresses - list(anvil.email.Address instance)
The addresses this message was sent to.
Message.DKIM
Message.DKIM Attributes
domains - list(string)
A list of the DKIM domains that signed this message.
valid_from_sender - boolean
Was this message signed by the domain in its envelope “from” address?
Message.Envelope
Message.Envelope Attributes
from_address - string
The email address from which this message was sent, according to the SMTP envelope.
recipient - string
The email address that received this message.
Note that this email address may not appear in any of the headers (eg if the email has been BCCed or blind forwarded).
SendFailure
Raised when sending an email fails.
Constructor
SendFailure()
SendReport
SendReport Attributes
message_id - string
The Message-ID header given to this outgoing message.
Functions
send([to=], [cc=], [bcc=], [from_address="no-reply"], [from_name=], [subject=], [text=], [html=], [attachments=], [inline_attachments=]) → anvil.email.SendReport instance
(more info)
Send an email
to- The email recipient[s] in the 'To' field. Can be a string or list of strings. Each string can be a bare address (eg 'joe@example.com') or include a display name (eg 'Joe Bloggs <joe@example.com>').cc- The email recipient[s] in the 'Cc' field. Can be a string or list of strings. Each string can be a bare address (eg 'joe@example.com') or include a display name (eg 'Joe Bloggs <joe@example.com>').bcc- The email recipient[s] in the 'Bcc' field. Can be a string or list of strings. Each string can be a bare address (eg 'joe@example.com') or include a display name (eg 'Joe Bloggs <joe@example.com>').from_address- The From: address from this email. Can be a bare address (eg 'joe@example.com') or include a display name (eg 'Joe Bloggs <joe@example.com>'). If no domain is specified, or the specified domain is not a legal sending domain for this app, the address will be replaced with a valid domain. So if you specify 'noreply', the email will come from 'noreply@your-app-domain.anvil.app'.from_name- The name associated with the From: address for this email. (Only valid if the from_address is a bare email address.)subject- The subject line for this email.text- The plain-text (no HTML) content for this email. You must specify at least one of 'text' and 'html'.html- The HTML content for this email. You must specify at least one of 'text' and 'html'.attachments- A list of Media objects to send as attachments with this email.inline_attachments- Inline that can be used in this email's HTML, for example in <img> tags. Must be a dictionary whose keys are IDs and values are Media objects. IDs can then be used in a message's HTML with 'cid:xxx' URIs.
Do you still have questions?
Our Community Forum is full of helpful information and Anvil experts.