Views.py
For sending emails
from django.core.mail import send_mail
from django.utils.html import strip_tags
from django.template import Context, Template
from pages.models import EmailTemplate
Send email
try:
template = EmailTemplate.objects.get(name="Registration")
email_to = user.email
t = Template(template.body)
s = Template(template.subject)
c = Context({'user': user})
html_message = t.render(c)
subject = s.render(c)
plain_message = strip_tags(html_message)
from_email = 'My company [email protected]'
send_mail(
subject,
plain_message,
from_email,
[email_to],
fail_silently=False,
html_message=html_message,
)
print('email sent to' + email_to )
except Exception as e:
print("Exception send email ==========================")
print(e)
messages.error(request, "Error. Something went wrong.")
print("End of Exception ==========================")