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 send_email(): msg = MIMEMultipart() sheet = get_sheet() email_file = MIMEApplication(save_virtual_workbook(sheet)) encoders.encode_base64(email_file) email_file.set_payload(email_file.get_payload()) email_file.add_header( "Content-Disposition", "attachment", filename="{}日报{}-{}.xlsx".format(name, today.month, today.day), ) msg.attach(email_file) msg.attach(MIMEText(get_content(sheet), _subtype="html", _charset="utf-8")) msg["From"] = sender msg["To"] = receiver msg["Subject"] = subject msg["Cc"] = copy_to smtp = smtplib.SMTP("smtp.live.com", 587) smtp.set_debuglevel(1) smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login(sender, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()
def test_send_bcc_smtp(self, mock_send_mime): attachment = tempfile.NamedTemporaryFile() attachment.write(b'attachment') attachment.seek(0) utils.email.send_email_smtp('to', 'subject', 'content', files=[attachment.name], cc='cc', bcc='bcc') self.assertTrue(mock_send_mime.called) _, call_args = mock_send_mime.call_args self.assertEqual(conf.get('smtp', 'SMTP_MAIL_FROM'), call_args['e_from']) self.assertEqual(['to', 'cc', 'bcc'], call_args['e_to']) msg = call_args['mime_msg'] self.assertEqual('subject', msg['Subject']) self.assertEqual(conf.get('smtp', 'SMTP_MAIL_FROM'), msg['From']) self.assertEqual(2, len(msg.get_payload())) self.assertEqual( 'attachment; filename="' + os.path.basename(attachment.name) + '"', msg.get_payload()[-1].get('Content-Disposition'), ) mimeapp = MIMEApplication('attachment') self.assertEqual(mimeapp.get_payload(), msg.get_payload()[-1].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] 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_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_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): with tempfile.NamedTemporaryFile() as attachment: attachment.write(b'attachment') attachment.seek(0) utils.email.send_email_smtp('to', 'subject', 'content', files=[attachment.name]) assert mock_send_mime.called _, call_args = mock_send_mime.call_args assert conf.get('smtp', 'SMTP_MAIL_FROM') == call_args['e_from'] assert ['to'] == call_args['e_to'] msg = call_args['mime_msg'] assert 'subject' == msg['Subject'] assert conf.get('smtp', 'SMTP_MAIL_FROM') == msg['From'] assert 2 == len(msg.get_payload()) filename = 'attachment; filename="' + os.path.basename(attachment.name) + '"' assert filename == msg.get_payload()[-1].get('Content-Disposition') mimeapp = MIMEApplication('attachment') assert mimeapp.get_payload() == msg.get_payload()[-1].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 attachment(self, logs_path): date = datetime.now().strftime("%d_%m_%Y_%H_%M_%S") zip_file_path = os.path.join(self.zipped_path, 'app_logs_{}.zip'.format(date)) zipf = zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) if not logs_path.endswith('/'): logs_path += '/' self._zip_dir(logs_path, zipf) zipf.close() fp = open(zip_file_path, "rb") part = MIMEApplication(fp.read()) fp.close() encoders.encode_base64(part) # the miracle part.set_payload(part.get_payload()) part.add_header( 'Content-Disposition', 'attachment; filename="%s"' % 'app_logs_{}.zip'.format(logs_path.split('\\')[-1])) return part