Import error when sending simple email with python

Asked

Viewed 247 times

1

I am learning to send email in python and I am facing several problems. One of them is already in the import of the smtplib module. My code is this:

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 in it. However, when I run the direct import on the Python command line, this import error does not occur. Because it is giving error when running a script and is not giving error when running writing line by line in the python command line?

I am on Windows 8 and use Python 3.5.2

1 answer

2


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

And then try:

python enviaremail.py

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.