How does Django send html emails?
This article describes how Django sends html emails. Share it with you for your reference. The details are as follows:
In Django, sending emails is very convenient and there is no time. Let's make a summary today.
We usually use send_mail to send Emails:
The Code is as follows:
Send_mail (subject, message, from_email, recipient_list, fail_silently = False, auth_user = None, auth_password
= None, connection = None)
The subject, message, from_email, and recipient_list parameters are required.
Subject: String, indicating the mail title.
Message: String, indicating the mail content.
From_email: String, indicating the sender's mailbox.
Recipient_list: String list. Each member in the list is an email address, and each recipient will see other recipients in the "recipient/To:" column in the recipient_list.
Fail_silently: (optional) Boolean value. If the value is False, send_mail throws an smtplib. SMTPException. The smtplib document lists all possible exceptions. These exceptions are subclasses of SMTPException.
Auth_user: (optional) authentication username of the SMTP server. If this parameter is not provided, Django uses the EMAIL_HOST_USER configuration item settings.
Auth_password: (optional) authentication password of the SMTP server. If this parameter is not provided, Django uses the EMAIL_HOST_PASSWORD configuration item.
Connection: (optional) the backend of the email. If this parameter is not provided, 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 From django. template import Context, 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 (context )), From_email = EMAIL_HOST_USER, # mail address Recipient_list = mail_list, Fail_silently = False, Auth_user = EMAIL_HOST_USER, # authentication username of the SMTP server Auth_password = EMAIL_HOST_PASSWORD, # authentication user password of the SMTP server Connection = None ) |
Users can find that the tags in template.html are not displayed through browser parsing.
What should I do? We want to send an HTML email. Django certainly provides a good solution for you. Please refer to the following code:
?
1 2 3 4 5 6 7 |
From django. core. mail import EmailMultiAlternatives From django. template import Context, loader Subject, from_email, to = title, EMAIL_HOST_USER, mail_list Html_content = t. render (Context (context )) Msg = EmailMultiAlternatives (subject, html_content, from_email,) Msg. attach_alternative (html_content, "text/html ") Msg. send () |
Look, it's easy. Now I want to make a little change. I need to send the attachment to the recipient, just make a simple modification:
?
1 2 3 4 5 6 7 8 |
From django. core. mail import EmailMultiAlternatives From django. template import Context, loader Subject, from_email, to = title, EMAIL_HOST_USER, mail_list Html_content = t. render (Context (context )) Msg = EmailMultiAlternatives (subject, html_content, from_email,) Msg. attach_alternative (html_content, "text/html ") Msg. attach_file (u 'd:/My Documents/Python/doc/test.doc ') # Add attachments for sending Msg. send () |
Now, the email has been sent. However, at this time, the demand has changed. I have configured multiple email sending usernames and passwords on my website. Now I need to use the user name and password I specified to send emails to users, what should I 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 From django. template import Context, loader Conn = get_connection () # Return the backend instance of the currently used email Conn. username = 'my _ email@qq.com '# Change User Name Conn. password = 'my _ email '# Change password Conn. host = 'smtp .exmail.qq.com '# Set the email server Conn. open () # open a connection EMAIL_HOST_USER = 'my _ email@qq.com' Subject, from_email, to = title, EMAIL_HOST_USER, mail_list Html_content = t. render (Context (context )) Msg = EmailMultiAlternatives (subject, html_content, from_email,) Msg. attach_alternative (html_content, "text/html ") Conn. send_messages ([msg,]) # Use send_messages to send emails Conn. close () # close connection after sending |
It seems that it is not very difficult!
The get_connection () method of django. core. mail returns the backend instance of the email you are currently using.
The Code is as follows:
Get_connection (backend = None, fail_silently = False, * args, ** kwargs)
By default, the call to get_connection () will return a mail backend instance. The specific backend is determined by the EMAIL_BACKEND configuration item. If the ''backend'' parameter is specified, the backend is instantiated.
Tip: When an HTML email contains an image, you must specify an address that can be accessed through http. Such as: http://www.baidu.com/medias/xxx.png
I hope this article will help you with Python programming.