1

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="example2@gmail.com"
SUBJECT = 'Sending an email yo'
TEXT = 'YOLO'

gmail_sender = "example@gmail.com"
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

1 Answers1

1

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

Yunhe
  • 665
  • 5
  • 10
  • 1
    That fixed one problem, still get Traceback (most recent call last): File "C:\Users\hack\Desktop\yolo.py", line 2, in import smtplib File "C:\Python34\lib\smtplib.py", line 47, in import email.utils ImportError: bad magic number in 'email': b'\x03\xf3\r\n' – David Richardson Mar 16 '16 at 03:07
  • @DavidRichardson After googling I guess remove files with extension .pyc may help you. More details can be found [link1](https://teamtreehouse.com/community/importerror-bad-magic-number-in-time-bx03xf3rn) and [link2](http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error) – Yunhe Mar 16 '16 at 03:59
  • Thanks for your help, I updated the post with more errors that come now. – David Richardson Mar 16 '16 at 12:15
  • I fixed it, it was because I had other versions of python installed. – David Richardson Mar 18 '16 at 01:18
  • @DavidRichardson Thanks for your reply and update. :) – Yunhe Mar 18 '16 at 01:20
  • @DavidRichardson You should accept the answer. This answer helps me as well. – ABCD May 14 '17 at 14:57
  • @SmallChess I can't figure out how to do that, still a newb user. – David Richardson May 18 '17 at 14:32