1414import sys
1515import time
1616
17+ # Testing mode:
18+ # import ietf.utils.mail
19+ # ietf.utils.mail.test_mode = True
20+ # ... send some mail ...
21+ # ... inspect ietf.utils.mail.outbox ...
22+ # ... call ietf.utils.mail.empty_outbox() ...
23+ test_mode = False
24+ outbox = []
25+
26+ def empty_outbox ():
27+ global outbox
28+ outbox = []
29+
1730def add_headers (msg ):
1831 if not (msg .has_key ('Message-ID' )):
1932 msg ['Message-ID' ] = make_msgid ('idtracker' )
@@ -29,13 +42,19 @@ def send_smtp(msg, bcc=None):
2942 The destination list will be taken from the To:/Cc: headers in the
3043 Message. The From address will be used if present or will default
3144 to the django setting DEFAULT_FROM_EMAIL
45+
46+ If someone has set test_mode=True, then just append the msg to
47+ the outbox.
3248 '''
3349 add_headers (msg )
3450 (fname , frm ) = parseaddr (msg .get ('From' ))
3551 addrlist = msg .get_all ('To' ) + msg .get_all ('Cc' , [])
3652 if bcc :
3753 addrlist += [bcc ]
3854 to = [addr for name , addr in getaddresses (addrlist )]
55+ if test_mode :
56+ outbox .append ((msg , to , msg .as_string ()))
57+ return
3958 server = None
4059 try :
4160 server = smtplib .SMTP ()
@@ -73,9 +92,6 @@ def copy_email(msg, to, toUser=False):
7392 Send a copy of the given email message to the given recipient.
7493 '''
7594 add_headers (msg )
76- # Overwrite the From: header, so that the copy from a development or
77- # test server doesn't look like spam.
78- msg ['From' ] = settings .DEFAULT_FROM_EMAIL
7995 new = MIMEMultipart ()
8096 # get info for first part.
8197 # Mode: if it's production, then "copy of a message", otherwise
@@ -91,7 +107,9 @@ def copy_email(msg, to, toUser=False):
91107 explanation = "The attached message would have been sent, but the tracker is in %s mode.\n It was not sent to anybody." % settings .SERVER_MODE
92108 new .attach (MIMEText (explanation + "\n \n " ))
93109 new .attach (MIMEMessage (msg ))
94- new ['From' ] = msg ['From' ]
110+ # Overwrite the From: header, so that the copy from a development or
111+ # test server doesn't look like spam.
112+ new ['From' ] = settings .DEFAULT_FROM_EMAIL
95113 new ['Subject' ] = '[Django %s] %s' % (settings .SERVER_MODE , msg .get ('Subject' , '[no subject]' ))
96114 new ['To' ] = to
97115 send_smtp (new )
@@ -148,7 +166,7 @@ def send_mail_mime(request, to, frm, subject, msg, cc=None, extra=None, toUser=N
148166 if extra :
149167 for k , v in extra .iteritems ():
150168 msg [k ] = v
151- if settings .SERVER_MODE == 'production' :
169+ if test_mode or settings .SERVER_MODE == 'production' :
152170 send_smtp (msg , bcc )
153171 elif settings .SERVER_MODE == 'test' :
154172 if toUser :
0 commit comments