目录结构
2.1.第一步:在【settings.py】里新增邮箱配置信息
2.2.第二步:在【helloworld/hello/views.py】里新增视图函数
2.3.第三步:在【helloworld/helloworld/urls.py】里新增url匹配规则
2.5.第五步:任一浏览器上输入url地址【http://192.168.1.81:8000/send_email_003/】进行访问后,查看结果
1.写这篇博客的目的
主要记录如何通过django来实现这个功能:发送满足【邮件正文值为一个html页面】的单个邮件;
发送满足【邮件正文值为一个html页面】的单个邮件,可以使用该函数:send_mail();
完整操作流程可以看接下来的内容;
2.完整操作流程
2.1.第一步:在【settings.py】里新增邮箱配置信息
# 下面的代码都是我个人新增的,不是djano框架默认生成的; # 1.1.配置qq邮箱信息 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # 值必须为这个 EMAIL_USE_SSL = True # SSL加密方式,值必须为True EMAIL_HOST = 'smtp.qq.com' # 发送邮件的qq邮箱的SMTP服务器 EMAIL_PORT = 465 # QQ邮箱对应的SMTP服务器端口 EMAIL_HOST_USER = '727803257@qq.com' # 发件人 EMAIL_HOST_PASSWORD = 'dwvvnvjerwcvswp' # qq授权码(不能使用qq密码只能使用qq授权码) EMAIL_FROM = '洪景盛<727803257@qq.com>' # 邮件显示的发件人
2.2.第二步:在【helloworld/hello/views.py】里新增视图函数
from django.core.mail import send_mail,send_mass_mail from utils.common import Common common = Common() def send_email_003(request): '''发送邮件正文值为一个html页面的单个邮件''' html_content = ''' <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>带图片的邮件</title> </head> <body> 这是第一张图片,点击图片后可跳转到我的博客首页<br> <a href="https://www.cnblogs.com/xiamen-momo/" target="_blank"> <p> <img src="http://www.w3school.com.cn/i/eg_chinarose.jpg" height="100" width="200" /> </p> </a> <p> 这是第二张图片,点击后不会触发任何跳转效果<br> <img src="http://www.w3school.com.cn/i/eg_chinarose.jpg" height=200 width=400 /> </p> </body> </html> ''' send_mail(subject="这是洪景盛给你们发的邮件的邮件标题!(邮件正文值为一个html页面)", message="这是邮件的正文!(当入参html_message的值不为None,那入参message的值就不会生效)", from_email="727803257@qq.com", recipient_list=["727803257@qq.com"], # 收件人邮箱可以是任意邮箱 # recipient_list=["727803257@qq.com","朋友1的qq@qq.com","朋友2的qq@qq.com","朋友3 的@qq.com"] html_message= html_content ) now_time = common.now_time_of_y_m_d_H_M_S() return HttpResponse("邮件正文值为一个html页面的邮件发送成功!发送时间为:%s"%now_time)
2.3.第三步:在【helloworld/helloworld/urls.py】里新增url匹配规则
url(r"^send_email_003/$",views.send_email_003),
2.4.第四步:重启服务
2.5.第五步:任一浏览器上输入url地址【http://192.168.1.81:8000/send_email_003/】进行访问后,查看结果
3.相关知识点
3.1.send_mail()函数的源码简单分析
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None): """ Easy wrapper for sending a single message to a recipient list. All members of the recipient list will see the other recipients in the 'To' field. If from_email is None, use the DEFAULT_FROM_EMAIL setting. If auth_user is None, use the EMAIL_HOST_USER setting. If auth_password is None, use the EMAIL_HOST_PASSWORD setting. Note: The API for this method is frozen. New code wanting to extend the functionality should use the EmailMessage class directly. """ connection = connection or get_connection( username=auth_user, password=auth_password, fail_silently=fail_silently, ) mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection) if html_message: mail.attach_alternative(html_message, 'text/html') return mail.send()
截取这部分源码:
mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection) if html_message: mail.attach_alternative(html_message, 'text/html') return mail.send()
从这部分源码可以简单看出来:当入参html_message值不为空,会调用【EmailMultiAlternatives】类提供的【attach_alternative】方法,从而实现最终发送邮件正文为一个html页面的邮件的功能;
入参message值和入参html_message值的组合,主要有这两个场景:
- 当函数send_mail()的入参message的值不为空,入参html_message的值也不为空,那么:入参message的值不生效,入参htm_message的值生效;
- 当函数send_mail()的入参message的值不为空,入参html_message的值为默认值None,那么:入参message的值生效,入参htm_message的值不生效;