Django – Sending E-mails

Django – Sending E-mails

Django comes with a light engine that is ready and simple to use to send emails. Similar to Python, you just need to import a smtplib. You just need to import django.core.mail in Django. Edit the project settings.py file to start sending emails and set  all the following options:

  • EMAIL_HOST – server with smtp.
  • EMAIL_HOST_USER – smtp server login key.
  • EMAIL_HOST_PASSWORD ? Password credential for the smtp server.
  • EMAIL_PORT – server port smtp.
  • EMAIL_USE_TLS or _SSL – if it is secure connection then set True.

Simple E-mail Sending

To submit a simple e-mail, let's create it a "simpleEmail" view.

from django.core.mail import send_mail
 from django.http import HttpResponse
 def simpleEmail(request,emailto):
    res = send_mail("hello deepak", "comment tu ?", "deepak@gmail.com", [emailto])
    return HttpResponse('%s'%res) 

Description of the parameter s of send mail-

  • subject – In this we write subject about e-mail.
  • message –In this we write body for e-mail .
  • from_email – e-mail from.
  • recipient_list ? List of receivers’ e-mail address.
  • Fail_silently ? Bool, if false send mail will raise an exception in case of error.
  • auth_user ? User login if not set in settings.py.
  • auth_password ?In settings.py user password will not set.
  • connection – Backend of e-mail.
  • html_message –It is new in Django 1.7 if present the e-mail will be multipart.

Creating URL to access a view.

from django.conf.urls import patterns, url
 urlpatterns = paterns('myapp.views', url(r'^simpleemail/(?P<emailto>
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/', 
    'sendSimpleEmail' , name = 'sendSimpleEmail'),) 

Sending Multiple Mails with send_mass_mail.

The process returns the number of messages sent successfully. This is the same as send mail but takes an extra parameter; datatuple, our view of sendMassEmail will be ?

from django.core.mail import send_mass_mail
 from django.http import HttpResponse 
 def sendMassEmail(request,emailto):
    message1 = ('subject 1', 'message 1', 'deepak@gmail.com', [first email]) 
    message2 = ('subject 2', 'message 2', ' deepak@gmail.com', [second email])
    res = send_mass_mail((msg1, msg2), fail_silently = False)
    return HttpResponse('%s'%res) 

Creating URL to access a view.

from django.conf.urls import patterns, url
 urlpatterns = paterns('myapp.views', url(r'^massEmail/(?P<emailto1>
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/(?P<emailto2> 
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})', 
 'sendMassEmail' , name = 'sendMassEmail'),) 

send_mass_mail details parameters are –

  • datatuples ? A tuple where element like (subject, message, from_email, recipent_list)
  • fail_silently ? Boolean, in case of error, if false send_mail raises an exception.
  • auth_user – login of user will not set in settings.py.
  • auth_password – password of user will not set in setting.py .
  • connection ? E-mail backend.

Note ? In the above example we are using Python smtp debuggingserver, that can launch using –

$python -m smtpd -n -c DebuggingServer localhost:1025

This means all your sent e-mails will be printed on stdout , and the dummy server is running on localhost:1025.


Related Topics

Django vs Laravel

Django Overview Django is a Python Programming Open Source framework for web development. It was released in 2008 and was developed by two web developers named Adrian Holovaty and Simon Willison....

4 minutes read.

How to Implement Multiple User Types with Django

Django Framework: Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the...

3 minutes read.

Django Installation

Django Installation Django development environment consists of installing and setting up Python, Django, and a Database System since Django deals with a web application. Below the statement will guide you through installing Python 3.8.0 and...

2 minutes read.

Django Template System

Django Template System Django allows Python and HTML to be divided, the Python goes into views, and HTML goes into templates. Django relies on the render feature and the language of the Django Model...

5 minutes read.

Creating View in Django

Creating View in Django    Django views are a key component of the frameworkbased applications.We are using Python function or class at their simplest, which takes up a web query and returns a web response.Views are used to get objects from the server, change objects if needed, render types, return HTML, and much more. Class Based View and Function based Django has two types of views: views based on functions (FBVs) and views based on class (CBVs).Initially, Django began with only FBVs, but then introduced CBVs as a way to model features so we didn't have to write boilerplate (i.e. the same software) code again and again.CBVs would be a way to increase efficiency, so you don't have to write as much code as you can. CBVs are classes of Python at their heart. Django ships with a range of "model" CBVs with pre-configured features that can be reused and often expanded.Then helpful names are given to these classes that explain what kind of functionality they provide.These are often referred to as "universal views" because they provide solutions to specific needs. The classes have a journal View function, or "view" for short, is simply a Python program that uses a web query and returns a web response. This response may include the HTML content of a web page, or a redirect, or a 404 error, or an XML file, or an image, etc. Example: You use a view to create a web page, remember that you need to connect a view to a URL to see it as a web page. Simple View We will create a simple view in myapplication to say “Welocome to django application” See the following view – from django.http import HttpResponse ...

4 minutes read.

Django Create App

Django is a Python-based web framework that is based on the Model View Template architecture. In this post, we will go through the installation and setup for our first Django...

4 minutes read.

Django and React

This article will discuss the overview of Django and React, their advantages, applications, and most importantly, how to implement react in Django. What is Django? Django is a free and open-source web...

6 minutes read.

Django Model Fields

In Django, if we want to store the information about anything, we use Models. It contains various fields about the data. Generally, each model represents a database where all the...

4 minutes read.

Django ORM

Django is a Python web framework that enables easy and quick web development. Django has a handy and powerful feature called Django Object Relational Mapper, also known as Django ORM,...

3 minutes read.

Django Model

Django Model A template is a class representing a table or collection in our DB and where each class attribute is a table or collection area. In the app / models.py, models are...

6 minutes read.

Best Django courses

In this tutorial, we will see various courses in python Django available on the internet. Django has become popular these days, so courses on it are also increasing day by...

7 minutes read.

Django Forms

Django is a Python Programming Open Source framework for web development. It was released in 2008 and was developed by two web developers named Adrian Holovaty and Simon Willison. It...

3 minutes read.

Django – Sending E-mails

Django – Sending E-mails Django comes with a light engine that is ready and simple to use to send emails. Similar to Python, you just need to import a smtplib. You just need to import django.core.mail in Django. Edit the project settings.py file to start sending emails and set  all the following options: EMAIL_HOST – server with smtp.EMAIL_HOST_USER – smtp server login key.EMAIL_HOST_PASSWORD ? Password credential for the smtp server.EMAIL_PORT – server port smtp.EMAIL_USE_TLS or _SSL – if it is secure connection then set True. Simple E-mail Sending To submit a simple e-mail, let's create it a "simpleEmail" view. from django.core.mail import...

3 minutes read.

Django Environment Setup

Django is a free and open-source web framework, which uses Python as its programming language. It is based on Model View Template (MVT) architecture. A framework is a collection of...

4 minutes read.

Django Create Superuser

For managing the website, we need an Admin while creating the Django application. In order to create the admin user, create superuser command is used. The superuser is called the...

3 minutes read.

Django Admin

To manage the content of the website, e.g., adding and deleting posts, web developers need an admin portal on the website. The admin portal allows trusted users, also knows as...

3 minutes read.

Django CMS

Django is one of the most popular open-source Python web development framework. Developers use Django to build a website from scratch. Like WordPress, Django CMS is also a Content Management...

9 minutes read.

How to Uninstall Django from cmd

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Django CSRF

Django is a high-level python based web framework. Django is open source and free to use for all. Django provides many features to web developers like scalability, security, rapid development,...

4 minutes read.

Django Stack

Django is a popular Python web application framework that adheres to the "batteries-included" tenet. Batteries-guiding included idea is that common functionality for creating web applications should be provided with the...

5 minutes read.