I am trying to send HTML Email with Inline Images as shown in the article : https://www.vlent.nl/weblog/2014/01/15/sending-emails-with-embedded-images-in-django/. I have got it working. Now, i want to integrate it with Django mailer: https://github.com/pinax/django-mailer.
I-e i can queue up the emails to send and send batch of emails in one go.
Code i have is:
msg = EmailMultiAlternatives(subject, text_content, from_email, to_email)
msg.attach_alternative(html_content, "text/html")
msg.mixed_subtype = 'related'
fp = open(STATIC_ROOT+ filename, 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(filename))
msg.attach(msg_img)
And to send the email, i just do a :
msg.send()
To send html emails with Django mailer, i have to use the module:
send_html_mail(subject, message_plaintext, message_html, settings.DEFAULT_FROM_EMAIL, recipients)
msg.send_html_mail obviously doesnt work. Am i missing something or is there an alternative ?
You can just instantiate the connection yourself (note that Django 1.8 will include a context manager for this, but that's not out yet) and send the mails. This should do the trick:
from django.core import mail
connection = mail.get_connection()
connection.open()
connection.send_messages(your_messages)
connection.close()
Or:
from django.core import mail
connection = mail.get_connection()
connection.open()
for to_email in recipients:
# Generate your mail here
msg = EmailMultiAlternatives(..., connection=connection)
msg.send()
connection.close()