from django.core import mail mail.send_mail
class EmailMessage(object):
"""
A container for email information.
"""
content_subtype = 'plain'
mixed_subtype = 'mixed'
encoding = None # None => use settings default
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
connection=None, attachments=None, headers=None, cc=None,
reply_to=None):
"""
Initialize a single email message (which can be sent to multiple
recipients).
All strings used to create the message can be unicode strings
(or UTF-8 bytestrings). The SafeMIMEText class will handle any
necessary encoding conversions.
"""
msg = mail.EmailMessage('富文本邮件测试', html, from_mail, recipient_list)
if to:
if isinstance(to, six.string_types):
raise TypeError('"to" argument must be a list or tuple')
self.to = list(to)
self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
# email config # 这是我们工程目录下settings文件的配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST_PASSWORD = 'xxxxxxxx' EMAIL_HOST_USER = 'xxxx@xxxx.xxxx' EMAIL_HOST = 'smtp.xxxx.com' EMAIL_PORT = port
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgText = MIMEText(plainText, 'plain', 'utf-8')
msgAlternative.attach(msgText)
#设定HTML信息
msgText = MIMEText(htmlText, 'html', 'utf-8')
msgAlternative.attach(msgText)
def message(self):
encoding = self.encoding or settings.DEFAULT_CHARSET
msg = SafeMIMEText(self.body, self.content_subtype, encoding)
msg = self._create_message(msg)
msg['Subject'] = self.subject
msg['From'] = self.extra_headers.get('From', self.from_email)
msg['To'] = self.extra_headers.get('To', ', '.join(map(force_text, self.to)))
# -*- coding: utf-8 -*-
# ===============================================================================
# @ Creator:首席小学生
# @ Date:2017-03-28
# 构造富文本邮件内容
# ===============================================================================
import os
from email.mime.image import MIMEImage
from django.conf import settings
from django.core import mail
def add_img(src, img_id):
"""
在富文本邮件模板里添加图片
:param src:
:param img_id:
:return:
"""
fp = open(src, 'rb')
msg_image = MIMEImage(fp.read())
fp.close()
msg_image.add_header('Content-ID', '<'+img_id+'>')
return msg_image
def send_util():
path = os.getcwd()
path_use = path.replace('\\', '/')
html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
牛逼呀小伙子,你成功了
<img src="cid:test_cid"/>
</body>
</html>
'''
recipient_list = ['xxxx@xxxx.com']
from_mail = settings.EMAIL_HOST_USER
msg = mail.EmailMessage('富文本邮件测试', html, from_mail, recipient_list)
msg.content_subtype = 'html'
msg.encoding = 'utf-8'
image = add_img(path_use+'/mail_util/test.png', 'test_cid')
msg.attach(image)
if msg.send():
return True
else:
return False
from django.shortcuts import render
from html_to_mail import send_util
def send_mail(request):
if send_util():
return render(request, 'ok.html')
else:
return render(request, 'error.html')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有