django中发送html邮件
settings.py文件中添加配置:
EMAIL_HOST=‘smtp.163.com‘
EMAIL_HOST_USER=‘[email protected]‘
EMAIL_HOST_PASSWORD=‘password‘
EMAIL_USE_TLS = True
方法1:
# -*- coding: utf-8 -*- from django.conf import settings settings.configure() from django.core.mail import send_mail from django.conf.global_settings import EMAIL_HOST_USER send_mail(‘hello‘,‘just a test!‘,EMAIL_HOST_USER,[‘收件人列表‘])
方法2:
# -*- coding: utf-8 -*- from django.conf import settings settings.configure() from django.core.mail import EmailMessage from django.conf.global_settings import EMAIL_HOST_USER subject, from_email, to = ‘hello‘, EMAIL_HOST_USER, ‘[收件人列表]‘ html_content = "<p>This is an <font size=3 color=‘green‘><strong>important</strong></font> message.</p>" msg = EmailMessage(subject, html_content, from_email, [to]) msg.content_subtype = ‘html‘ msg.send()
方法3:
# -*- coding: utf-8 -*- from django.conf import settings settings.configure() from django.core.mail import EmailMultiAlternatives from django.conf.global_settings import EMAIL_HOST_USER subject, from_email, to = ‘hello‘, EMAIL_HOST_USER, ‘[收件人列表]‘ text_content = ‘This is an important message.‘ html_content = "<p>This is an <font size=3 color=‘green‘><strong>important</strong></font> message.</p>" msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()
可以看到以html显示的邮件:
——————————————————分割线————————————————
同时发送多封邮件:
# -*- coding: utf-8 -*- from django.conf import settings settings.configure() from django.core.mail import EmailMessage,get_connection from django.conf.global_settings import EMAIL_HOST_USER connection = get_connection() connection.open() html_content1 = "<p><strong>important </strong>message 1.</p>" html_content2 = "<p><strong>important </strong>message 2.</p>" email1 = EmailMessage(‘Hello‘, html_content1, EMAIL_HOST_USER, [‘收件人1‘]) email2 = EmailMessage(‘Hello‘, html_content2, EMAIL_HOST_USER, [‘收件人2‘]) email1.content_subtype = ‘html‘ email2.content_subtype = ‘html‘ connection.send_messages([email1, email2]) connection.close()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。