33import os
44import smtplib
55import socket
6+ import os
67
78from email import message , quoprimime
89from email .utils import formatdate
2122
2223class PiusMailer (object ):
2324 def __init__ (self , mail , host , port , user , tls , no_mime , override , msg_text ,
24- tmp_dir ):
25+ tmp_dir , local_mail_dir ):
2526 self .mail = mail
2627 self .host = host
2728 self .port = port
@@ -32,6 +33,7 @@ def __init__(self, mail, host, port, user, tls, no_mime, override, msg_text,
3233 self .address_override = override
3334 self .message_text = msg_text
3435 self .tmp_dir = tmp_dir
36+ self .local_mail_dir = local_mail_dir
3537
3638 @staticmethod
3739 def add_options (parser ):
@@ -258,32 +260,39 @@ def _send_mail(self, to, msg):
258260 msg ['To' ] = to
259261 msg ['Date' ] = formatdate (localtime = True )
260262
261- try :
262- smtp = smtplib .SMTP (self .host , self .port )
263- if self .tls :
264- # NOTE WELL: SECURITY IMPORTANT NOTE!
265- # In python 2.6 if you attempt to starttls() and the server doesn't
266- # understand an exception is raised. However before that, it just
267- # carried on # and one could attempt to auth over a plain-text session.
268- # This is BAD!
269- #
270- # So, in order be secure on older pythons we ehlo() and then check the
271- # response before attempting startls.
272- smtp .ehlo ()
273- if not smtp .has_extn ('STARTTLS' ):
274- # Emulate 2.6 behavior
275- raise smtplib .SMTPException ('Server does not support STARTTLS' )
276- smtp .starttls ()
277- # must re-ehlo after STARTTLS
278- smtp .ehlo ()
279- # Don't want to send auth information unless we're TLS'd
280- if self .user :
281- smtp .login (self .user , self .password )
282- if self .address_override :
283- env_to = self .address_override
284- else :
285- # BCC the user...
286- env_to = [msg ['To' ], self .mail ]
263+ if self .local_mail_dir :
264+ if not os .path .isdir (self .local_mail_dir ):
265+ os .mkdir (self .local_mail_dir )
266+ email = open (os .path .join (self .local_mail_dir , msg ['To' ]), 'w' )
267+ email .write (str (msg ))
268+ email .close ()
269+ else :
270+ try :
271+ smtp = smtplib .SMTP (self .host , self .port )
272+ if self .tls :
273+ # NOTE WELL: SECURITY IMPORTANT NOTE!
274+ # In python 2.6 if you attempt to starttls() and the server doesn't
275+ # understand an exception is raised. However before that, it just
276+ # carried on # and one could attempt to auth over a plain-text session.
277+ # This is BAD!
278+ #
279+ # So, in order be secure on older pythons we ehlo() and then check the
280+ # response before attempting startls.
281+ smtp .ehlo ()
282+ if not smtp .has_extn ('STARTTLS' ):
283+ # Emulate 2.6 behavior
284+ raise smtplib .SMTPException ('Server does not support STARTTLS' )
285+ smtp .starttls ()
286+ # must re-ehlo after STARTTLS
287+ smtp .ehlo ()
288+ # Don't want to send auth information unless we're TLS'd
289+ if self .user :
290+ smtp .login (self .user , self .password )
291+ if self .address_override :
292+ env_to = self .address_override
293+ else :
294+ # BCC the user...
295+ env_to = [msg ['To' ], self .mail ]
287296
288297 smtp .sendmail (self .mail , env_to , msg .as_string ())
289298 smtp .quit ()
0 commit comments