[snowy: 12/26] Add a simple mail debugging server



commit abfe11623c554ca7fe59339347ea787e0f82b9e3
Author: Sander Dijkhuis <sander dijkhuis gmail com>
Date:   Thu Mar 11 20:19:40 2010 +0100

    Add a simple mail debugging server
    
    Django's send_mail() assumes that the SMTP server as set in snowy.settings
    is available, and gives an error otherwise. This patch adds a debugmail
    command to manage.py, which starts a small smptd server that matches these
    settings. Instead of sending emails, they are printed to stdout.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=612568

 INSTALL                               |   12 ++++-
 core/management/commands/debugmail.py |   79 +++++++++++++++++++++++++++++++++
 local_settings.py.in                  |    2 +
 3 files changed, 90 insertions(+), 3 deletions(-)
---
diff --git a/INSTALL b/INSTALL
index 5627269..46c6567 100644
--- a/INSTALL
+++ b/INSTALL
@@ -26,10 +26,16 @@ Running Snowy From Your Git Checkout
 
 	NOTE: Don't name your admin user 'admin'
 
-4. Start local snowy (in your snowy git checkout):
+4. Start mail debugging server (in your snowy git checkout):
+	python manage.py debugmail
+
+	NOTE: This is only needed if Snowy needs to send mails (like in the
+	case of user registration) and you don't have an SMTP server set up.
+
+5. Start local snowy (in your snowy git checkout):
 	python manage.py runserver
 
-5. Admin your snowy:
+6. Admin your snowy:
 	http://localhost:8000/admin
 
 	- In the Sites table, make sure to set the domain and name properly, e.g.:
@@ -43,7 +49,7 @@ Running Snowy From Your Git Checkout
 		Secret: anyone
 		Status: Accepted
 
-6. Play with snowy:
+7. Play with snowy:
 	http://localhost:8000/myusername/notes (for example)
 
 
diff --git a/core/management/__init__.py b/core/management/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/core/management/commands/__init__.py b/core/management/commands/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/core/management/commands/debugmail.py b/core/management/commands/debugmail.py
new file mode 100644
index 0000000..97da6c7
--- /dev/null
+++ b/core/management/commands/debugmail.py
@@ -0,0 +1,79 @@
+#
+# Copyright (c) 2010 Sander Dijkhuis <sander dijkhuis gmail com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+from django.core.management.base import NoArgsCommand, CommandError
+
+import smtpd
+
+class MailDebuggingServer(smtpd.DebuggingServer):
+    skip_headers = ('Content-Type', 'MIME-Version', 'Content-Transfer-Encoding',
+        'Message-ID')
+
+    def process_message(self, peer, mailfrom, rcpttos, data):
+        print('\n----------- BEGIN MESSAGE -----------')
+
+        (headers, body) = data.split('\n\n', 1)
+        quoted_printable = False
+
+        for header in headers.split('\n'):
+            if header == 'Content-Transfer-Encoding: quoted-printable':
+                quoted_printable = True
+
+            if not header.split(':')[0] in self.skip_headers:
+                print(header)
+
+        print('')
+
+        if quoted_printable:
+            import quopri
+
+            print(quopri.decodestring(body))
+        else:
+            print(body)
+
+        print('------------ END MESSAGE ------------')
+
+class Command(NoArgsCommand):
+    help = "Run a mail debugging server, matching the project settings"
+
+    def handle_noargs(self, **options):
+        from django.conf import settings
+
+        import asyncore
+        import socket
+
+        host_port = (settings.EMAIL_HOST, settings.EMAIL_PORT)
+
+        try:
+            server = MailDebuggingServer(host_port, None)
+        except socket.error:
+            raise CommandError('Could not set up the mail server at %s:%d.\n'
+                % host_port
+                + 'Make sure that you can actually host a server using the\n'
+                + 'current values for settings.EMAIL_HOST and settings.EMAIL_PORT.')
+
+        print('Mail debugging server is running at %s:%d' % host_port)
+        print('Emails from this Django site will appear here instead of being sent.')
+        print('Quit the server with CONTROL-C.')
+
+        try:
+            asyncore.loop()
+        except KeyboardInterrupt:
+            # Print a blank line after the ^C. This looks nicer.
+            print('')
+
+            pass
diff --git a/local_settings.py.in b/local_settings.py.in
index b7cb5ad..d6e7c7c 100644
--- a/local_settings.py.in
+++ b/local_settings.py.in
@@ -11,3 +11,5 @@ DATABASE_NAME = 'snowy.db'  # Or path to database file if using sqlite3.
 RECAPTCHA_ENABLED = False
 RECAPTCHA_PUBLIC_KEY = ''
 RECAPTCHA_PRIVATE_KEY = ''
+
+EMAIL_PORT = 1025



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]