django Email

相关文档:https://www.cnblogs.com/Lin-Yi/p/7590855.html

 

一、相关配置: 以QQ为例

1、邮箱开通stmp服务  开通成功会自动弹出授权码弹框

 

2、setting配置:

# Email设置
# 管理员邮箱
ADMINS = (
    ('xxxx', 'xxxxx'),
)
# 非空链接,却发生404错误,发送通知MANAGERS
SEND_BROKEN_LINK_EMAILS = True
MANAGERS = ADMINS
 
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # 固定写法
EMAIL_HOST = 'smtp.qq.com'                                # QQ邮箱SMTP服务器(邮箱需要开通SMTP服务)
EMAIL_PORT = 25                                           # QQ邮箱SMTP服务端口
EMAIL_HOST_USER = 'xxxx@qq.com'                           # 我的邮箱帐号
EMAIL_HOST_PASSWORD = 'medxhyzcxodcdjhc'                  # 授权码 - 密码  xamniqaasycsdjag / medxhyzcxodcdjhc
EMAIL_SUBJECT_PREFIX = 'django website'                   # 为邮件标题的前缀,默认是'[django]'
EMAIL_USE_TLS = True                                      # 开启安全链接
# DEFAULT_FROM_EMAIL = SERVER_EMAIL = EMAIL_HOST_USER     # 设置发件人
EMAIL_FROM = 'xxxx@qq.com'                                # 你的 QQ 账号

二、内容发送

1、简单内容发送

from django.conf import settings
from django.core.mail import send_mail

def sendEmial(self):
    email_title = '邮件标题'
    email_body = '邮件内容'
    email_to = 'zhaxxxxl@grzq.com'
    msg = "测试邮件发送!"
    send_status = send_mail(email_title, email_body, settings.EMAIL_FROM, [email_to], html_message=msg)

    if send_status:
        ret = {
            'success': True,
            'retCode': 200,
            'retMsg': "邮件添发送成功!"
        }
        return HttpResponse(json.dumps(ret), content_type='application/json')

2、html格式内容发布

import smtplib
from email.header import Header
from email.mime.text import MIMEText

def sendEmial(self):
    # html 格式内容发送
    # 发件人和收件人
    sender = '2495xxxxxx@qq.com'
    receiver = 'zxxxxxx@grzq.com'

    # 所使用的用来发送邮件的SMTP服务器
    smtpserver = 'smtp.qq.com'

    # 发送邮箱的用户名和授权码(不是登录邮箱的密码)
    username = '2495xxx@qq.com'
    password = 'medxxxxxxxxjhc'

    # 邮件主题
    mail_title = '主题:python邮件发送测试'

    # 读取html文件内容
    f = open('views/index.html', 'rb')  # HTML文件默认和当前文件在同一路径下,若不在同一路径下,需要指定要发送的HTML文件的路径
    mail_body = f.read()   # mail_body type: <class 'bytes'>
    f.close()
    
       #  手写html格式 设置邮件格式:
    mail_body = """
         <h2>测试邮件内容html格式:</h2>
         <a href="www.baidu.com">链接导航</a>
         <p>测试段落,文本</p>
    """

    # 邮件内容, 格式, 编码
    message = MIMEText(mail_body, 'html', 'utf-8')
    message['From'] = sender
    message['To'] = receiver
    message['Subject'] = Header(mail_title, 'utf-8')

    try:
        smtp = smtplib.SMTP()
        smtp.connect('smtp.qq.com')
        smtp.login(username, password)
        smtp.sendmail(sender, receiver, message.as_string())
        print("发送邮件成功!!!")
        smtp.quit()
        return True
    except smtplib.SMTPException:
        logging.error("发送邮件失败!!!")
        return False

 

3、带附件发送 https://www.cnblogs.com/visec479/p/4142818.html

# 发送邮件
from django.conf import settings
from django.core.mail import send_mail
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def sendEmial(self):
    email_title = '邮件标题'
    email_body = '邮件内容'
    email_to = 'zxxxxl@gxxx.com'
    cclist = ['xxxxx5@163.com']    # 抄送人
    msg = "测试邮件发送!"

    # html 格式内容发送
    # 发件人和收件人
    sender = '24xxxx00@qq.com'
    receiver = 'xxxxl@gxxxxq.com'

    # 所使用的用来发送邮件的SMTP服务器
    smtpserver = 'smtp.qq.com'

    # 发送邮箱的用户名和授权码(不是登录邮箱的密码)
    username = '24xxxx0@qq.com'
    password = 'medxxxxxxxxjhc'

    # 邮件主题
    mail_title = '主题:python邮件发送测试'

    # 创建一个带附件的实例
    message = MIMEMultipart()

    # 构造附件1
    att1 = MIMEText(open('media/files/20190117/订饭.txt', 'rb').read(), 'html', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="OIDFrom.doc"'  # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    message.attach(att1)

    # 构造附件2
    att2 = MIMEText(open('media/files/20190117/1111.txt', 'rb').read(), 'html', 'utf-8')
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename="OIDFrom.txt"'
    message.attach(att2)

    # 邮件内容, 格式, 编码
    message['From'] = sender
    message['To'] = receiver
    message['Subject'] = Header(mail_title, 'utf-8')

    try:
        smtp = smtplib.SMTP()
        smtp.connect('smtp.qq.com')
        smtp.login(username, password)
        smtp.sendmail(sender, receiver, message.as_string())
        print("发送邮件成功!!!")
        smtp.quit()
        return True
    except smtplib.SMTPException:
        logging.error("发送邮件失败!!!")
        return False

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值