DJANGO: How to Send Text and HTML Emails with Dynamic data in Python
In this post we will give you information about DJANGO: How to Send Text and HTML Emails with Dynamic data in Python. Hear we will give you detail about DJANGO: How to Send Text and HTML Emails with Dynamic data in PythonAnd how to use it also give you demo for it if it is necessary.
DJANGO: How to Send Text and HTML Emails with Dynamic data in Python
In this Python tutorial, I will let you know how to send emails to user with template data in Django .
Sending emails to user via applications are very common and great feature to enhances the User Experience. For example, you have to send welcome email sometimes when user sign up on your website, forgot password emails etc.
When you will not define the format of mail content then all the content will be treated as simple text.
Email Configuration in Django
In this example, I will use my Gmail account credential as SMTP server. You can change your settings in settings.py
file of the project.
... EMAIL_USE_TLS = True EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_PASSWORD='******' EMAIL_HOST_USER='myaccount@gmail.com' EMAIL_PORT = 587 DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Never add your password or any confidential credentials to any file that you commit to version control, it should be placed inside the environment settings.
You can also configure the SMTP SSL connection using django-smtp-ssl
package.
Run following command to install SMTP SSL email backend for Django :
pip install django-smtp-ssl
Change your setting in following way :
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'myaccount@gmail.com' EMAIL_HOST_PASSWORD = '******' EMAIL_PORT = '465'
Configure the templates directory
In order to use the html template, I need to configure the templates directory inside the setting.py
file.
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], ... ]
Now create a directory “templates” on root level of application and then create a html file “mail.html” inside the directory for email templates.
templates/mail.html
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"/> <metaname="viewport"content="width=device-width, initial-scale=1.0"/> <title>Sending emails to user in Python</title> </head> <body> Hello {{user}}, <p>Welcome to the world of programming</p> <p>Thanks,<br/>onlinecode</p> </body> </html>
views.py
fromdjango.templateimport Context fromdjango.template.loaderimport render_to_string, get_template fromdjango.core.mailimport EmailMessage defsendmail(request): ctx = { 'user': "Ajay" } message = get_template('mail.html').render(ctx) msg = EmailMessage( 'Subject', message, 'from@example.com', ['to@example.com'], ) msg.content_subtype ="html"# Main content is now text/html msg.send() print("Mail successfully sent")
It is very simple to send emails in Django.
Label :
Python
Django
Hope this code and post will helped you for implement DJANGO: How to Send Text and HTML Emails with Dynamic data in Python. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs