1๐
[UPDATE]: In order for the below to work you must have set then appropriate email settings inside your settings.py
file, like this:
# settings.py
#######################
# EMAIL SETTINGS #
#######################
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email_username_here'
EMAIL_HOST_PASSWORD = 'email_password_here'
The <a>
link is not rendered because there is no loader
to render it (unknown {% url %}
template tag). If you like to preserve this syntax ({% url ... %}
) and have a separate HTML file that will be sent then store the HTML file as separate file, say html_email.html
and then use render_to_string
and do the following:
<!-- html_email.html -->
<a href="http://localhost:8000/{% url 'invitations:refer-invitation' invite_code %}">Click Here</a>
# views.py
from django.template.loader import render_to_string
if created:
# above code as is
# in the context you can pass other context variables that will be available inside the html_email.html
context = {'invite_code': new_join_old.invite_code,}
html_message = render_to_string('path/to/html_email.html', context=context)
# below code as is
or you can do it this way:
#views.py
from django.core.mail import EmailMultiAlternatives
from django.urls import reverse
if created:
# above code as is
html_message = '<a href="http://localhost:8000{}">Click Here</a>'.format(reverse('invitations:refer-invitation', kwrags={'invite_code': invite_code}))
msg = EmailMultiAlternatives(subject, message, from_email, to_email, fail_silently=True)
msg.attach_alternative(html_message, 'text/html')
msg.send()
0๐
from django.template import Context, Template
email_data = open('email_templates/email.html', 'r').read()
html_data = Template(email_data)
html_content = html_data.render(Context({'invite_code': new_join_old.invite_code}))
You can add message
,subject
etc key-value pair in context as well
In email.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a href="http://localhost:8000/invitations/refer-invitation/{{ invite_code }}">Click Here</a>
</body>
</html>
You can access the value of context variable like message
and subject
in email.html using {{ message }}
and {{ subject }}
https://docs.djangoproject.com/en/1.10/topics/email/#the-emailmessage-class
- Django child template not including content in template tags from base.html
- How to make Postgresql query with JQuery in Django?
- CSS not loading on gcloud app engine?
- Change the css class of an element in a Django form that uses ModelForm
- Reversed counting in django template with <ol>-tag