Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendmail Errno[61] Connection Refused

I've been trying to get my application to mail some outputted text to an email. For simplification I have isolated the script :

import smtplib import sys import os  SERVER = "localhost"  FROM = os.getlogin() TO = [raw_input("To : ")]  SUBJECT = "Message From " + os.getlogin()  print "Message : (End with ^D)" TEXT = '' while 1:     line = sys.stdin.readline()     if not line:         break     TEXT = TEXT + line  # 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() 

This script outputs :

    Traceback (most recent call last):   File "/Users/christianlaustsen/Dropbox/Apps - Python/mail/smtplib_mail.py", line 32, in <module>     server = smtplib.SMTP(SERVER)   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in __init__     (code, msg) = self.connect(host, port)   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect     self.sock = self._get_socket(host, port, self.timeout)   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket     return socket.create_connection((port, host), timeout)   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 512, in create_connection     raise error, msg error: [Errno 61] Connection refused 

So as you can see, the connection is being refused. I'm running Python 2.6 on Mac OS X Snow Leopard (if that's relevant).

I have tried searching around a lot, but haven't been able to find a solution. Any help will be appreciated.

like image 782
Tehnix Avatar asked Apr 11 '11 10:04

Tehnix


2 Answers

If you start a local server as follows:

python -m smtpd -n -c DebuggingServer localhost:1025

Make sure to modify the mail-sending code to use the non-standard port number:

server = smtplib.SMTP(SERVER, 1025) server.sendmail(FROM, TO, message) server.quit() 
like image 110
Gabriel Ferrer Avatar answered Sep 21 '22 08:09

Gabriel Ferrer


My guess is that you do not have any SMTP server installed on your local machine.

If your emails are not sensitive, open a Gmail account and send your emails using it with Python.

like image 32
Adam Matan Avatar answered Sep 19 '22 08:09

Adam Matan