Login

Better debugging mail server

Author:
yourcelf
Posted:
February 21, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

Python includes (and Django recommends) a simple email debugging server which prints mail to stdout. The trouble is, unlike any half-competent mail reader, long lines are broken up, and thus long URLs don't work without modification.

This snippet simply unwraps long lines (broken by "=") so long URLs can be easily copied/pasted from the terminal.

Save this snippet into a file named "better.py" and execute it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
"""
Like the built-in debugging server, but unwarps long lines for easier
copy/paste of URLs.
"""
from __future__ import print_function
from smtpd import SMTPServer
import os

class DebuggingServer(SMTPServer):
    def process_message(self, peer, mailfrom, rcpttos, data):
        inheaders = 1
        lines = data.split('\n')
        print('---------- MESSAGE FOLLOWS ----------')
        for line in lines:
            # headers first
            if inheaders and not line:
                print('X-Peer:', peer[0])
                inheaders = 0
            if line.endswith('='):
                print(line[:-1], end='')
            else:
                print(line)
        print('------------ END MESSAGE ------------')

if __name__ == "__main__":
    os.system("python -m smtpd -n -c better.DebuggingServer localhost:1025")

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.