Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

NOTE: All the periods are replacements for password/email/file paths/etc.

like image 960
Jacob Kudria Avatar asked May 12 '13 21:05

Jacob Kudria


People also ask

How does SMTP work in Python?

Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers. Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Is Smtplib included in Python?

Python comes with the built-in smtplib module for sending emails using the Simple Mail Transfer Protocol (SMTP). smtplib uses the RFC 821 protocol for SMTP.


1 Answers

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.

like image 50
Blender Avatar answered Sep 28 '22 01:09

Blender