Skip to content

Commit df7299c

Browse files
author
Matthew Borger
committed
Added option to save generated emails
to a given folder instead of sending via SMTP.
1 parent 62b4a59 commit df7299c

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

libpius/mailer.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import smtplib
55
import socket
6+
import os
67

78
from email import message, quoprimime
89
from email.utils import formatdate
@@ -21,7 +22,7 @@
2122

2223
class 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()

pius

100755100644
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def main():
158158
' into the default keyring. Ignored if -r is not'
159159
' specified, or if it\'s the same as the default'
160160
' keyring.')
161+
parser.add_option('-L', '--local-mail-dir', dest='local_mail_dir',
162+
metavar='FILE',
163+
help='Instead of calling SMTP, save'
164+
' the email to this directory.')
161165
parser.add_option('-m', '--mail', dest='mail', metavar='EMAIL', nargs=1,
162166
type='email',
163167
help='Email the encrypted, signed keys to the'
@@ -238,7 +242,8 @@ def main():
238242
options.mail_no_pgp_mime,
239243
options.mail_override,
240244
options.mail_text,
241-
options.tmp_dir
245+
options.tmp_dir,
246+
options.local_mail_dir
242247
)
243248
else:
244249
mailer = None

0 commit comments

Comments
 (0)