To Send an Email¶
Create a new MailerMessage() object:
from mailqueue.models import MailerMessage new_message = MailerMessage() new_message.subject = "My Subject" new_message.to_address = "firstname.lastname@example.org" new_message.cc_address = "email@example.com" new_message.bcc_address = "firstname.lastname@example.org" new_message.from_address = "email@example.com" new_message.content = "Mail content" new_message.html_content = "<h1>Mail Content</h1>" new_message.app = "Name of your App that is sending the email." new_message.save()
When save is called, Django will immediately try to send the email. Should it fail, it will be marked as unsent, and wait for the next time the job is called. Of course, the BCC address is optional, as well as html content.
File attachments can be added to the e-mail with MailerMessage’s add_attachment method:
from mailqueue.models import MailerMessage from django.core.files import File message = MailerMessage(to_address="firstname.lastname@example.org", from_address="email@example.com") photo_one = File(open("Poznan_square.jpg", "rb")) message.add_attachment(photo_one) # …you can add more than one file attachment photo_two = File(open("Poznan_Malta-lake.jpg", "rb")) message.add_attachment(photo_two) message.save()
Adding a Reply To header¶
You can add a reply to header to your emails be setting:
from mailqueue.models import MailerMessage new_message = MailerMessage() new_message.reply_to = 'firstname.lastname@example.org'
Sending to Multiple Recipients¶
To include more than one CC/BCC in your email, just separate the addresses with a comma:
message.cc_address = "email@example.com, firstname.lastname@example.org, email@example.com" message.bcc_address = "firstname.lastname@example.org, email@example.com, firstname.lastname@example.org"
As of version 2.2.0 multiple recipients may be included in the to_address field as well:
message.to_address = "email@example.com, firstname.lastname@example.org, email@example.com"
Using the Management Command¶
You can use the management command to send email:
python manage.py send_queued_messages --limit=20