This article mainly introduces Django's method of sending html mail, which involves the skills related to Django Framework's email operation. If you need it, refer to the following example to describe Django's method of sending html mail. 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:
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), from_email = EMAIL_HOST_USER, # sender email recipient_list = mail_list, sender = False, auth_user = EMAIL_HOST_USER, # SMTP Server Authentication username auth_password = EMAIL_HOST_PASSWORD, # SMTP server authentication user password 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:
from django.core.mail import EmailMultiAlternativesfrom django.template import Context, loadersubject, from_email, to = title, EMAIL_HOST_USER, mail_listhtml_content = t.render(Context(context))msg = EmailMultiAlternatives(subject, html_content, from_email, to)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:
From django. core. mail import EmailMultiAlternativesfrom django. template import Context, loadersubject, from_email, to = title, EMAIL_HOST_USER, mail_listhtml_content = t. render (Context (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 an attachment to send 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:
From django. core. mail import EmailMultiAlternatives, get_connectionfrom django. template import Context, loaderconn = get_connection () # Return the currently used mail backend instance conn. username = 'my _ email@qq.com '# change the user name conn. password = 'my _ email '# Change password conn. host = 'smtp .exmail.qq.com '# Set the mail server conn. open () # open the connection EMAIL_HOST_USER = 'my _ email@qq.com 'subject, from_email, to = title, EMAIL_HOST_USER, mail_listhtml_content = t. render (Context (context) msg = EmailMultiAlternatives (subject, html_content, from_email, to) msg. attach_alternative (html_content, "text/html") conn. send_messages ([msg,]) # Use send_messages to send the email conn. close () # close the connection after sending the message
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.