when naming a file name, we should avoid coinciding with the system package name
Reprinted from: https://stackoverflow.com/questions/16512256/no-attribute-smtp-error-when-trying-to-send-email-in-python
I am trying to send an email in Python:
import smtplib
fromaddr = '......................'
toaddrs = '......................'
msg = 'Spam email Test'
username = '.......'
password = '.......'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
I understand that this is probably not the correct message format.
Anyways, I get an error:
C:\.....>python email.py
Traceback (most recent call last):
File "email.py", line 1, in <module>
import smtplib
File "C:\.....\Python\lib\smtplib.py", line 47,
in <module>
import email.utils
File "C:\.....\email.py", line 15, in
<module>
server = smtplib.SMTP('smtp.gmail.com', 587)
AttributeError: 'module' object has no attribute 'SMTP'
I don’t quite understand what I am doing wrong here… Anything incorrect?
Solution:
Method1:
Python already has an email
module. Your script’s name is email.py
, which is preventing smtplib
from importing the built-in email
module.
Rename your script to something other than email.py
and the problem will go away.
Method 2:
import smtplib
conn = smtplib.SMTP('imap.gmail.com',587)
conn.ehlo()
conn.starttls()
conn.login('[email protected]', 'your_password')
conn.sendmail('[email protected]','[email protected]','Subject: What you like? \n\n Reply Reply Reply')
conn.quit()
Similar Posts:
- [773]smtplib.SMTPServerDisconnected: Connection unexpectedly closed
- Python Send Email Error: smtplib.SMTPException: SMTP AUTH extension not supported by server
- [Solved] Python Error: CentOS failed to send mail without any error message
- Error: need RCPT command
- [Solved] smtplib Sent Email Error: SMTPDataError: (554, ‘DT:SPM 163 smtp1
- [mailbox usage] POP3, SMTP activation and setting method of Yahoo mailbox
- Error reported when Foxmail software adds QQ email
- javax.mail.MessagingException: Could not convert socket to TLS
- Phpmailer’s solution to extension missing: OpenSSL error
- Linux sendmail can’t send email [How to Solve]