阿里云Django部署发送QQ邮件

image description

阿里云Django部署发送QQ邮件

写在最前面:

需求描述:博客留言功能,留言成功后使用用户填写的邮箱地址发送邮件,在我阿里云部署的服务器上使用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 = '1524126437@qq.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxx'
EMAIL_FROM = '1524126437@qq.com'

注意这个password不是你的qq邮箱密码,而是授权码

第二步写个函数发送

def sendemail(request):
    email_title = '邮件标题'
    email_body = '邮件内容'
    email = '1524126437@qq.com'  # 对方的邮箱
    send_mail(email_title, email_body, '1524126437@qq.com', [email])
    return render_to_response('myweather.html')

通过url触发发送邮件

然后问题就来了,磨了我一个晚上,好不容易调休一天,头疼。

大家都知道,Django的EMAIL_PORT默认是25,于是我们本地使用25端口发送,成功!

部署到阿里云就是不行。那么我们换成465。结果就是不行

再换587端口,注意,此时的EMAIL_USE_TLS此时一定是True,详见Django的文档。如下:、

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端口。。。。

    ArithmeticJia         0         1451         Django         25    

David Ramon

ArithmeticJia

www.guanacossj.com

Life is Short,You need Python

Related Posts

You may like these post too

image description

用Django写一个自己的博客网站

##写在最前面: 想用Django写网站很久了,本地也建立过很多demo,对于整个框架的入门算是熟练了。用pycharm可 以很方便的新建一个Django工程。专业版的Pycharm是自带Django的,目前Diango的最新版本应该是2.1。 IDE:Pycharm python版本

image description

Django和Flask的区别

###写在最前面: python web开发有很多常用的框架,主要包括Django,Flask,Tornado,Diesel,Cubes,Pulsar,Falcon,webpy,大家最熟知的还是Django和Flask,今天就来简单介绍一下Django和Flask的区别,本博客就是使用Django

Comments on this post

0 comments

Leave a comment

it’s easy to post a comment

image description
image description
image description
image description
image description
image description
image description
image description
image description

Copyright © 2019.Company name All rights reserved.苏ICP备19007197号