class EmailMessage
[source]
The EmailMessage
class is initialized with the following parameters (in the given order, if positional arguments are used). All parameters are optional and can be set at any time prior to calling the send()
method.
-
subject
: The subject line of the email. -
body
: The body text. This should be a plain text message. -
from_email
: The sender’s address. Bothfred@example.com
andFred <fred@example.com>
forms are legal. If omitted, theDEFAULT_FROM_EMAIL
setting is used. -
to
: A list or tuple of recipient addresses. -
bcc
: A list or tuple of addresses used in the “Bcc” header when sending the email. -
connection
: An email backend instance. Use this parameter if you want to use the same connection for multiple messages. If omitted, a new connection is created whensend()
is called. -
attachments
: A list of attachments to put on the message. These can be eitheremail.MIMEBase.MIMEBase
instances, or(filename, content, mimetype)
triples. -
headers
: A dictionary of extra headers to put on the message. The keys are the header name, values are the header values. It’s up to the caller to ensure header names and values are in the correct format for an email message. The corresponding attribute isextra_headers
. -
cc
: A list or tuple of recipient addresses used in the “Cc” header when sending the email. -
reply_to
: A list or tuple of recipient addresses used in the “Reply-To” header when sending the email.
For example:
from django.core.mail import EmailMessage email = EmailMessage( 'Hello', 'Body goes here', 'from@example.com', ['to1@example.com', 'to2@example.com'], ['bcc@example.com'], reply_to=['another@example.com'], headers={'Message-ID': 'foo'}, )
The class has the following methods:
-
send(fail_silently=False)
sends the message. If a connection was specified when the email was constructed, that connection will be used. Otherwise, an instance of the default backend will be instantiated and used. If the keyword argumentfail_silently
isTrue
, exceptions raised while sending the message will be quashed. An empty list of recipients will not raise an exception. -
message()
constructs adjango.core.mail.SafeMIMEText
object (a subclass of Python’semail.MIMEText.MIMEText
class) or adjango.core.mail.SafeMIMEMultipart
object holding the message to be sent. If you ever need to extend theEmailMessage
class, you’ll probably want to override this method to put the content you want into the MIME object. -
recipients()
returns a list of all the recipients of the message, whether they’re recorded in theto
,cc
orbcc
attributes. This is another method you might need to override when subclassing, because the SMTP server needs to be told the full list of recipients when the message is sent. If you add another way to specify recipients in your class, they need to be returned from this method as well. -
attach()
creates a new file attachment and adds it to the message. There are two ways to callattach()
:- You can pass it a single argument that is an
email.MIMEBase.MIMEBase
instance. This will be inserted directly into the resulting message. -
Alternatively, you can pass
attach()
three arguments:filename
,content
andmimetype
.filename
is the name of the file attachment as it will appear in the email,content
is the data that will be contained inside the attachment andmimetype
is the optional MIME type for the attachment. If you omitmimetype
, the MIME content type will be guessed from the filename of the attachment.For example:
message.attach('design.png', img_data, 'image/png')
If you specify a
mimetype
ofmessage/rfc822
, it will also acceptdjango.core.mail.EmailMessage
andemail.message.Message
.In addition,
message/rfc822
attachments will no longer be base64-encoded in violation of RFC 2046#section-5.2.1, which can cause issues with displaying the attachments in Evolution and Thunderbird.
- You can pass it a single argument that is an
-
attach_file()
creates a new attachment using a file from your filesystem. Call it with the path of the file to attach and, optionally, the MIME type to use for the attachment. If the MIME type is omitted, it will be guessed from the filename. The simplest use would be:message.attach_file('/images/weather_map.png')
Please login to continue.