Search code examples
pythonsmtppython-3.7smtplib

Attribute error in sending email through smtp in python 3.7


I have hidden id and password in this question. But, gmail password was generated after turning on 2 step verification and generating an app password.

ERROR-- AttributeError: module 'smtplib' has no attribute 'SMTP'

import smtplib

email = "*****@gmail.com"
password = "*****"
with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
    connection.starttls()
    connection.login(user=email, password=password)
    connection.sendmail(from_addr=email,
                        to_addrs="*****@yahoo.com",
                        msg="Subject:Hello\n\nThis is the body of the email"
                        )

Solution

  • This error generally occurs when Python discovers a circular dependency with the smtplib module.

    If your python script is named smtplib.py or email.py, Python would try to import your file when it executes the import statement for the smtplib library.

    As a result, it creates a partially initialized module, and if your script does not have an attribute named smtp, you will run into this error.

    Solution :

    Rename your email sending python script to a name apart from smtplib.py or email.py