def test_send_smtp_with_email_mutator(self, mock_send_mime): attachment = tempfile.NamedTemporaryFile() attachment.write(b"attachment") attachment.seek(0) # putting this into a variable so that we can reset after the test base_email_mutator = app.config["EMAIL_HEADER_MUTATOR"] def mutator(msg, **kwargs): msg["foo"] = "bar" return msg app.config["EMAIL_HEADER_MUTATOR"] = mutator utils.send_email_smtp( "to", "subject", "content", app.config, files=[attachment.name] ) assert mock_send_mime.called call_args = mock_send_mime.call_args[0] logger.debug(call_args) assert call_args[0] == app.config["SMTP_MAIL_FROM"] assert call_args[1] == ["to"] msg = call_args[2] assert msg["Subject"] == "subject" assert msg["From"] == app.config["SMTP_MAIL_FROM"] assert msg["foo"] == "bar" assert len(msg.get_payload()) == 2 mimeapp = MIMEApplication("attachment") assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload() app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator
def _deliver_email(schedule, subject, email): for (to, bcc) in _get_recipients(schedule): send_email_smtp( to, subject, email.body, config, data=email.data, images=email.images, bcc=bcc, mime_subtype='related', dryrun=config.get('SCHEDULED_EMAIL_DEBUG_MODE'), )
def test_send_smtp_data(self, mock_send_mime): utils.send_email_smtp( 'to', 'subject', 'content', app.config, data={'1.txt': b'data'}) assert mock_send_mime.called call_args = mock_send_mime.call_args[0] logging.debug(call_args) assert call_args[0] == app.config.get('SMTP_MAIL_FROM') assert call_args[1] == ['to'] msg = call_args[2] assert msg['Subject'] == 'subject' assert msg['From'] == app.config.get('SMTP_MAIL_FROM') assert len(msg.get_payload()) == 2 mimeapp = MIMEApplication('data') assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
def test_send_smtp_inline_images(self, mock_send_mime): image = read_fixture('sample.png') utils.send_email_smtp( 'to', 'subject', 'content', app.config, images=dict(blah=image)) assert mock_send_mime.called call_args = mock_send_mime.call_args[0] logging.debug(call_args) assert call_args[0] == app.config.get('SMTP_MAIL_FROM') assert call_args[1] == ['to'] msg = call_args[2] assert msg['Subject'] == 'subject' assert msg['From'] == app.config.get('SMTP_MAIL_FROM') assert len(msg.get_payload()) == 2 mimeapp = MIMEImage(image) assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
def test_send_smtp_data(self, mock_send_mime): utils.send_email_smtp( "to", "subject", "content", app.config, data={"1.txt": b"data"} ) assert mock_send_mime.called call_args = mock_send_mime.call_args[0] logger.debug(call_args) assert call_args[0] == app.config["SMTP_MAIL_FROM"] assert call_args[1] == ["to"] msg = call_args[2] assert msg["Subject"] == "subject" assert msg["From"] == app.config["SMTP_MAIL_FROM"] assert len(msg.get_payload()) == 2 mimeapp = MIMEApplication("data") assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
def test_send_smtp_inline_images(self, mock_send_mime): image = read_fixture("sample.png") utils.send_email_smtp( "to", "subject", "content", app.config, images=dict(blah=image) ) assert mock_send_mime.called call_args = mock_send_mime.call_args[0] logger.debug(call_args) assert call_args[0] == app.config["SMTP_MAIL_FROM"] assert call_args[1] == ["to"] msg = call_args[2] assert msg["Subject"] == "subject" assert msg["From"] == app.config["SMTP_MAIL_FROM"] assert len(msg.get_payload()) == 2 mimeapp = MIMEImage(image) assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
def _deliver_email( schedule: Union[DashboardEmailSchedule, SliceEmailSchedule], subject: str, email: EmailContent, ) -> None: for (to, bcc) in _get_recipients(schedule): send_email_smtp( to, subject, email.body, config, data=email.data, images=email.images, bcc=bcc, mime_subtype="related", dryrun=config["SCHEDULED_EMAIL_DEBUG_MODE"], )
def test_send_bcc_smtp(self, mock_send_mime): attachment = tempfile.NamedTemporaryFile() attachment.write(b'attachment') attachment.seek(0) utils.send_email_smtp( 'to', 'subject', 'content', app.config, files=[attachment.name], cc='cc', bcc='bcc') assert mock_send_mime.called call_args = mock_send_mime.call_args[0] assert call_args[0] == app.config.get('SMTP_MAIL_FROM') assert call_args[1] == ['to', 'cc', 'bcc'] msg = call_args[2] assert msg['Subject'] == 'subject' assert msg['From'] == app.config.get('SMTP_MAIL_FROM') assert len(msg.get_payload()) == 2 mimeapp = MIMEApplication('attachment') assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
def test_send_smtp(self, mock_send_mime): attachment = tempfile.NamedTemporaryFile() attachment.write(b"attachment") attachment.seek(0) utils.send_email_smtp( "to", "subject", "content", app.config, files=[attachment.name] ) assert mock_send_mime.called call_args = mock_send_mime.call_args[0] logger.debug(call_args) assert call_args[0] == app.config["SMTP_MAIL_FROM"] assert call_args[1] == ["to"] msg = call_args[2] assert msg["Subject"] == "subject" assert msg["From"] == app.config["SMTP_MAIL_FROM"] assert len(msg.get_payload()) == 2 mimeapp = MIMEApplication("attachment") assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
def _deliver_email( # pylint: disable=too-many-arguments recipients: str, deliver_as_group: bool, subject: str, body: str, data: Optional[Dict[str, Any]], images: Optional[Dict[str, bytes]], ) -> None: for (to, bcc) in _get_email_to_and_bcc(recipients, deliver_as_group): send_email_smtp( to, subject, body, config, data=data, images=images, bcc=bcc, mime_subtype="related", dryrun=config["SCHEDULED_EMAIL_DEBUG_MODE"], )
def send(self) -> None: subject = self._get_subject() content = self._get_content() to = self._get_to() try: send_email_smtp( to, subject, content.body, app.config, files=[], data=content.data, images=content.images, bcc="", mime_subtype="related", dryrun=False, ) logger.info("Report sent to email") except Exception as ex: raise NotificationError(ex)