3

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!

1
  • 2
    Do you have mail server on your localhost ? If not then you can't connect to it. Commented Jul 20, 2014 at 19:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I never knew the php mail() function actually needed an external program (because it always "just worked"). But I just sudo apt-get installed sendmail, and now it works like a charm! Thanks for enlightening me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.