David Richardson
David Richardson

Reputation: 39

Error Trying To Send Email Python

Okay, I am trying to make a program that emails someone something in python, there are other things like this on the forum but i have made those changes and they seem to not be working, I think smtplib has changed, for reference I am using python 2.7

Here is the code

#sending email with python
import smtplib
TO="[email protected]"
SUBJECT = 'Sending an email yo'
TEXT = 'YOLO'

gmail_sender = "[email protected]"
gmail_passwd = 'pass'

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join([
    'To: %s' % TO,
    'From: %s' % gmail_sender,
    'Subject: %s' % SUBJECT,
    '',
    TEXT
    ])

try:
    server.sendmail(gmail_sender, [TO], BODY)
except:
    print('ERROR!!!!!')
server.quit()

After making changes suggested, this is y new error:

Traceback (most recent call last):
File "C:\Users\hack\Desktop\yolo.py", line 10, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "C:\Python34\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python34\lib\smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\lib\smtplib.py", line 292, in _get_socket
self.source_address)
File "C:\Python34\lib\socket.py", line 512, in create_connection
raise err
File "C:\Python34\lib\socket.py", line 503, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because                       
the target machine activley refused it

Upvotes: 1

Views: 515

Answers (1)

Yunhe
Yunhe

Reputation: 665

It's caused by the name of your script -- 'email.py'. Because Python has an email package already. Rename your script would solve it.

Upvotes: 1

Related Questions