in output/mail.py [0:0]
def output(self):
mail = {
'html_body': '',
'text_body': '',
'mail_from': '',
'mail_to': '',
'mail_subject': '',
}
if 'from' not in self.output_config:
raise NotConfiguredException(
'No sender (from) configured for email output!')
if 'to' not in self.output_config:
raise NotConfiguredException(
'No recipient (to) configured for email output!')
if 'subject' not in self.output_config:
raise NotConfiguredException(
'No subject configured for email output!')
if 'body' not in self.output_config:
raise NotConfiguredException('No body configured for email output!')
for mail_type in ['html', 'text']:
if mail_type in self.output_config['body']:
mail_template = self.jinja_environment.from_string(
self.output_config['body'][mail_type])
mail_template.name = mail_type
mail['%s_body' % mail_type] = mail_template.render()
if mail['html_body'] == '' and mail['text_body'] == '':
raise NotConfiguredException(
'No HTML or text email body configured for email output!')
for tpl in ['from', 'to', 'subject']:
result = self._jinja_expand_string(self.output_config[tpl], tpl)
mail['mail_%s' % tpl] = result
self.logger.debug('Canonicalizing email formats...')
# Canonicalize the email formats
for tpl in ['from', 'to']:
parsed_emails = []
try:
parsed_emails = email.utils.getaddresses(
[mail['mail_%s' % tpl]], strict=False)
except TypeError:
parsed_emails = email.utils.getaddresses(
[mail['mail_%s' % tpl]])
if tpl == 'from' and len(parsed_emails) > 1:
raise MultipleSendersException(
'Multiple senders in from field!')
new_email = ''
for e in parsed_emails:
new_email += ', ' if new_email != '' else ''
new_email += email.utils.formataddr(e)
mail['mail_%s' % tpl] = new_email
if 'expandGroupRecipients' in self.output_config and self.output_config[
'expandGroupRecipients']:
mail = self.expand_recipients(self, mail, self.output_config)
if 'transports' not in self.output_config:
raise NotConfiguredException(
'No transports configured for sending email.')
embedded_images = {}
if 'images' in self.output_config['body']:
embedded_images = self.embed_images(self.output_config)
sent_successfully = False
for transport in self.output_config['transports']:
try:
if transport['type'] == 'smtp':
sent_successfully = self.send_via_smtp(
transport, mail, embedded_images, self.output_config)
if sent_successfully:
break
elif transport['type'] == 'sendgrid':
sent_successfully = self.send_via_sendgrid(
transport, mail, embedded_images, self.output_config)
if sent_successfully:
break
elif transport['type'] == 'msgraphapi':
sent_successfully = self.send_via_msgraphapi(
transport, mail, embedded_images, self.output_config)
if sent_successfully:
break
else:
self.logger.exception(
'Unknown transport type %s in configuration.' %
transport['type'])
except Exception:
transport_sanitized = transport
transport_sanitized.pop('apiKey', None)
transport_sanitized.pop('user', None)
transport_sanitized.pop('password', None)
self.logger.exception('Error when attempting to use transport.',
extra={
'transport': transport_sanitized,
'mail': mail
})
if not sent_successfully:
self.logger.error(
'Unable to send email, none of the transports worked.')
raise AllTransportsFailedException(
'Unable to send email, none of the transports worked.')
else:
self.logger.info('Message sent!',
extra={
'from': mail['mail_from'],
'to': mail['mail_to'],
'subject': mail['mail_subject']
})