Best Python code snippet using mailosaur-python_python
email_tool.py
Source:email_tool.py
...11import logging1213logger = logging.getLogger('airenao')1415def send_emails(subject, content, from_address, to_list):16 connection = mail.get_connection()17 tmp_flag = True18 while tmp_flag:19 try:20 connection.open()21 tmp_flag = False22 except Exception:23 pass24 email1 = mail.EmailMessage(subject, content, from_address, to_list, connection = connection)25 email1.content_subtype = "html"26 email1.send()27 connection.close()2829def send_email(outbox_message, message, party):30 subject = u'[ç±çé¹]æ¨æ¶å°ä¸ä¸ªæ´»å¨é请'31 try:32 address_list = outbox_message.address.split(',')33 if message.is_apply_tips:34 for address in address_list:35 enroll_link = DOMAIN_NAME + '/parties/%d/enroll/?key=%s' % (party.id, hashlib.md5('%d:%s' % (party.id, address)).hexdigest())36 content = message.content37 content = content.replace('\n\r', '<br />')38 content = content + u'\r\nå¿«æ¥æ¥åï¼<a href="%s">%s</a>' % (enroll_link, enroll_link)39 send_emails(subject, content, SYS_EMAIL_ADDRESS, [address])40 else:41 send_emails(subject, message.content,42 SYS_EMAIL_ADDRESS, address_list)43 except:44 logger.exception('send email error!')45 finally:46 outbox_message.delete()4748def send_binding_email(instance):49 email = instance.binding_address50 key = instance.key51 subject = u'ãç±çé¹ãå¸å·ç»å®ç¡®è®¤é®ä»¶'52 link = '%s/accounts/email_handle_url/binding/?key=%s' % (DOMAIN_NAME, key)53 content = u'å°æ¬çç±çé¹ç¨æ·ï¼å½æ¨çå°è¿å°é®ä»¶æ¶ï¼è¯´ææ¨æ£å¨è¿è¡ç»å®é®ç®±çæä½ã å¦æä¸æ¯æ¨èªå·±è¿è¡çæä½ï¼è¯·å é¤æ¬é®ä»¶ã<br/> 请ç¹å»ä»¥ä¸é¾æ¥ç»å®æ¨çé®ç®±ï¼ <a href="%s" > %s </a>' % (link, link)54 try:55 send_emails(subject, content, SYS_EMAIL_ADDRESS, [email])56 except:57 logger.exception('send sendEmailBingdingmessage error!')5859def send_unbinding_email(instance):60 email = instance.binding_address61 key = instance.key62 subject = u'ãç±çé¹ãå¸å·è§£é¤ç»å®ç¡®è®¤é®ä»¶'63 link = '%s/accounts/email_handle_url/unbinding/?key=%s' % (DOMAIN_NAME, key)64 content = u'å°æ¬çç±çé¹ç¨æ·ï¼å½æ¨çå°è¿å°é®ä»¶æ¶ï¼è¯´ææ¨æ£å¨è¿è¡è§£é¤ç»å®é®ç®±çæä½ã å¦æä¸æ¯æ¨èªå·±è¿è¡çæä½ï¼è¯·å é¤æ¬é®ä»¶ã<br/> 请ç¹å»ä»¥ä¸é¾æ¥ç»å®æ¨çé®ç®±ï¼ <a href="%s" > %s </a>' % (link, link)65 try:66 send_emails(subject, content, SYS_EMAIL_ADDRESS, [email])67 except:68 logger.exception('send sendEmailBingdingmessage error!')697071def send_forget_pwd_email(instance):72 email = instance.user.userprofile.email73 key = instance.temp_password74 subject = u'ãç±çé¹ãæ¾åå¯ç '75 content = u'å°æ¬çç±çé¹ç¨æ·ï¼<br> æ¨ç临æ¶å¯ç 为: <br> <p style="font-size:22px;">%s</p> <br> 该å¯ç åªè½ä½¿ç¨ä¸æ¬¡ï¼è¯·å°½å¿«ç»éæ们çåºç¨/ç½ç«(<a href="http://www.airenao.com/">http://www.airenao.com</a>)ï¼å¹¶ä¿®æ¹æ¨çå¯ç ã<br>ç¥æ¨ä½¿ç¨æå¿«<br>ç±çé¹å¼åå¢é' % key76 try:77 send_emails(subject, content, SYS_EMAIL_ADDRESS, [email])78 except:79 logger.exception('send temp password error! username:%s', instance.user.username)8081def send_apply_confirm_email(party_client):82 party = party_client.party83 client = party_client.client84# å°æ¬çxxxï¼æ¨ååæ¥ååå äºxxxåå¸çæ´»å¨ï¼è¯·ç¹å»ä»¥ä¸é¾æ¥ç该活å¨ï¼85# http://www.airenao.com/accounts/email_handle_url/binding/?key=3e328929d2bef50cd2c8e0b626c639d786 content = u'å°æ¬ç' + client.name=='' and client.email or client.name + u'ï¼æ¨ååæ¥ååå äº' + party.creator.username + u'åå¸çæ´»å¨ï¼è¯·ç¹å»ä»¥ä¸é¾æ¥ç该活å¨ï¼'87 enroll_link = DOMAIN_NAME + '/parties/%d/enroll/?key=%s' % (party.id, hashlib.md5('%d:%s' % (party.id, client.email)).hexdigest())88 content = content + u'\r\nå¿«æ¥æ¥åï¼<a href="%s">%s</a>' % (enroll_link, enroll_link)89 subject = u'ãç±çé¹ãæ¥åæéé®ä»¶'90 email = client.email91 try:92 send_emails(subject, content, SYS_EMAIL_ADDRESS, [email])93 except:
...
test_send_email.py
Source:test_send_email.py
1from unittest import TestCase2from unittest.mock import patch3from auth import app4from auth.helpers.send_emails import send_email, send_confirmation_email, send_forgot_email5class TestSendEmail(TestCase):6 def setUp(self) -> None:7 app.config['TESTING'] = True8 self.app = app.test_client()9 def test_correct_data(self):10 with app.test_request_context():11 response = send_email("topic", ["katarzyna.rzesikowska@gmail.com"], "Kasia")12 self.assertTrue(response)13 def test_incorrect_email(self):14 with app.test_request_context():15 response = send_email("topic", ["katarzyna.rzesikowska"], "Kasia")16 self.assertFalse(response)17 def test_incorrect_email_format(self):18 with app.test_request_context():19 response = send_email("topic", "katarzyna.rzesikowska@gmail.com", "Kasia")20 self.assertFalse(response)21 @patch("auth.helpers.send_emails.Message")22 def test_Message_called(self, mock_Message):23 send_email("subject", ["recipients"], "html_body")24 mock_Message.assert_called()25 @patch("auth.helpers.send_emails.Message")26 def test_Message_called_once(self, mock_Message):27 send_email("subject", ["recipients"], "html_body")28 mock_Message.assert_called_once()29 @patch("auth.helpers.send_emails.Message")30 def test_Message_called_with(self, mock_Message):31 send_email("subject", ["recipients"], "html_body")32 mock_Message.assert_called_with("subject", recipients=["recipients"])33 @patch("auth.helpers.send_emails.Message")34 @patch("auth.helpers.send_emails.mail")35 def test_mail_called(self, mock_mail, mock_Message):36 send_email("subject", ["recipients"], "html_body")37 mock_mail.send.assert_called()38 @patch("auth.helpers.send_emails.Message")39 def test_Message_throws_exception(self, mock_Message):40 mock_Message.side_effect = Exception41 response = send_email("subject", ["recipients"], "html_body")42 self.assertFalse(response)43 @patch("auth.helpers.send_emails.Message")44 @patch("auth.helpers.send_emails.mail")45 def test_mail_called_once(self, mock_mail, mock_Message):46 send_email("subject", ["recipients"], "html_body")47 mock_mail.send.assert_called_once()48 @patch("auth.helpers.send_emails.Message")49 @patch("auth.helpers.send_emails.mail")50 def test_mail_called_with_object_Message(self, mock_mail, mock_Message):51 send_email("subject", ["recipients"], "html_body")52 msg = mock_Message("subject", recipients=["recipients"])53 mock_mail.send.assert_called_with(msg)54 @patch("auth.helpers.send_emails.Message")55 @patch("auth.helpers.send_emails.mail")56 def test_mail_throws_exception(self, mock_mail, mock_Message):57 mock_mail.send.side_effect = Exception58 response = send_email("subject", ["recipients"], "html_body")59 self.assertFalse(response)60 @patch("auth.helpers.send_emails.Message")61 @patch("auth.helpers.send_emails.mail")62 def test_function_return_true(self, mock_mail, mock_Message):63 response = send_email("subject", ["recipients"], "html_body")64 self.assertTrue(response)65class TestSendConfirmationEmail(TestCase):66 def setUp(self) -> None:67 app.config['TESTING'] = True68 self.app = app.test_client()69 def test_correct_data(self):70 with app.test_request_context():71 res = send_confirmation_email("katarzyna.rzesikowska@gmail.com", "Kasia")72 self.assertTrue(res)73 def test_incorrect_email(self):74 with app.test_request_context():75 res = send_confirmation_email("katarzyna.rzesikowska", "Kasia")76 self.assertFalse(res)77class TestSendForgotEmail(TestCase):78 def setUp(self) -> None:79 app.config['TESTING'] = True80 self.app = app.test_client()81 def test_correct_data(self):82 with app.test_request_context():83 res = send_forgot_email("katarzyna.rzesikowska@gmail.com", "Kasia")84 self.assertTrue(res)85 def test_incorrect_email(self):86 with app.test_request_context():87 res = send_forgot_email("katarzyna.rzesikowska", "Kasia")...
test_emailer.py
Source:test_emailer.py
...7class TestEmailer(TestCase):8 @skip("is not seeing the call to connect() in SMTP constructor")9 @patch('smtplib.SMTP')10 def test_conects_to_the_right_host_and_port(self, smtp_spy):11 Emailer('', '').send_emails({}, '')12 smtp_spy.return_value.connect.assert_called_once_with(MAILHOST, TLS_PORT)13 @patch('smtplib.SMTP')14 def test_encrypts_using_TLS(self, smtp_spy):15 Emailer('', '').send_emails({}, '')16 self.assertTrue(smtp_spy.return_value.starttls.called)17 @patch('smtplib.SMTP')18 def test_logs_in(self, smtp_spy):19 Emailer('username', 'password').send_emails({}, '')20 smtp_spy.return_value.login.assert_called_once_with(21 'username', 'password')22 @patch('smtplib.SMTP')23 def test_sends_email_with_the_right_arguments(self, smtp_spy):24 emails = {'to@domain.com': 'name'}25 forecast = ForecastResponseFormatter(FAKE_RESPONSE)26 Emailer('username', '').send_emails(emails, forecast)27 ((username, email, message), b) = smtp_spy.return_value.sendmail.call_args28 self.assertEqual(username, 'username')29 self.assertEqual(email, 'to@domain.com')30 self.assertIsNotNone(message)31 self.assertTrue('name' in message)32 @skip("is not intercepting EmailFormatter")33 @patch('smtplib.SMTP')34 @patch('weather_forecast_emailer.EmailFormatter')35 def test_sends_email_to_several_recipients(self, fake_formatter, smtp_spy):36 fake_formatter.return_value.build_message.return_value = 'message'37 emails = {'to@domain.com': 'name', 'foo@domain.com': 'foo name'}38 Emailer('username', '').send_emails(emails, None)39 self.assertEqual(smtp_spy.return_value.sendmail.call_count, 2)40 smtp_spy.return_value.sendmail.assert_any_call(41 'username', 'foo@domain.com', 'message')42 @skip("is not intercepting EmailFormatter")43 @patch('smtplib.SMTP')44 @patch('weather_forecast_emailer.EmailFormatter')45 def test_builds_the_message_to_send(self, fake_formatter, smtp_spy):46 emails = {'to@domain.com': 'name'}47 Emailer('username', '').send_emails(emails, None)48 fake_formatter.return_value.build_message.assert_called_once_with(49 'to@domain.com', 'name', None)50 @patch('smtplib.SMTP')51 def test_quits_when_finished(self, smtp_spy):52 Emailer('', '').send_emails({}, '')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!