djblets.mail.message¶
E-mail message composition and sending.
-
class
EmailMessage
(subject=u”, text_body=u”, html_body=u”, from_email=None, to=None, cc=None, bcc=None, sender=None, in_reply_to=None, headers=None, auto_generated=False, prevent_auto_responses=False, enable_smart_spoofing=None)[source]¶ Bases:
django.core.mail.message.EmailMultiAlternatives
An EmailMesssage subclass with improved header and message ID support.
This class knows about several headers (standard and variations), including Sender/X-Sender, In-Reply-To/References`, and Reply-To.
The generated Message-ID header from the e-mail can be accessed via the
message_id
attribute after the e-mail has been sent.In order to prevent issues when sending on behalf of users whose e-mail domains are controlled by DMARC, callers can specify
enable_smart_spoofing=True
(or setsettings.EMAIL_ENABLE_SMART_SPOOFING
). If set, then the e-mail address used for the From header will only be used if there aren’t any DMARC rules that may prevent the e-mail from being sent/received.In the event that a DMARC rule would prevent sending on behalf of that user, the
sender
address will be used instead, with the full name appearing as the value infrom_email
with “via <Service Name>” tacked onto it.Callers wishing to use this should also set
settings.EMAIL_DEFAULT_SENDER_SERVICE_NAME
to the desired service name. Otherwise, the domain on the sender e-mail will be used instead.This class also supports repeated headers.
-
__init__
(subject=u”, text_body=u”, html_body=u”, from_email=None, to=None, cc=None, bcc=None, sender=None, in_reply_to=None, headers=None, auto_generated=False, prevent_auto_responses=False, enable_smart_spoofing=None)[source]¶ Create a new EmailMessage.
Parameters: - subject (unicode, optional) – The subject of the message. Defaults to being blank (which MTAs might replace with “no subject”.)
- text_body (unicode, optional) – The body of the e-mail as plain text. Defaults to an empty string (allowing HTML-only e-mails to be sent).
- html_body (unicode, optional) – The body of the e-mail as HTML. Defaults to an empty string (allowing text-only e-mails to be sent).
- from_email (unicode, optional) – The from address for the e-mail. Defaults to
DEFAULT_FROM_EMAIL
. - to (list, optional) – A list of e-mail addresses as
unicode
objects that are to receive the e-mail. Defaults to an empty list of addresses (allowing using CC/BCC only). - cc (list, optional) – A list of e-mail addresses as
unicode
objects that are to receive a carbon copy of the e-mail, orNone
if there are no CC recipients. - bcc (list, optional) – A list of e-mail addresses as
unicode
objects that are to receive a blind carbon copy of the e-mail, orNone
if there are not BCC recipients. - sender (unicode, optional) –
The actual e-mail address sending this e-mail, for use in the Sender header. If this differs from
from_email
, it will be left out of the header as per RFC 2822.This will default to
DEFAULT_FROM_EMAIL
if unspecified. - in_reply_to (unicode, optional) – An optional message ID (which will be used as the value for the
In-Reply-To and References
headers). This will be generated if not provided and will be
available as the
message_id
attribute after the e-mail has been sent. - headers (django.utils.datastructures.MultiValueDict, optional) – Extra headers to provide with the e-mail.
- auto_generated (bool, optional) – If
True
, the e-mail will contain headers that mark it as an auto-generated message (as per RFC 3834) to avoid auto replies. - prevent_auto_responses (bool, optional) – If
True
, the e-mail will contain headers to prevent auto replies for delivery reports, read receipts, out of office e-mails, and other auto-generated e-mails from Exchange. - enable_smart_spoofing (bool, optional) –
Whether to enable smart spoofing of any e-mail addresses for the From header.
This defaults to
settings.EMAIL_ENABLE_SMART_SPOOFING
(which itself defaults toFalse
).
-
message
()[source]¶ Construct an outgoing message for the e-mail.
This will construct a message based on the data provided to the constructor. This represents the e-mail that will later be sent using
send()
.After calling this method, the message’s ID will be stored in the
message_id
attribute for later reference.This does not need to be called manually. It’s called by
send()
.Returns: The resulting message. Return type: django.core.mail.message.SafeMIMEText
-