django 发送邮件遇到的那些坑

官网测试用例

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    '[email protected]',
    ['[email protected]'],
    fail_silently=False,
)

成功返回1,不成功返回0或者报错,经测试,用QQ个人邮箱和163个人邮箱均可发送成功


最大的坑:当你在shell里进行测试,如果报错,在修改代码后,需要退出shell,重新测试,否则基本还报原来的错,因为你重新加载python manage.py shell的时候才会调用你更改settings里文件的配置。

邮箱未开启SMTP/POP3/IMAP等服务

smtplib.SMTPAuthenticationError: (550, b'User has no permission')

验证失败,可能是因为使用的是原始登录密码,应该使用客户端授权密码,或者EMAIL_HOST填写错误

smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')

启用SSL,但是端口写错

ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:833)

EMAIL_USE_SSLEMAIL_USE_TLS只能选一个为True:

ValueError: EMAIL_USE_TLS/EMAIL_USE_SSL are mutually exclusive, so only set one
of those settings to True.

不启用SSL,但是端口写错

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

settings配置示例

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
#EMAIL_USE_SSL = True 
#EMAIL_PORT = 465
EMAIL_USE_TLS = True   #是否使用TLS安全传输协议
EMAIL_PORT = 25
EMAIL_HOST = 'smtp.qq.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourpassword'

你可能感兴趣的:(django 发送邮件遇到的那些坑)