Make Django a built-in send_mail function working with html by default

I want to replace the built-in send_mail function , which only works with text email messages, with my own intelligent send_mail function , which generates as html, text versions automatically. Everything works as expected for my own emails defined in my own application. I can do this in views.py this way:

from django.core import mail
from utils import send_mail as send_html_mail
mail.send_mail = send_html_mail

But the problem still appears with third-party messaging applications. There, all the imports have already been completed before my code by decapitating the send_mail function .

Is it possible to override this function before importing all django applications? Or there may be another solution to this problem. I really do not want to fix the code with sending emails from these third-party applications. It's easy to just put the html template.

+3
source share
6 answers

I wrote an application to quickly switch from html-based text messages to email for all project applications. It will work without changing the Django code or the code of other applications of the third part.

Have a look here: https://github.com/ramusus/django-email-html

+1
source

Django docs: http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

send_mail().

+2

, .

, sys.modules['django.core.mail'].

, .

import sys
from django.core import mail 

def my_send_mail(*args, **kwargs):
    print "Sent!11"

mail.send_mail = my_send_mail
sys.modules['django.core.mail'] = mail


from django.core.mail import send_mail
send_mail('blah')
# output: Sent!11

, settings.py manage.py shell send_mail, .

In [1]: from django.core.mail import send_mail
In [2]: send_mail()
Sent!11


, - .

+2

django ? , . , ( ), .

0

Django 1.7 html_message to send_mail() html-.

https://docs.djangoproject.com/en/1.8/topics/email/#send-mail

0

Source: https://habr.com/ru/post/1785300/


All Articles