import smtplib from . import interface 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') def hook_check_ml_restrictions(context): pass def hook_validate_poster(context): pass def hook_process_subscribe_queries(context): pass 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 def hook_generate_rcpt_list(context): listname = context.get('listname', None) if not listname: raise interface.HookStopIteration('list name not found') recipients_filename = ''.join([listname, '-recipients.txt']) try: with open(recipients_filename, encoding='utf8') as fi: recipients = [recipient for recipient in fi] context['recipients'] = recipients except FileNotFoundError as e: n = interface.HookAbortError('recipients list not found') n.args += ({'listname': listname, 'recipients_filename': recipients_filename}, ) raise n from e def hook_send_all(context): listaddress = context.get('listaddress', None) if not listaddress: raise interface.HookStopIteration('list address not found') 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'])