ItGo.me Focus on IT Recommend

Home > Error Trying To Send Email Python

Error Trying To Send Email Python

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

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
python email gmail smtplib
|
  this question
edited Mar 16 '16 at 12:14 asked Mar 16 '16 at 1:25 David Richardson 9 4

 | 

1 Answers
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.

Recommend:email - Sending gmail to gmail message using smtplib python

f send_email(price): try: s = smtplib.SMTP(SMTP_URL) s.ehlo() s.starttls() s.ehlo() s.login(email_user, email_pass) except smtplib.SMTPAuthenticationError: print("Failed to login")


|
  this answer
answered Mar 16 '16 at 1:45 Yunhe 552 2 7      That fixed one problem, still get Traceback (most recent call last): File "C:\Users\hack\Desktop\yolo.py", line 2, in <module> import smtplib File "C:\Python34\lib\smtplib.py", line 47, in <module> import email.utils ImportError: bad magic number in 'email': b'\x03\xf3\r\n' –  David Richardson Mar 16 '16 at 3:07      @DavidRichardson After googling I guess remove files with extension .pyc may help you. More details can be found link1 and link2 –  Yunhe Mar 16 '16 at 3: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 1:18      @DavidRichardson Thanks for your reply and update. :) –  Yunhe Mar 18 '16 at 1:20

 | 

Recommend:smtplib - Python - sending an email through gmail servers

tplib.SMTP('smtp.gmail.com', 587)>>> smtpObj.ehlo()>>> smtpObj.starttls()>>> smtpObj.login("email", "password")>>> smtpObj.sendmail("email", "email2", "Subject: Test \nsent with python!")>>> smtpObj.quit() but when I try to make a pro

oriUlr:http://stackoverflow.com/questions/36025239/error-trying-to-send-email-python

------splitte line----------------------------