How Django sends HTML messages

Source: Internet
Author: User
The example in this article describes how Django sends HTML messages. Share to everyone for your reference. Specific as follows:

In Django, sending mail is very convenient, there is no time, today to do a small summary of it.

The most common use of course is to send mail via send_mail:
Copy the Code code as follows:

Send_mail (Subject,message,from_email,recipient_list,fail_silently=false,auth_user=none,auth_password=none, Connection=none)

The four parameters of Subject,message,from_email and recipient_list are required.

Subject: String that represents the message header.
Message: A string that represents the message content.
From_email: String that represents the outgoing mailbox.
Recipient_list: A list of strings, each member of the list is a mailbox address, and each recipient sees other recipients in Recipient_list in the to/to: column.
fail_silently: (optional) boolean value. When false, Send_mail throws Smtplib. Smtpexception exception. All possible exceptions are listed in the Smtplib documentation. These exceptions are smtpexception subclasses.
Auth_User: (optional) authentication user name for the SMTP server. If this parameter is not provided, Django uses the settings of the Email_host_user configuration item.
Auth_password: (optional) The authentication password of the SMTP server, without providing this parameter, Django uses the settings of the Email_host_password configuration item.
Connection: (optional) Send the back end of the message. If this parameter is not provided, Django will use the default back-end instance.

The following is a simple example:

From Django.core.mail import send_mail from django.template import context, Loader context = {   ' nickname ': User.nickna Me,   ' verify_url ': verify_url,} email_template_name = ' template.html ' t = loader.get_template (email_template_name) Mail_list = [User.email,] Send_mail (   subject=title,   message=t.render (context),   from_email= email_host_user,# e-mail   recipient_list=mail_list,   fail_silently=false,   Auth_user=email_host_user, # SMTP Server Authentication user name   Auth_password=email_host_password, # SMTP server Authentication user password   connection=none)

Used people may find that your tags in template.html are not displayed by browser parsing.

What to do? We're sending HTML messages, and Django is certainly providing you with a good solution, see the following code:

From Django.core.mail import emailmultialternativesfrom django.template import Context, Loadersubject, from_email, to = t Itle, email_host_user, mail_listhtml_content = T.render (context) msg = Emailmultialternatives (subject, Html_ Content, From_email, to) msg.attach_alternative (html_content, "text/html") Msg.send ()

Look, very simple, so now I want to make a little bit of change, I need to send an attachment to the recipient, only need to make a simple change:

From Django.core.mail import emailmultialternativesfrom django.template import Context, Loadersubject, from_email, to = t Itle, email_host_user, mail_listhtml_content = T.render (context) msg = Emailmultialternatives (subject, Html_ Content, From_email, to) msg.attach_alternative (html_content, "text/html") msg.attach_file (U ' d:/my documents/python/ Doc/test.doc ') # Add attachment send Msg.send ()

OK, so far, sending the mail is over. However, at this time, the demand has changed, I configured on my own website multiple email user name and password, now I need to use the user name and password I specified to send the user mail, what should I do? Take a look at the following section of code:

It seems that it is not very difficult!

The Django.core.mail get_connection () method returns an instance of the mail backend that you are currently using.
Copy the Code code as follows:

Get_connection (Backend=none,fail_silently=false,*args, **kwargs)


By default, a call to Get_connection () returns a message back-end instance, which is determined by which driven by Email_backend configuration item. If the ' backend ' parameter is specified, the backend is instantiated.

Tip: When you include a picture in an HTML message that you send, you should make an address that you can access via HTTP. such as: Http://www.baidu.com/medias/xxx.png

Hopefully this article will help you with Python programming.

  • 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.