kramer65
kramer65

Reputation: 53923

How to send simple email using Python

Before you mark this as a duplicate; I've read the other threads as well. I'm trying to send an email as simple as using the mail() function in php. I simply tried the example from the official python docs. But when I try to connect with the localhost on my linux machine I get:

>>> import smtplib
>>> smtplib.SMTP('localhost')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 311, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused

I thought this example didn't need an email server to run (just like the php mail() function doesn't need an email-server to run).

Any ideas what I might be doing wrong or how I can get this to send an email? All tips are welcome!

Upvotes: 3

Views: 507

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174662

The php mail() function does need a relay to send messages. It defaults to sendmail on Linux machines.

On Windows you have to give it the address of a SMTP server.

In order for any program to send email, you need to connect to a SMTP server. There is no running around that.

Upvotes: 1

Related Questions