from django.db.models.signals import post_save from django.dispatch import receiver from django.core.mail import send_mail, EmailMultiAlternatives from django.template.loader import get_template, render_to_string from .models import WithDraw, InvoiceHistory @receiver(post_save, sender=WithDraw) def send_for_accountant(sender, instance, created, **kwargs): if created: ctx_dict = { 'username': instance.user.username, } subject, from_email, to = 'Заявка на распечатку', 'mukhtar@mukhtar', 'muhtarzubanchi05@gmail.com' text_content = render_to_string('send_for_accountant.txt', ctx_dict) html_content = get_template('send_for_accountant.html').render(ctx_dict) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.send()