How to send emails in Django applications

Source: Internet
Author: User

How to send emails in Django applications

Preface

The most common use cases for sending emails in Django applications are password reset, Account Activation, and general notifications related to your applications. Let's take a look at the detailed introduction.

Configure Django to send email

To configure your Django application, add the following parameters to your settings. py:

# Host EMAIL_HOST = "smtp.sina.com" # port EMAIL_PORT = 25 # sender email EMAIL_HOST_USER = "anshengme@sina.com" # password EMAIL_HOST_PASSWORD = "ansheng. me "# Use htt?mail_use_tls = False # sender EMAIL_FROM =" ansheng"

Send email

First looksend_mail()The method provides the following parameters:

Parameters Description
Subject Email Subject
Message Email body
From_email Sender
Recipient_list Recipient List
Fail_silently Boolean value,
Auth_user Optional user name used for authentication to the SMTP server. If this item is not provided, Django will use the value set by EMAIL_HOST_USER
Auth_password Optional password used for authentication to the SMTP server. If this option is not provided, Django uses the value set by EMAIL_HOST_PASSWORD.
Connection Optional email backend for sending mail. If this parameter is not specified, the default backend instance is used.
Html_message If html_message is provided, the generated email is a multi-part/alternative email. The message type is text/plain, and html_message is text/html.

Then we enter the python interpreter with the django shell environment variable, and then send an email to try it?

$ Python3 manage. py shellIn [1]: from django. core. mail import send_mailIn [2]: send_mail ("this is the mail title", "this is the mail subject", 'anshengme @ sina.com ', ['ianshengme @ gmail.com']) # The returned value is the number of successfully passed messages (it can be 0 or 1, because it can only send one message) Out [2]: 1

Open the email address to check whether the email has been received?

Send multiple emails at the same time

send_mass_mail()Provided parameter value

Attribute Description
Datatuple Receives a single tuples. Each element is in the format of (subject, message, from_email, recipient_list ).

Xiao Chestnut

In [1]: message1 = ("This is the first mail title", "This is the first mail subject", 'anshengme @ sina.com ', ['ianshengme @ gmail.com']) in [2]: message2 = ("this is the second mail title", "this is the second mail subject", 'anshengme @ sina.com ', ['ianshengme @ gmail.com']) in [3]: from django. core. mail import send_mass_mailIn [4]: send_mass_mail (message1, message2), fail_silently = False) Out [4]: 2

The returned value is the number of messages successfully sent.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.