From 26a11acc738b9177403e64abff23cf609be5b16f Mon Sep 17 00:00:00 2001 From: VG Date: Sun, 12 Jun 2016 16:01:35 +0200 Subject: replace manual management by email python class --- climl/__init__.py | 1 + climl/hooks.py | 39 ++++++++++++++------------------------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/climl/__init__.py b/climl/__init__.py index c6f6db4..056d33a 100644 --- a/climl/__init__.py +++ b/climl/__init__.py @@ -48,6 +48,7 @@ def main(): conf = conf_postprocess(confparser.read_conf(confpath)) hooksfuncs = ( + hooks.hook_extract_email_from_content, hooks.hook_extract_listname, hooks.hook_check_ml_restrictions, # open/private ml hooks.hook_validate_poster, diff --git a/climl/hooks.py b/climl/hooks.py index 027329b..660d1db 100644 --- a/climl/hooks.py +++ b/climl/hooks.py @@ -1,17 +1,17 @@ import smtplib from . import interface +import email + + +def hook_extract_email_from_content(context): + context['email'] = email.message_from_bytes(context['content']) def hook_extract_listname(context): - lines = context['content'].split(b'\n') - for line in lines: - if line.startswith(b'To: '): - listname = line.split()[1].decode('ascii') - context['listname'] = listname.split('@')[0] - context['listaddress'] = listname - print('found listname: {}'.format(listname)) - return - raise interface.HookAbortError('listname not found') + email = context['email'] + context['listaddress'] = email['To'] + context['listname'] = context['listaddress'].split('@')[0] + print('found listname: {}'.format(context['listname'])) def hook_check_ml_restrictions(context): @@ -27,17 +27,8 @@ def hook_process_subscribe_queries(context): def hook_modify_reply_to(context): - content = context['content'] - listaddress = context['listaddress'].encode('ascii') - try: - index = content.index(b'\r\n\r\n') - context['content'] = b''.join([ - content[0:index], - b'\r\nReply-To: ', listaddress, # same header line - content[index:]]) - except ValueError as e: - raise interface.HookAbortError('bad email, missing header separator') \ - from e + email = context['email'] + email['Reply-To'] = context['listaddress'] def hook_generate_rcpt_list(context): @@ -63,12 +54,10 @@ def hook_send_all(context): recipients = context.get('recipients', None) if not recipients: raise interface.HookStopIteration() - # TODO: how to send emails here ? print('server_smtp:', context['conf']['smtp.server']) with smtplib.SMTP('devys.org') as smtp_server: #smtp_server.set_debuglevel(1) - smtp_server.sendmail(listaddress, - context['recipients'], - context['content'], - mail_options=['SMTPUTF8']) + smtp_server.send_message(context['email'], + from_addr=listaddress, + to_addrs=context['recipients']) -- cgit v1.2.3