django发送带附件、图像、html格式的邮件

1. 效果图:

django发送带附件、图像、html格式的邮件_第1张图片

 

2. settings.py配置项

# 发送邮件配置项
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# smpt服务器地址
EMAIL_HOST = 'smtp.163.com'
# 端口
EMAIL_PORT = 25
# 发送邮件的邮箱
EMAIL_HOST_USER = '[email protected]'
# 在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = 'XPTLZDPKAWWHTSLI'
# 收件人看到的发件人
EMAIL_FROM = 'mayanan'
# 报错此项必须加上
DEFAULT_FROM_EMAIL = '[email protected]'

3. views.py代码

from django.shortcuts import render, HttpResponse
from django.core.mail import send_mail, EmailMultiAlternatives
from django.conf import settings
from email.header import make_header
from email.mime.text import MIMEText  #html格式和文本格式邮件
from email.mime.image import MIMEImage  #带图片格式邮件
import os


def send_common_email(request):
    # 发邮件(subject:邮件主题, message:邮件内容, from_email:发件人, recipient:收件人列表, html_message:带有Html标签的邮件内容)
    subject = "天天生鲜欢迎信息"
    message = "哈哈哈哈哈"
    from_email = settings.EMAIL_FROM
    recipient_list = ["[email protected]"]
    ret = send_mail(subject, message, from_email, recipient_list)

    return HttpResponse(ret)


def send_multi_email(request):
    print('**************开始生成消息*********************')
    subject = '报告邮件'
    text_content = '这是一封重要的报告邮件.'
    html_content = '

尊敬的先生/女士您好

这是一封重要的机密文件,请查阅后及时销毁。

' from_email = settings.DEFAULT_FROM_EMAIL receive_email_addr = ["[email protected]"] msg = EmailMultiAlternatives(subject, text_content, from_email, receive_email_addr) msg.attach_alternative(html_content, "text/html") # 发送图像 html1 = "
" msg_html_img = MIMEText(html1, 'html', 'utf-8') msg.attach(msg_html_img) file_path = os.path.join(settings.BASE_DIR, "static/kd.png") with open(file_path, "rb") as f: msg_img = MIMEImage(f.read()) msg_img.add_header('Content-ID', 'imgid') # 扩展图片标题 msg.attach(msg_img) # 发送附件 print('********************发送附件txt********************') file_path = os.path.join(settings.BASE_DIR, "日志.txt") text = open(file_path, 'rb').read() file_name = os.path.basename(file_path) # 对文件进行编码处理 b = make_header([(file_name, 'utf-8')]).encode('utf-8') msg.attach(b, text) print('********************发送附件jpg********************') file_path = os.path.join(settings.BASE_DIR, "test.jpg") text = open(file_path, 'rb').read() file_name = os.path.basename(file_path) # 对文件进行编码处理 b = make_header([(file_name, 'utf-8')]).encode('utf-8') msg.attach(b, text) print('********************发送附件xlsx********************') file_path = os.path.join(settings.BASE_DIR, "test.xlsx") text = open(file_path, 'rb').read() file_name = os.path.basename(file_path) # 对文件进行编码处理 b = make_header([(file_name, 'utf-8')]).encode('utf-8') msg.attach(b, text) # msg.attach_file(file_path) msg.send() if msg.send(): print('******************发送成功*********************') else: print('******************发送失败*********************') print('********************发送完成********************') return HttpResponse("发送完成")

4. urls.py代码

from django.urls import path
from . import views

urlpatterns = [
    path("send_common_email/", views.send_common_email, name="send_common_email"),  # 发送普通邮件
    path("send_multi_email/", views.send_multi_email, name="send_multi_email"),  # 发送带附件和html格式邮件
]

5. 更多详细教程见链接:https://www.cnblogs.com/zhangxinqi/p/9113859.html#_label4

你可能感兴趣的:(Django)