Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an email using python script

Tags:

python

email

Today I needed to send email from a Python script. As always I searched Google and found the following script that fits to my need.

import smtplib

SERVER = "localhost"

FROM = "[email protected]"
TO = ["[email protected]"] # must be a list

SUBJECT = "Hello!"

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

# Prepare actual message

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

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

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

But when I tried to run the program, I got the following error message:

Traceback (most recent call last):
  File "C:/Python26/email.py", line 1, in <module>
    import smtplib
  File "C:\Python26\lib\smtplib.py", line 46, in <module>
    import email.utils
  File "C:/Python26/email.py", line 24, in <module>
    server = smtplib.SMTP(SERVER)
AttributeError: 'module' object has no attribute 'SMTP'

How can i solve this problem? Any one can help me?

Thanks in advance, Nimmy.


changed the name to emailsendin .py. But I got the following error

Traceback (most recent call last):
  File "C:\Python26\emailsending.py", line 24, in <module>
    server = smtplib.SMTP(SERVER)
  File "C:\Python26\lib\smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python26\lib\smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python26\lib\smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:\Python26\lib\socket.py", line 512, in create_connection
    raise error, msg
error: [Errno 10061] No connection could be made because the target machine actively refused it
like image 592
Nimmy Avatar asked Mar 20 '10 06:03

Nimmy


People also ask

Can we send email from Python script?

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. The examples in this tutorial will use the Gmail SMTP server to send emails, but the same principles apply to other email services.

How do I send an email using Python 3?

To send the mail you use smtpObj to connect to the SMTP server on the local machine. Then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these are not always used to route the mail).


3 Answers

You've named your module the same as one of Python's internal modules. When you import smtplib, it tries to import email, and finds your module instead of the internal one. When two modules import one another, only the variables in each module visible before the both import statements will be visible to one another. Renaming your module will fix the problem.

You can see this, although it's slightly obscure, in the stack trace. The import email.utils line from smtplib.py is calling your module, in "c:/Python26/email.py".

Another note: it's probably not a great idea to use your Python install directory as your working directory for Python code.

like image 50
Chris B. Avatar answered Oct 28 '22 04:10

Chris B.


Did you name your script email.py?

If so, rename to something else and the naming conflict you're encountering should be solved.

like image 42
Chris Ballance Avatar answered Oct 28 '22 02:10

Chris Ballance


You have managed to shadow the stdlib email module by calling your script email.py. Rename your script to something not in the stdlib and try again.

like image 41
Ignacio Vazquez-Abrams Avatar answered Oct 28 '22 04:10

Ignacio Vazquez-Abrams