Я сделал вспомогательную функцию для этого и использовал render_to_string
, чтобы использовать шаблон HTML для отправки вместо того, чтобы вводить все в функции.Вот моя функция:
def signup_email(username, email, signup_link):
subject, from_email, to = 'Site Registration', settings.DEFAULT_FROM_EMAIL, email
text_content = 'Click on link to finish registration'
html_content = render_to_string('pet/html_email.html', {'username': username, 'signup_link':signup_link})
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
return msg
Затем в views.py просто вызовите его:
class OwnerCreateAccount(FormView):
# class based view attributes
def form_valid(self, form):
# other form logic
msg = signup_email(username=username, email=email, signup_link=owner.salt)
msg.send()
return super(OwnerCreateAccount, self).form_valid(form)