写在最前面:
想在我阿里云部署的服务器上使用Django自带的发送邮件的功能,我选择发送QQ邮件。
第一步配置Settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 25
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxx'
EMAIL_FROM = '[email protected]'
注意这个password不是你的qq邮箱密码,而是授权码
第二步写个函数发送
def sendemail(request):
email_title = '邮件标题'
email_body = '邮件内容'
email = '[email protected]' # 对方的邮箱
send_mail(email_title, email_body, '[email protected]', [email])
return render_to_response('myweather.html')
通过url触发发送邮件
然后问题就来了,磨了我一个晚上,好不容易调休一天,头疼。
大家都知道,Django的EMAIL_PORT默认是25,于是我们本地使用25端口发送,成功!
部署到阿里云就是不行。那么我们换成465。结果就是不行
EMAIL_USE_TLS
¶Default: False
Whether to use a TLS (secure) connection when talking to the SMTP server. This is used for explicit TLS connections, generally on port 587. If you are experiencing hanging connections, see the implicit TLS setting EMAIL_USE_SSL
.
EMAIL_USE_SSL
¶Default: False
Whether to use an implicit TLS (secure) connection when talking to the SMTP server. In most email documentation this type of TLS connection is referred to as SSL. It is generally used on port 465. If you are experiencing problems, see the explicit TLS setting EMAIL_USE_TLS
.
Note that EMAIL_USE_TLS
/EMAIL_USE_SSL
are mutually exclusive, so only set one of those settings to True
.
说起来轻松,一开始是想着阿里云给解封25端口,结果客服说我是轻量级服务器不给解封25端口。。。。