Zach Zach -4 years ago 141
Python Question

Trying to send email through GoDaddy using Python

Here is the code I am using to send email through GoDaddy:

import smtplib

server = smtplib.SMTP('', 465)
server.login("username", "password")
msg = "Please work!!!!!!"
fromaddr = "fromemail"
toaddr = "toemail"
server.sendmail(fromaddr, toaddr, msg)

When running the script, I get this error:

Traceback (most recent call last):
File "", line 3, in <module>
server = smtplib.SMTP('', 465)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 362, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

I'm really lost on this one, and I know for a fact that my login information is correct.

Answer Source

Replace these two lines:

server = smtplib.SMTP('', 465)

with these two:

server = smtplib.SMTP_SSL('', 465)

Quoting the doc:

SMTP_SSL should be used for situations where SSL is required from the beginning of the connection and using starttls() is not appropriate.

Using port 465 is one of those situations. SMTP.starttls() is appropriate when you use port 25 or port 587.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download