django发送邮件

来源:互联网 发布:手机端图片点击放大js 编辑:程序博客网 时间:2024/05/18 02:33

django封装了python自带的发送邮件的功能, 使其更加简单易用。


1、settings中进行配置

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_USE_TLS = TrueEMAIL_HOST = 'smtp.163.com'EMAIL_PORT = 465EMAIL_HOST_USER = '*****@163.com'EMAIL_HOST_PASSWORD = '****'DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
2、使用 EmailMultiAlternatives 发送邮件

msg.content_subtype = "html"
# 添加附件(可选)
file1 = 'file1path'
file2 = 'file2path'
msg.attach_file(file1)
msg.attach_file(file2)
# 发送
msg.send()

一直报错:

File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

3、修改EMAIL_USE_TLS = True 为EMAIL_USE_SSL = True 问题解决。

原创粉丝点击