python smtp starttls() is giving me a SMTPServerDi

2019-08-24 04:07发布

Follow is my code from the python manage.py shell and the error that appears. I am testing this because django send_mail was giving me the same error so I tried to reproduce it in the shell

>>> import smtplib
>>> server = smtplib.SMTP('smtp.gmail.com', 587)
>>> server.set_debuglevel(1)
>>> server.ehlo()
send: 'ehlo PKL-FKHAN-LT.mgc.mentorg.com\r\n'
reply: '250-smtp.gmail.com at your service, [58.27.158.222]\r\n'
reply: '250-SIZE 35882577\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-CHUNKING\r\n'
reply: '250 SMTPUTF8\r\n'
reply: retcode (250); Msg: smtp.gmail.com at your service, [58.27.158.222]
SIZE 35882577
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
PIPELINING
CHUNKING
SMTPUTF8
(250, 'smtp.gmail.com at your service, [58.27.158.222]\nSIZE 35882577\n8BITMIME\
nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
>>> server.starttls()
send: 'STARTTLS\r\n'
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\smtplib.py", line 645, in starttls
    (resp, reply) = self.docmd("STARTTLS")
  File "C:\Python27\lib\smtplib.py", line 394, in docmd
    return self.getreply()
  File "C:\Python27\lib\smtplib.py", line 365, in getreply
    + str(e))
SMTPServerDisconnected: Connection unexpectedly closed: [Errno 10054] An existin
g connection was forcibly closed by the remote host

I went through all the questions and most problems arise after this step, so I must be missing something very basic here.

EDIT 1 Following are the email setting in the settings.py

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'removed' #this is my gmails account email
EMAIL_HOST_PASSWORD = "removed" #this is the gmails account password
SERVER_EMAIL = "same as the EMAIL_HOST_USER"
DEFAULT_FROM_EMAIL=EMAIL_HOST_USER

EDIT 2: I just tested it on a another computer using python 3. the same error. This means that there is some basic fault in my workflow.

1条回答
甜甜的少女心
2楼-- · 2019-08-24 04:49

So for the purpose of completion, I am answering my own question.

It was an ISP issue, not sure why gmail wont work with some ISP, read somewhere that is has to do with gmail backchecking IP of the sender.

Anyhow if anyone is seeing some form of error before even connecting then its a good idea to change your ISP...

查看更多
登录 后发表回答