def test_attach_image(self): """ Tests the embed_image function. """ dir_path = os.path.join(settings.PROJ_DIR, 'cyphon/static/images/') file_name = 'cyphon-sm.png' msg = embed_image(self.msg, dir_path, file_name) msg_dict = vars(msg) img_dict = vars(msg_dict['attachments'][0]) self.assertEqual(msg_dict['mixed_subtype'], 'related') self.assertEqual(img_dict['_headers'][0], ('Content-Type', 'image/png')) self.assertEqual(img_dict['_headers'][3], ('Content-ID', '<%s>' % file_name))
def compose_comment_email(comment, user): """Email a comment notification to a user. Parameters ---------- comment : |Comment| The |Comment| to notify the user about. user : |AppUser| The |AppUser| who should receive the email notification. Returns ------- :class:`django.core.mail.message.EmailMultiAlternatives` """ context = { 'comment': comment, 'image': _LOGO_FILE, } subject = loader.render_to_string(_SUBJECT_TEMPLATE, context) # email subject must not contain newlines subject = ''.join(subject.splitlines()) body = loader.render_to_string(_TEXT_TEMPLATE, context) email_message = EmailMultiAlternatives( subject=subject, body=body, to=[user.email], from_email=settings.DEFAULT_FROM_EMAIL ) # attach html html_email = loader.render_to_string(_HTML_TEMPLATE, context) email_message.attach_alternative(html_email, 'text/html') # embed logo email_message = emailutils.embed_image( message=email_message, dir_path=_LOGO_DIR, file_name=_LOGO_FILE ) return email_message