django E-Mail senden in html mit django-Registrierung
-
19-09-2019 - |
Frage
im django-Registrierung verwendet, ist alles in Ordnung, die Bestätigungsmail wurde im Klartext zu senden, aber weiß, im festen und im HTML sendet, aber ich habe einen Wurf Problem ... der HTML-Code angezeigt wird:
<a href="http://www.example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/">http://www. example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/</a>
und i dont müssen den HTML-Code zeigen, wie die ...
Jede Idee?
Danke
Lösung
Ich würde empfehlen, sowohl eine Textversion und eine HTML-Version senden. Blick in der models.py der django-Registrierung für:
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
und stattdessen etwas tun, wie von den 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()
Andere Tipps
Um zu vermeiden, django-Registrierung Patchen, sollten Sie das RegistrationProfile Modell erweitern mit Proxy = True :
models.py
class HtmlRegistrationProfile(RegistrationProfile):
class Meta:
proxy = True
def send_activation_email(self, site):
"""Send the activation mail"""
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
ctx_dict = {'activation_key': self.activation_key,
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
'site': site}
subject = render_to_string('registration/activation_email_subject.txt',
ctx_dict)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
message_text = render_to_string('registration/activation_email.txt', ctx_dict)
message_html = render_to_string('registration/activation_email.html', ctx_dict)
msg = EmailMultiAlternatives(subject, message_text, settings.DEFAULT_FROM_EMAIL, [self.user.email])
msg.attach_alternative(message_html, "text/html")
msg.send()
Und in Ihrer Registrierung Backend, nur Gebrauch HtmlRegistrationProfile statt RegistrationProfile .
Ich weiß, das ist alt und das Registrierungspaket wird nicht mehr gepflegt. Nur für den Fall will noch jemand diese.
Die zusätzlichen Schritte WRT auf die Antwort von @bpierre sind:
- die RegistrationView Unterklasse, das heißt Ihre Anwendung ist views.py
class MyRegistrationView(RegistrationView):
...
def register(self, request, **cleaned_data):
...
new_user = HtmlRegistrationProfile.objects.create_inactive_user(username, email, password, site)
- in Ihrem urls.py Änderung der Blick auf die Unterklassierten Ansicht, das heißt - Liste item
url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'
Dieser Typ hat die default erweitert es uns ermöglicht, eine HTML-Version hinzufügen die Aktivierung E-Mail.
Insbesondere die alternative Version Arbeit erledigt ist hier
ich es geschafft, die Back-End-Teil zu verwenden, erfolgreich