6

Running into a very stange error. I'm running Django on my Mac OSX and when I tried to send an email from my application it hangs and gives me this error: "Error 61 Connection Refused"

Any ideas? I don't have my firewall turned on. I can upload an image of the error if needed.

mymmaster
  • 826
  • 1
  • 9
  • 17
  • Cool. So I added configs in my settings.py but now I get this: "Error 60, Operation Timed Out". Exception Location: ...python2.6/socket.py in create_connection – mymmaster Apr 23 '10 at 00:15

4 Answers4

4

Have you actually configured the EMAIL_* settings in settings.py? Error 61 is the error you get if you leave it on the default values and you don't have a local SMTP server running.

Alternatively, as Peter suggests, if you have set it up then you might need to use authentication with your SMTP server.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

Its simple, if sendmail works via command-line, copy the code from http://djangosnippets.org/snippets/1864/ into a file called sendmail.py

"""sendmail email backend class."""

import threading

from django.conf import settings
from django.core.mail.backends.base import BaseEmailBackend
from subprocess import Popen,PIPE

class EmailBackend(BaseEmailBackend):
    def __init__(self, fail_silently=False, **kwargs):
        super(EmailBackend, self).__init__(fail_silently=fail_silently)
        self._lock = threading.RLock()

    def open(self):
        return True

    def close(self):
        pass

    def send_messages(self, email_messages):
        """
        Sends one or more EmailMessage objects and returns the number of email
        messages sent.
        """
        if not email_messages:
            return
        self._lock.acquire()
        try:
            num_sent = 0
            for message in email_messages:
                sent = self._send(message)
                if sent:
                    num_sent += 1
        finally:
            self._lock.release()
        return num_sent

    def _send(self, email_message):
        """A helper method that does the actual sending."""
        if not email_message.recipients():
            return False
        try:
            ps = Popen(["sendmail"]+list(email_message.recipients()), \
                       stdin=PIPE)
            ps.stdin.write(email_message.message().as_string())
            ps.stdin.flush()
            ps.stdin.close()
            return not ps.wait()
        except:
            if not self.fail_silently:
                raise
            return False
        return True

Inside of settings.py, set the variable:

EMAIL_BACKEND = 'path.to.sendmail.EmailBackend'
1

Being totally Max OS X ignorant, my first guess would be that your SMTP server requires authentication.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
0

I had a similar problem and found this link: http://dashasalo.com/2011/05/29/django-send_mail-connection-refused-on-macos-x/

Basically, you need to have a mail server running in order to send mail from it. If there is no mail server running you will get a 61.

araneae
  • 1,525
  • 2
  • 13
  • 13