Search code examples
pythonemailsmtp

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('smtpout.secureserver.net', 465)
server.starttls()
server.ehlo()
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 "emailTest.py", line 3, in <module>
server = smtplib.SMTP('smtpout.secureserver.net', 465)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py",      line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", 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.


Solution

  • Replace these two lines:

    server = smtplib.SMTP('smtpout.secureserver.net', 465)
    server.starttls()
    

    with these two:

    server = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
    #server.starttls()
    

    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.

    References: