The mail API provides two ways to send an email message: the
mail.send_mail()
function and the EmailMessage
class.
Sending is asynchronous: the mail.send_mail()
function and the
EmailMessage.send()
method transmit the message data to the mail
service, then return. The mail service queues the message, then attempts to
send it, retrying if the destination mail server is unavailable. Errors and
bounce messages are sent to the sender address for the email message.
Before you begin
You must register your sender emails as authorized senders. For more information, see who can send email.
Sending mail with mail.send_mail()
To send mail using the mail.send_mail()
function, use the fields of the
email message as parameters, including the sender, the recipients, the subject
and the body of the message. For example:
Sending mail with EmailMessage
To send mail using objects with the
EmailMessage
class, pass the fields of the email message to
the EmailMessage constructor and use attributes of the instance to update
message.
The EmailMessage.send()
method sends the email message
represented by the instance's attributes. An application can re-use an
EmailMessage
instance by modifying attributes and calling the send()
method
again.
The following example demonstrates sending a message to confirm an email address:
Sending bulk mail
See the Bulk mail guidelines for considerations around sending bulk email.