Answer the question
In order to leave comments, you need to log in
How to properly attach images when sending an email?
Hello!
There is a code for sending reports from the site by e-mail.
def generate_mail():
get_chart()
today = timezone.now()
peoples = Peoples.objects.filter(date=today, obj__is_active=True).values('obj__short_title').annotate(total_rab=Sum('rab'), total_itr=Sum('itr')).order_by('-total_rab')
plain_text = get_template('email/email.txt')
html_msg = get_template('email/email.html')
d = Context({'peoples': peoples, 'today': today})
subject, from_email, to = 'Проверка отчётов', 'some mailbox', ['list of mailboxes']
text_content = plain_text.render(d)
html_content = html_msg.render(d)
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.send()
<img src=''>
Answer the question
In order to leave comments, you need to log in
Alternatively, encode to base64 and paste directly into the body of the email:
import base64
...
my_image = '<img src="data:image/jpg;base64,{im}" />'.format(im=base64.b64encode(instance.image.read()))
...
mail_html = """
<html>
<head></head>
<body>
<h4>Заголовок письма</h4>
{img}
....
</body>
</html>
""".format(img=my_image)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question