Import error when sending simple email with python

1

I am learning to send email in python and am encountering several problems. One of them is already in importing the smtplib module. My code is as follows:

from smtplib import SMTP
smtp=SMTP('smtp.live.com',587)
smtp.starttls()
smtp.login('[email protected]','senha')
msg='ola mundo'
smtp.sendmail('[email protected]',['[email protected]'],msg)
smtp.quit()

This script generates the following error:

Traceback (most recent call last):
  File "C:\Users\Benedito\Desktop\email.py", line 1, in <module>
    from smtplib import SMTP
  File "C:\Users\Benedito\AppData\Local\Programs\Python\Python35- 32\lib\smtplib.py", line 47, in <module>
    import email.utils
  File "C:\Users\Benedito\Desktop\email.py", line 1, in <module>
    from smtplib import SMTP
ImportError: cannot import name 'SMTP'

From what I understand, the error treats the smtplib module as if it did not have the SMTP method on it. However, when I perform direct import on the Python command line, this import error does not occur. Why is giving error when I run a script and is not giving error when I execute writing line by line in the python command line?

I'm on Windows 8 and use Python 3.5.2

    
asked by anonymous 29.12.2016 / 15:42

1 answer

2

Change the name of your file from email.py to enviaremail.py (or whatever), it seems to be conflicting with import email.utils (used by SMTP lib)

Then try:

python enviaremail.py

It is always good to avoid these names so they do not conflict with native classes and namespaces

    
29.12.2016 / 15:54