This article describes how Django sends an HTML message. Share to everyone for your reference. Specifically as follows:
In Django, sending mail is very convenient, there is no time, today to make a small summary of it.
Our common use of course is to send mail via send_mail:
The code is as follows:
Send_mail (Subject,message,from_email,recipient_list,fail_silently=false,auth_user=none,auth_password
=none,connection=none)
Subject,message,from_email and recipient_list These four parameters are required.
Subject: String that represents the message header.
Message: A string that represents the content of the messages.
From_email: String representing the outgoing mailbox.
Recipient_list: List of strings, each member of the list is a mailbox address, and each recipient sees the other recipients appearing in Recipient_list in the recipient/to: column.
fail_silently: (optional) boolean value. False, Send_mail throws Smtplib. Smtpexception exception. The Smtplib document lists all possible exceptions. These exceptions are smtpexception subclasses of the class.
Auth_User: (optional) The authenticated user name of the SMTP server. If this parameter is not supplied, Django uses the settings for the Email_host_user configuration item.
Auth_password: (optional) The authentication password for the SMTP server, which is not provided, Django uses the settings for the Email_host_password configuration item.
Connection: (optional) Send the back end of the message. If this parameter is not supplied, Django uses the default backend instance.
The following is a simple example:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
From Django.core.mail import Send_mail the Django.template import context, the loader context = {' nickname ': User.nickname, ' 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,# send mailbox Recipient_list=mail_list, Fail_silently=false, Auth_user=email_host_user, # Authentication user name for SMTP server Auth_password=email_host_ PASSWORD, # Authentication user password for SMTP server Connection=none) |
Used people may find that your tags in the template.html are not displayed through browser parsing.
What do we do? We're going to send an HTML email, and Django certainly provides you with a good solution, see the following code:
?
1 2 3 4 5 6 7 |
From Django.core.mail import emailmultialternatives The import context of django.template, loader subject, from_email, to = Title, Email_host_user, Mail_list html_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 do a little bit of change, I need to send the attachment to the recipient, only need to make a simple change can:
?
1 2 3 4 5 6 7 8 |
From Django.core.mail import emailmultialternatives The import context of django.template, loader subject, from_email, to = Title, Email_host_user, Mail_list html_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 to send Msg.send () |
OK, so far, the email is over. However, at this point, the demand has changed, I have been configured on their own web site to send a number of e-mail user name and password, now need to use my specified username and password to send the user e-mail, how to do? See the following code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
From Django.core.mail import emailmultialternatives,get_connection The import context of django.template, loader conn = Get _connection () # Returns the instance of the currently used message backend Conn.username = ' my_email@qq.com ' # change username Conn.password = ' my_email ' # change Password conn.host = ' SMT P.exmail.qq.com ' # Set mail server Conn.Open () # Open Connection Email_host_user = ' my_email@qq.com ' subject, from_email, to = title, Email_h Ost_user, Mail_list html_content = T.render (context) msg = Emailmultialternatives (subject, html_content, from_ email, to) msg.attach_alternative (html_content, "text/html") conn.send_messages ([MSG,]) # We send emails with send_messages Conn.close () # Send complete remember to close the connection |
It seems, it is not very difficult!
Django.core.mail's Get_connection () method returns an instance of the message backend you are currently using.
The code is as follows:
Get_connection (Backend=none,fail_silently=false,*args, **kwargs)
By default, a call to Get_connection () returns an instance of the message back-end, specifically which driven by Email_backend configuration item. If the ' backend ' parameter is specified, the backend is instantiated.
Tips: When you send an HTML message that contains a picture, you should make an address that you can access via HTTP. such as: Http://www.baidu.com/medias/xxx.png
I hope this article will help you with your Python programming.