~表明你的态度~





当前已表态的用户(0人):
send_email()
方法来发送QQ邮件,但是该方法只能发送普通文本的邮件,并不能识别和发送具有HTML格式的邮件,这样发送的邮件就比较单调,不能得到更好的模板渲染,所以Django提供了EmailMultiAlternatives
方法,该方法可以识别、发送HTML格式的邮件,从而使发送的邮件更加的美观,效果如下(渲染后的样式取决于你的HTML模板代码)from django.core.mail import EmailMultiAlternatives subject, from_email, to = 'hello', 'from@example.com', 'to@example.com' text_content = 'This is an important message.' html_content = '<p>This is an <strong>important</strong> message.</p>' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()其中
subject
是发送邮件的标题,from_email
是发送者的邮箱,需要和settings中配置的邮箱相同,to
是接收者的邮箱,群发邮件使用列表的数据类型列出,text_content
是发送的文本邮件,为了防止接受邮件的设备不能解析查看HTML邮件,html_content
就是需要发送的HTML邮件def send_email_to_me(data): subject, from_email, to = '友链申请', '2551628690@qq.com', '2551628690@qq.com' text_content = '网站名称' + data['web_name'] + '正在申请友链 -- 黄文杨的个人博客' html_content = '<div style="width:700px; margin:auto; padding:auto; font-size: 12px; border: 1px solid #999999;">\ <div style="width:700px; height:52px; background-color: #417690; color: #fff; font-size: 16px; line-height: 52px; font-weight: bolder;text-align: center;">黄文杨的个人博客</div>\ <div style="width: 700px;">\ <div style="width: 700px;height:180px">\ <div style="width: 700px; height: 30px; line-height: 30px; color: #cd3f08; text-indent: 66px;">' + data['web_name'] + '在<span style="border-bottom: 1px dashed rgb <div style="width: 700px; height: 30px; line-height: 30px; text-indent: 66px;">网站名称:<span style="color: #000; font-weight: bolder;">'+ data['web_name'] +'</sp <div style="width: 700px; height: 30px; line-height: 30px; text-indent: 66px;">网站链接:<span style="color: #000; font-weight: bolder;"><a href="'+ data['web_url' <div style="width: 700px; height: 30px; line-height: 30px; text-indent: 66px;"><a href="'+ data['web_url'] +'" rel="noopener" target="_blank"><span style="color: # <div style="width: 700px; height: 30px; line-height: 30px; text-indent: 66px;"><a href="http://www.huangwenyang.cn/agree_friend_url?web_name='+ data['web_name'] + </div>\ </div>\ <div style="width: 700px; height: 137px; background-color: #f2f2f2; border-top: 1px solid #999999;">\ <pre style="margin-left: 45px; margin-top: 15px; line-height: 20px; color: #666666;">注:此邮件为系统邮件,请勿直接回复。\ </pre>\ </div>\ </div>' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()使用时只需要传入相关参数即可