Django Sending E-mails - Django

How to send E-mails in Django?

Django comes with a geared up and clean-to-use light engine to send E-mail. Similar to Python you just need an import of smtplib. In Django you simply need to import django.center.e mail. to begin sending E-mail edit your venture settings.py file and set the following alternatives −

  • EMAIL_HOST − smtp server.
  • EMAIL_HOST_USER − Login credential for the smtp server.
  • EMAIL_HOST_PASSWORD − Password credential for the smtp server.
  • EMAIL_PORT − smtp server port.
  • EMAIL_USE_TLS or _SSL − actual if at ease connection.

Sending a easy E-mail

Let’s create a "sendSimpleEe mail" view to send a simple E-mail


Here are the details of the parameters of send_E mail −

  • subject − E-mail situation.
  • message − E-mailframe.
  • from_e mail − E-mail from.
  • recipient_list − list of receivers’ E-mail address.
  • fail_silently − Bool, if false send_e-mail will boost an exception in case of blunders.
  • auth_user − person login if no longer set in settings.py.
  • auth_password − consumer password if now not set in settings.py.
  • connection − E-mail backend.
  • html_message − (new in Django 1.7) if present, E-mail could be multipart/alternative.

Let’s create a URL to get admission to our view –

So while gaining access to /myapp/simpleeelectronic mail/polo@gelectronic mail.com, you may get the following web page −
sending e-mails-1

Sending multiple Mails with send_mass_e-mail

The approach returns the number of messages correctly introduced. that is same as send_e mail but takes a further parameter; datatuple, our sendMassEelectronic mailemail view will then be –


Allow create a URL to get admission to our view –


While getting access to /myapp/masseelectronic mailemail/polo@ge-mail.com/sorex@ge mail.com/, we get −

sending e-mails-2

send_mass_electronic mailemail parameters information are −

  • datatuples − A tuple where each element is like (concern, message, from_eelectronic mailemail, recipient_list).
  • fail_silently − Bool, if fake send_the eeemail will boost an exception in case of blunders.
  • auth_user − person login if no longer set in settings.py.
  • auth_password − person password if no longer set in settings.py.
  • connection − E-mail backend.

As you can see within the above image, two messages were sent effectively.

Be Aware − In this situation we're using Python smtp debuggingserver, that you could release the use of –

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

Sending e-e mails to admins and managers the use of electronic mailemail_admins and e mail_managers strategies

Those techniques send e-electronic mails to web site directors as described in the ADMINS option of the settings.py report, and to web site managers as defined in MANAGERS alternative of the settings.py document. allow's assume our ADMINS and bosses options look like −

ADMINS = (('polo', 'polo@polo.com'),)

MANAGERS = (('popoli', 'popoli@polo.com'),)


The above code will send an E-mail to every admin defined inside the ADMINS phase.

The above code will send an E-mail to each manager defined within the MANAGERS phase.

Parameters details −

  • challenge − E-mail concern.
  • message − E-mail frame.
  • fail_silently − Bool, if false send_electronic mail will enhance an exception in case of errors.
  • connection − E-mail backend.
  • html_message − (new in Django 1.7) if present, E-mail may be multipart/opportunity.

Sending HTML E-mail

Sending HTML message in Django >= 1.7 is as easy as –


This will produce a multipart/alternative E-mail.

However for Django < 1.7 sending HTML messages is completed via the django.center.e mail.Ee mailMessage magnificence then calling 'send' at the item −

Let’s create a "sendHTMLEe-mail" view to ship an HTML E-mail.


Parameters details for the Ee mailMessage elegance introduction –

  • issue − E-mail challenge.
  • message − E-mail body in HTML.
  • from_eelectronic mail − E-mail from.
  • to − listing of receivers’ E-mail cope with.
  • bcc − listing of “Bcc” receivers’ E-mail deal with.
  • connection − E-mail backend.

Let’s create a URL to get right of entry to our view –

While having access to /myapp/htmleelectronic mailemail/polo@ge-mail.com, we get –

sending e-mails-3

Sending E-mail with Attachment

This is performed through using the 'connect' technique on the E mailMessage item.

A view to send an E-mail with attachment might be –

Info on attached arguments −

filename − The call of the report to attach.
content − The content of the document to connect.
mimetype − The attachment's content mime type.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Django Topics