django封装了python自带的发送邮件的功能, 使其更加简单易用。
1、settings中进行配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.163.com' EMAIL_PORT = 465 EMAIL_HOST_USER = '*****@163.com' EMAIL_HOST_PASSWORD = '****' DEFAULT_FROM_EMAIL = EMAIL_HOST_USER2、使用 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 问题解决。