Focus on IT Recommend

Home > Error Trying To Send Email Python

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
SUBJECT = 'Sending an email yo'

gmail_sender = ""
gmail_passwd = 'pass'

server = smtplib.SMTP('', 587)
server.login(gmail_sender, gmail_passwd)

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

    server.sendmail(gmail_sender, [TO], BODY)

After making changes suggested, this is y new error:

Traceback (most recent call last):
File "C:\Users\hack\Desktop\", line 10, in <module>
server = smtplib.SMTP('', 587)
File "C:\Python34\lib\", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python34\lib\", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\lib\", line 292, in _get_socket
File "C:\Python34\lib\", line 512, in create_connection
raise err
File "C:\Python34\lib\", line 503, in create_connection
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

It's caused by the name of your script -- ''. 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\", line 2, in <module> import smtplib File "C:\Python34\lib\", 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('', 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


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