Connection Error SMTP python
Solution 1
all sorted took a few idea and tried the code below
msg = MIMEText ('%s - %s' % (msg.text, msg.channel))
server = smtplib.SMTP('smtp.gmail.com')
server.starttls()
server.login('user','pass')
msg['Subject'] = "msg.channel"
msg['From'] = ('from')
msg['To'] = ('to')
server.sendmail(msg.get('From'),msg["To"],msg.as_string())
server.quit()
So i removed ehlo(), close() and port number. now i have to workout how to change the subject to msg.channel so it changes each time.
thanks all
Solution 2
Try using SMTP's empty constructor, then call connect(host, port)
:
server = smtplib.SMTP()
server.connect('smtp.gmail.com', '587')
server.ehlo()
server.starttls()
server.login(username, password)
Solution 3
You have an ehlo
after close
. That seems unlikely to ever succeed. Also, quit
does close
so you can probably just get rid of the ehlo
and close
calls near the end
Solution 4
You can still have an encrypted connection with the smtp server by using the SMTP_SSL class without needing the starttls call (shorter). You don't need to be calling the ehlo every time, that's done automatically when needed, and when connecting to the default port, don't have to supply one when creating instances SMTP* classes.
msg = MIMEText ('%s - %s' % (msg.text, msg.channel))
msg['To'] = ','.join(receivers)
msg['Subject'] = 'msg.channel'
msg['From'] = '[email protected]'
Using SMTP with the starttls:
server = smtplib.SMTP('smtp.gmail.com')
server.starttls()
server.login('user', 'password')
server.sendmail(msg['From'], receivers, msg.as_string())
and now with the SMTP_SSL class
server = smtplib.SMTP_SSL('smtp.gmail.com')
server.login('user', 'password')
server.sendmail(msg['From'], receivers, msg.as_string())
and finally
server.quit()
Solution 5
For Pyhton 3.6.*
Note : In gmail it will work only if 2-Step verification is turned off.
Allow gmail to open via low secured app.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from_addr = 'sender-email-id'
to_addr = 'receiver-email-id'
text = 'Hi Friend!!!'
username = 'sender-username'
password = 'password'
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'Test Mail'
msg.attach(MIMEText(text))
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(from_addr,to_addr,msg.as_string())
server.quit()
Shaggy89
Updated on February 21, 2021Comments
-
Shaggy89 about 3 years
I've been using python for a bit now and have been using the email function without any errors in the past but on the latest program I have made I've been getting this error
Traceback (most recent call last): File "daemon.py", line 62, in <module> scraper.run() File "c:\cfsresd\scraper.py", line 48, in run self.scrape() File "c:\cfsresd\scraper.py", line 44, in scrape handler(msg) File "daemon.py", line 57, in handler server.ehlo() File "C:\Python27\lib\smtplib.py", line 385, in ehlo self.putcmd(self.ehlo_msg, name or self.local_hostname) File "C:\Python27\lib\smtplib.py", line 318, in putcmd self.send(str) File "C:\Python27\lib\smtplib.py", line 310, in send raise SMTPServerDisconnected('please run connect() first') smtplib.SMTPServerDisconnected: please run connect() first
I used the same email code for all my projects but this is first time is done it. I've tried adding the connect() but that made no difference. Below is email section of my script
msg = MIMEText ('%s - %s' % (msg.text, msg.channel)) server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() msg['Subject'] = "msg.channel" msg['From'] = ('removed') msg['To'] = ('removed') server.login('user','password') server.sendmail(msg.get('From'),msg["To"],msg.as_string()) server.close() server.ehlo() server.quit() print 'sent'
cheers for any help
shaggy