Django+ Django 등록과 함께 HTML로 이메일을 보내십시오
-
19-09-2019 - |
문제
Django-Registration을 사용하는 IM은 모두 괜찮습니다. 확인 이메일은 일반 텍스트로 전송되었지만 IM을 고정시키고 HTML로 보내고 있지만 쓰레기 문제가 있습니다 ... HTML 코드가 표시됩니다.
<a href="http://www.example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/">http://www. example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/</a>
그리고 나는 HTML 코드를
아이디어가 있습니까?
감사
해결책
텍스트 버전과 HTML 버전을 모두 보내는 것이 좋습니다. django- 등록의 모델을 살펴보십시오.
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
대신 문서에서 나온 일을하십시오 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()
다른 팁
Django 등록 패치를 피하려면 등록 프로파일 모델을 연장해야합니다. 프록시 = 참:
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()
등록 백엔드에서만 사용하십시오 htmlgistrationProfile 대신에 등록 프로파일.
나는 이것이 오래되었고 등록 패키지가 더 이상 유지되지 않는다는 것을 알고 있습니다. 누군가가 여전히 이것을 원할 경우를 대비하여. @bpierre의 답변에 대한 추가 단계는 다음과 같습니다.
-RegistrationView, 즉 앱의 뷰를 서브 클래스
class MyRegistrationView(RegistrationView):
...
def register(self, request, **cleaned_data):
...
new_user = HtmlRegistrationProfile.objects.create_inactive_user(username, email, password, site)
- urls.py에서 뷰를 하위 클래스 뷰 (IE -List Item)로 변경합니다.
url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'
제휴하지 않습니다 StackOverflow