Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Error SMTP python

Tags:

python

smtp

gmail

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

like image 247
Shaggy89 Avatar asked May 10 '15 03:05

Shaggy89


People also ask

How does SMTP work in Python?

Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers. Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

What is Ehlo in Python?

Extended HELO (EHLO) is an Extended Simple Mail Transfer Protocol (ESMTP) command sent by an email server to identify itself when connecting to another email server to start the process of sending an email. It is followed with the sending email server's domain name.

Is Smtplib part of Python?

smtplib is Python's built-in module for sending emails to any Internet machine with an SMTP or ESMTP listener daemon. I'll show you how to use SMTP_SSL() first, as it instantiates a connection that is secure from the outset and is slightly more concise than the . starttls() alternative.


2 Answers

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

like image 118
Shaggy89 Avatar answered Oct 14 '22 01:10

Shaggy89


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)
like image 22
DeepSpace Avatar answered Oct 14 '22 02:10

DeepSpace