H4KKR
H4KKR

Reputation: 64

Python 3 smtplib has no attribute SMTP when sending an email

I'm trying to send a really simple email in Python 3 using CodeRunner to script and MacBook Terminal to run And every time I run it, tons of errors come up. I'm sending the email using yahoo mail.

Here's my code:

import smtplib
SERVER = "localhost"

FROM = ['[email protected]', 'Password']

TO = ["[email protected]"] 

SUBJECT = ["Hello!"]

TEXT = ["This message was sent with Python's smtplib."]

# actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP('smtp.mail.yahoo.com', 465)
server.sendmail(FROM, TO, message)
server.quit()

It throws up these errors that I can't seem to fix. It says that smtplib has no attribute SMTP.

Any help would be much appreciated.

Upvotes: 0

Views: 5393

Answers (2)

Jeyekomon
Jeyekomon

Reputation: 3391

I had the same error and the karothiya's comment solved my issue.

Make sure that the name of your script isn't email.py or any other string that is identical with a package the code is trying to find.

Upvotes: 6

holdenweb
holdenweb

Reputation: 37003

When reporting errors it's always better to copy and paste the full traceback unless you understand what the error message is telling you. Consider the following interactive session:

Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> smtplib.SMTP
<class 'smtplib.SMTP'>

As you can see, the library clearly DOES have an SMTP attribute, so the real question is "what have you done to smtplib"? Without an error traceback we are struggling with partial information.

Bottom line: if you need people's help fixing errors it's easiest to tell them exactly what the errors are. As a newcomer to SO I understand it's difficult to get things like code formatting right, but people are generally willing to help (and, indeed, someone else has already tried to reformat your code for you).

Upvotes: 0

Related Questions