Skip to main content

EmailMessage

A container for email information.

Attributes

AttributeTypeDescription
content_subtypestring = "plain"The subtype of the MIME content type for the email body, defaulting to "plain".
encodingstring = nullUndocumented charset to use for text/* message bodies and attachments. If None, defaults to settings.DEFAULT_CHARSET.

Constructor

Signature

def EmailMessage(
subject: str = "",
body: str = "",
from_email: str = None,
to: list = None,
bcc: list = None,
connection: object = None,
attachments: list = None,
headers: dict = None,
cc: list = None,
reply_to: list = None
) - > null

Parameters

NameTypeDescription
subjectstr = ""The subject line of the email.
bodystr = ""The body text of the email.
from_emailstr = NoneThe sender's address. Defaults to settings.DEFAULT_FROM_EMAIL if not provided.
tolist = NoneA list or tuple of recipient addresses.
bcclist = NoneA list or tuple of blind carbon copy recipient addresses.
connectionobject = NoneThe email backend instance to use for sending the mail (deprecated).
attachmentslist = NoneA list of attachments to put on the message.
headersdict = NoneA dictionary of extra headers to put on the message.
cclist = NoneA list or tuple of carbon copy recipient addresses.
reply_tolist = NoneA list or tuple of reply-to addresses.

Signature

def EmailMessage(
subject: string = "",
body: string = "",
from_email: string = null,
to: list = null,
bcc: list = null,
connection: object = null,
attachments: list = null,
headers: dict = null,
cc: list = null,
reply_to: list = null
)

Parameters

NameTypeDescription
subjectstring = ""The subject line of the email
bodystring = ""The main text content of the email message
from_emailstring = nullThe sender's email address; defaults to settings.DEFAULT_FROM_EMAIL if not provided
tolist = nullA list or tuple of recipient email addresses
bcclist = nullA list or tuple of blind carbon copy recipient email addresses
connectionobject = nullDeprecated; the email backend instance to use for sending this message
attachmentslist = nullA list of attachments, which can be MIMEPart instances or tuples of (filename, content, mimetype)
headersdict = nullA dictionary of extra headers to put on the message
cclist = nullA list or tuple of carbon copy recipient email addresses
reply_tolist = nullA list or tuple of reply-to email addresses

Methods


connection()

@classmethod
def connection() - > object

Accesses or sets the deprecated connection attribute used for sending the email.

Returns

TypeDescription
objectThe email backend connection instance

message()

@classmethod
def message(
policy: email.policy.Policy
) - > email.message.EmailMessage

Constructs and returns a native Python email.message.EmailMessage object based on the current instance state.

Parameters

NameTypeDescription
policyemail.policy.PolicyThe email policy used to control how the message is constructed and serialized

Returns

TypeDescription
email.message.EmailMessageA fully populated email message object ready for serialization or sending

recipients()

@classmethod
def recipients() - > list

Return a list of all recipients of the email (includes direct addressees as well as Cc and Bcc entries).

Returns

TypeDescription
listA list of all unique email addresses found in the To, Cc, and Bcc fields

send()

@classmethod
def send(
fail_silently: boolean = false,
using: string = null
) - > int

Send the email message.

Parameters

NameTypeDescription
fail_silentlyboolean = falseWhether to suppress exceptions during the sending process
usingstring = nullThe alias of the mailer configuration to use for sending

Returns

TypeDescription
intThe number of messages sent (typically 1 if successful, 0 if no recipients)

attach()

@classmethod
def attach(
filename: string = null,
content: any = null,
mimetype: string = null
) - > null

Attach a file with the given filename and content. The filename can be omitted and the mimetype is guessed, if not provided.

Parameters

NameTypeDescription
filenamestring = nullThe name of the file to be displayed in the email; can also be a MIMEPart instance
contentany = nullThe raw file data or text content to attach
mimetypestring = nullThe explicit MIME type for the attachment; if omitted, it is guessed from the filename

Returns

TypeDescription
nullnull

attach_file()

@classmethod
def attach_file(
path: string,
mimetype: string = null
) - > null

Attach a file from the filesystem.

Parameters

NameTypeDescription
pathstringThe filesystem path to the file that should be attached
mimetypestring = nullThe explicit MIME type for the file; if omitted, it is guessed from the file extension

Returns

TypeDescription
nullnull