DJANGO: How to Send Text and HTML Emails with Dynamic data in Python

DJANGO: How to Send Text and HTML Emails with Dynamic data in Python

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>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="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 />ExpertPHP</p>
  </body>
</html>
views.py

from django.template import Context
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMessage


def sendmail(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")

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort