Reputation: 5073
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.
Upvotes: 1
Views: 7376
Reputation: 161
If anyone is having problems with RFC 5322 compliant emails this ended up working for me:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg.set_unixfrom('author')
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
mailserver.ehlo()
mailserver.login('[email protected]', 'PASSWORD')
mailserver.sendmail('[email protected]','[email protected]',msg.as_string())
mailserver.quit()
Upvotes: 7
Reputation: 1
Here is the overall code that works.
import smtplib`enter code here`
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg.set_unixfrom('author')
msg['From'] = 'your email'
msg['To'] = '[email protected]'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP_SSL('smtpout.secureserver.net', 465)
mailserver.ehlo()
mailserver.login('your email', 'password')
response = mailserver.sendmail('[email protected]','[email protected]',msg.as_string())
mailserver.quit()
Upvotes: 0
Reputation: 305
Joe's code worked for me. The only thing i had to change were the import statements:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Then everything worked! Thanks, Joe.
(Sorry I couldn't leave this as a comment or upvote it - my reputation isn't high enough to comment yet.)
Upvotes: 2
Reputation: 168646
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:
Upvotes: 5