def __init__(self, context_function): # gather all options from env context = context_function() escaped_context = utils.html_escape_context(context) extend_context(escaped_context) content_txt, content_html, attachments = construct_content(escaped_context) # TODO: cleanup duplicate code with BulkEmailContent # TODO: the context is only needed because of SMPT settings used in send_mail super().__init__( context=escaped_context, mailto=escaped_context["CONTACTEMAIL"], subject=escaped_context["SUBJECT"], from_address=utils.format_address( escaped_context.get("PARAMETER_FROM_DISPLAY_NAME", ""), escaped_context.get("PARAMETER_FROM_ADDRESS", utils.default_from_address()), ), reply_to=utils.format_address( escaped_context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", ""), escaped_context.get("PARAMETER_REPLY_TO_ADDRESS", ""), ), content_txt=content_txt, content_html=content_html, attachments=attachments, )
def __init__(self, context_function): self.attachments = [] self.content_txt = "" self.content_html = "" parameters, contexts = context_function() hosts = set([]) for context in contexts: context.update(parameters) utils.html_escape_context(context) extend_context(context) txt, html, att = construct_content(context) self.content_txt += txt self.content_html += html self.attachments += att hosts.add(context["HOSTNAME"]) last_context = contexts[-1] self.mailto = last_context['CONTACTEMAIL'] # Assume the same in each context # Use the single context subject in case there is only one context in the bulk self.subject = (utils.get_bulk_notification_subject(contexts, hosts) if len(contexts) > 1 else last_context['SUBJECT']) # TODO: cleanup duplicate code with SingleEmailContent # TODO: the context is only needed because of SMPT settings used in send_mail self.from_address = utils.format_address( last_context.get("PARAMETER_FROM_DISPLAY_NAME", u""), last_context.get("PARAMETER_FROM", utils.default_from_address())) self.reply_to = utils.format_address( last_context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", u""), last_context.get("PARAMETER_REPLY_TO", u"")) self.context = last_context
def main(): if bulk_mode: content_txt = "" parameters, contexts = utils.read_bulk_contexts() hosts = set() for context in contexts: context.update(parameters) content_txt += construct_content(context) mailto = context["CONTACTEMAIL"] # Assume the same in each context subject = context["SUBJECT"] hosts.add(context["HOSTNAME"]) # Use the single context subject in case there is only one context in the bulk if len(contexts) > 1: subject = utils.get_bulk_notification_subject(contexts, hosts) else: # gather all options from env context = utils.collect_context() content_txt = construct_content(context) mailto = context["CONTACTEMAIL"] subject = context["SUBJECT"] if not mailto: # e.g. empty field in user database sys.stdout.write( "Cannot send ASCII email: empty destination email address\n") sys.exit(2) # Create the mail and send it from_address = utils.format_address( context.get("PARAMETER_FROM_DISPLAY_NAME", ""), context.get("PARAMETER_FROM_ADDRESS", utils.default_from_address()), ) reply_to = utils.format_address( context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", ""), context.get("PARAMETER_REPLY_TO_ADDRESS", ""), ) m = utils.set_mail_headers( mailto, subject, from_address, reply_to, MIMEText(content_txt, "plain", _charset="utf-8")) try: sys.exit(utils.send_mail_sendmail(m, mailto, from_address)) except Exception as e: sys.stderr.write("Unhandled exception: %s\n" % e) # unhandled exception, don't retry this... sys.exit(2)
def __init__( self, context_function: Callable[[], tuple[dict[str, str], list[dict[str, str]]]] ) -> None: attachments = [] content_txt = "" content_html = "" parameters, contexts = context_function() hosts = set([]) for i, c in enumerate(contexts, 1): c.update(parameters) escaped_context = utils.html_escape_context(c) extend_context(escaped_context) txt, html, att = construct_content(escaped_context, is_bulk=True, notification_number=i) content_txt += txt content_html += html attachments += att hosts.add(c["HOSTNAME"]) # TODO: cleanup duplicate code with SingleEmailContent # TODO: the context is only needed because of SMPT settings used in send_mail super().__init__( context=escaped_context, # Assume the same in each context mailto=escaped_context["CONTACTEMAIL"], # Use the single context subject in case there is only one context in the bulk subject=(utils.get_bulk_notification_subject(contexts, hosts) if len(contexts) > 1 else escaped_context["SUBJECT"]), from_address=utils.format_address( escaped_context.get("PARAMETER_FROM_DISPLAY_NAME", ""), # TODO: Correct context parameter??? escaped_context.get("PARAMETER_FROM_ADDRESS", utils.default_from_address()), ), reply_to=utils.format_address( escaped_context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", ""), escaped_context.get("PARAMETER_REPLY_TO", ""), ), content_txt=content_txt, content_html=content_html, attachments=attachments, )
def __init__(self, context_function): attachments = [] content_txt = "" content_html = "" parameters, contexts = context_function() context = contexts[-1] hosts = set([]) for i, c in enumerate(contexts, 1): c.update(parameters) utils.html_escape_context(c) extend_context(c) txt, html, att = construct_content(c, is_bulk=True, notification_number=i) content_txt += txt content_html += html attachments += att hosts.add(c["HOSTNAME"]) # TODO: cleanup duplicate code with SingleEmailContent # TODO: the context is only needed because of SMPT settings used in send_mail super(BulkEmailContent, self).__init__( context=context, # Assume the same in each context mailto=context['CONTACTEMAIL'], # Use the single context subject in case there is only one context in the bulk subject=(utils.get_bulk_notification_subject(contexts, hosts) if len(contexts) > 1 else context['SUBJECT']), from_address=utils.format_address( context.get("PARAMETER_FROM_DISPLAY_NAME", u""), # TODO: Correct context parameter??? context.get("PARAMETER_FROM", utils.default_from_address())), reply_to=utils.format_address( context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", u""), context.get("PARAMETER_REPLY_TO", u"")), content_txt=content_txt, content_html=content_html, attachments=attachments, )
def __init__(self, context_function): # gather all options from env context = context_function() utils.html_escape_context(context) extend_context(context) content_txt, content_html, attachments = construct_content(context) self.content_txt = content_txt self.content_html = content_html self.attachments = attachments self.mailto = context['CONTACTEMAIL'] self.subject = context['SUBJECT'] # TODO: cleanup duplicate code with BulkEmailContent # TODO: the context is only needed because of SMPT settings used in send_mail self.from_address = utils.format_address( context.get("PARAMETER_FROM_DISPLAY_NAME", u""), context.get("PARAMETER_FROM_ADDRESS", utils.default_from_address())) self.reply_to = utils.format_address(context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", u""), context.get("PARAMETER_REPLY_TO_ADDRESS", u"")) self.context = context
def test_format_address(display_name, address, expected): actual = utils.format_address(display_name, address) assert actual == expected