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?