0

I wanted to send an email with Django(1.5), in my console it looked like the following:

In [30]: send_mail("bla", "here it is", "from_address@gmail.com", 
["to_address@gmail.com"], fail_silently=False)

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: bla
From: from_address@gmail.com
To: to_address@gmail.com
Date: Sat, 14 Dec 2013 11:56:37 -0000
Message-ID: <20131214115637.4720.60719@my_username-E531>

here it is
-------------------------------------------------------------------------------
Out[30]: 1

Eventually I found the problem was caused because EMAIL_BACKEND (containing django.core.mail.backends.smtp.EmailBackend) in settings.common.py was being overwritten by EMAIL_BACKEND (containing django.core.mail.backends.console.EmailBackend) in settings.dev.py with a different value.

The only explicit clue I had that something went wrong was Out[30]: 1, as I understand an exit status of an unsuccessful run of a process.

  1. How come I didn't get any Python error?
  2. Was there a way to get the Python error for this error? If this was not possible how should I have debugged this properly?
Bentley4
  • 10,678
  • 25
  • 83
  • 134

1 Answers1

1

The return value you see is the number of messages processed. From the django.core.mail.backends.base.BaseEmailBackend.send_messages() method:

def send_messages(self, email_messages):
    """
    Sends one or more EmailMessage objects and returns the number of email
    messages sent.
    """

The backend you are using, even though it is the wrong one, is indicating it sent 1 message.

If this is the django.core.mail.console.EmailBackend() backend, that just means 1 message was written to sys.stdout, but otherwise not forwarded to a SMTP server.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks so much for the explanation! I found it really strange that an exit status would have been used to indicate this behavior. I guess that could've been possible if the person who programmed the involved part would just use `sys.stderr.write` instead of raising a Python error. I'm glad I was wrong! : ) – Bentley4 Dec 14 '13 at 19:09
  • No, I'm wrong. `sys.stderr.write` would look different, it would not be outputted behind the `Out` Ipython prompt. You'd have to use print for that. – Bentley4 Dec 14 '13 at 23:41
  • @Bentley4: `console.EmalBackend()` writes to `sys.stdout`, just as `print` would. – Martijn Pieters Dec 14 '13 at 23:42
  • 1
    @Bentley4: But writing to `sys.stderr` would look just the same here. The write takes place *before* the call returns. – Martijn Pieters Dec 14 '13 at 23:42
  • My false assumptions again. I only checked `sys.stdout.write`, `sys.stderr.write` and not `print`... . Forgot that only return statements (afaik) have that `Out` prompt in `Ipython`. I should have known and checked before writing that down. Sorry! – Bentley4 Dec 14 '13 at 23:50