1

I try to send an email with Python 3.4.3 with the following code:

import smtplib
content = "Just does not work"
mail = smtplib.STMP('stmp.gmail.com', '587')
mail.ehlo()
mail.starttls()
mail.login('MyEmailaddress', 'password')
mail.sendmail('MyEmailaddress', 'ToEmailaddress', content)
mail.close()

Instead of sending an email, Windows asks me with which program I want to open the program and then simply displays the above code in Word or Explorer whatever I choose.

Thanks for your tips so far but nothing seems to work still

Your answers so far all helped. I got the following that is working

import sys
sys.path.append("/Python34/Lib")
sys.path.append("/Python34")
import smtplib
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
address = input("Enter gmail address: ")
passw = input("Enter password: ")
smtpObj.login(address, passw)
subject = "Subject: Test email\nThis is finally working."
smtpObj.sendmail('FromEmailAddress', 'ToEmailAdress', subject)
smtpObj.quit()

Eric - indeed I needed to type in Python *.py to make this work. I learned to set path in DOS through: set path c:\Python34 That worked to get python going in a different subdirectory where my scripts are

Also learned to use 'sys.path.append' to add directories to your path within the code to ensure all imports can occur

The code above works in C:\Python34

It does not work however from another directory I keep on getting the error message: ImportError: 'Cannot import name 'ascii_letters'

2 questions

  • can anyone help me with the error code above? (I use python 3.4.3)
  • Why do you need to type in Python in CMD.exe before certain script names to run accurately while with others that is not necessary?
newb
  • 11
  • 4
  • Basic sanity check: have you tried typing `python script.py` in a cmd.exe windows? – Eric Levieil Jun 27 '15 at 14:07
  • See also http://stackoverflow.com/questions/778202/smtplib-and-gmail-python-script-problems – Eric Levieil Jun 27 '15 at 14:09
  • Sounds like your Python is not in the path. I bet you have to fix environment variables or try winpython portable. http://winpython.sourceforge.net/ – Alex Ivanov Jun 27 '15 at 14:17
  • Forgive me my ignorance Alex but what do I need to do to get it in the path? – newb Jun 27 '15 at 14:25
  • Well, when you install Python it is supposed to do it automatically. You can try this one: http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7 Оr you can use winpython portable instead. Python Path there is set via its Control Panel. – Alex Ivanov Jun 27 '15 at 15:20
  • Make sure you can run a python file with simple `print("hello world")` in it first. – jfs Jun 27 '15 at 17:01
  • You might need to set your PYTHONPATH environment variable to the directory where all the modules are located, probably C:\python34\lib, or similar. – chown Jun 28 '15 at 14:25
  • For all the other beginners out there - I now know why the I got the import error on ascii_letters. It did run in main directory but not in my scripts and the reason is embarrassingly enough: I wrote an script called string which basically messes up import string from that directory – newb Jun 29 '15 at 15:39

0 Answers0