def _send_mail(self, receivers_list, subject, body, log_files=None): """Actually sends the mail with `subject` and `body` and optionanl log_file attachements to all members of `receivers_list`.""" if log_files: msg = MIMEMultipart() msg.attach(MIMEText(body)) for entry in log_files: log_mime = MIMEBase('application', "octet-stream") log_file = entry[0] # Output.file log_file.seek(0) log_mime.set_payload(log_file.read()) encoders.encode_base64(log_mime) log_mime.add_header( 'Content-Disposition', 'attachment; filename="{}"'.format(entry[1]['filename'])) msg.attach(log_mime) else: msg = MIMEText(body) msg['Subject'] = subject msg['From'] = self.from_address msg['To'] = ', '.join([x.strip() for x in receivers_list]) s = None try: s = get_smtp_session(self.workflow, self.smtp_fallback) s.sendmail(self.from_address, receivers_list, msg.as_string()) except (socket.gaierror, smtplib.SMTPException): self.log.error('Error communicating with SMTP server') raise finally: if s is not None: s.quit()
def test_get_smtp_session(self, fallback, config, raise_error): tasker, workflow = self.prepare() workflow.plugin_workspace[ReactorConfigPlugin.key] = {} if raise_error: with pytest.raises(Exception): read_yaml(config, 'schemas/config.json') return config_json = read_yaml(config, 'schemas/config.json') fallback_map = {} if fallback: fallback_map['host'] = config_json['smtp']['host'] else: workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig(config_json) (flexmock(smtplib.SMTP).should_receive('__init__').with_args( config_json['smtp']['host']).once().and_return(None)) get_smtp_session(workflow, fallback_map)
def test_get_smtp_session(self, fallback, config, raise_error): tasker, workflow = self.prepare() workflow.plugin_workspace[ReactorConfigPlugin.key] = {} if raise_error: with pytest.raises(Exception): read_yaml(config, 'schemas/config.json') return config_json = read_yaml(config, 'schemas/config.json') fallback_map = {} if fallback: fallback_map['host'] = config_json['smtp']['host'] else: workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig(config_json) (flexmock(smtplib.SMTP) .should_receive('__init__') .with_args(config_json['smtp']['host']) .once() .and_return(None)) get_smtp_session(workflow, fallback_map)
def _send_mail(self, receivers_list, subject, body, log_files=None): if not receivers_list: self.log.info('no valid addresses in requested addresses. Doing nothing') return self.log.info('sending notification to %s ...', receivers_list) """Actually sends the mail with `subject` and `body` and optionanl log_file attachements to all members of `receivers_list`.""" if log_files: msg = MIMEMultipart() msg.attach(MIMEText(body)) for entry in log_files: log_mime = MIMEBase('application', "octet-stream") log_file = entry[0] # Output.file log_file.seek(0) log_mime.set_payload(log_file.read()) encoders.encode_base64(log_mime) log_mime.add_header('Content-Disposition', 'attachment; filename="{}"'.format(entry[1]['filename'])) msg.attach(log_mime) else: msg = MIMEText(body) msg['Subject'] = subject msg['From'] = self.from_address msg['To'] = ', '.join([x.strip() for x in receivers_list]) s = None try: s = get_smtp_session(self.workflow, self.smtp_fallback) s.sendmail(self.from_address, receivers_list, msg.as_string()) except (socket.gaierror, smtplib.SMTPException): self.log.error('Error communicating with SMTP server') raise finally: if s is not None: s.quit()