How to use send_emails method in mailosaur-python

Best Python code snippet using mailosaur-python_python

email_tool.py

Source: email_tool.py Github

copy

Full Screen

...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: ...

Full Screen

Full Screen

test_send_email.py

Source: test_send_email.py Github

copy

Full Screen

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")...

Full Screen

Full Screen

test_emailer.py

Source: test_emailer.py Github

copy

Full Screen

...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({}, '')...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Webinar: Move Forward With An Effective Test Automation Strategy [Voices of Community]

The key to successful test automation is to focus on tasks that maximize the return on investment (ROI), ensuring that you are automating the right tests and automating them in the right way. This is where test automation strategies come into play.

How Testers Can Remain Valuable in Agile Teams

Traditional software testers must step up if they want to remain relevant in the Agile environment. Agile will most probably continue to be the leading form of the software development process in the coming years.

Getting Started with SpecFlow Actions [SpecFlow Automation Tutorial]

With the rise of Agile, teams have been trying to minimize the gap between the stakeholders and the development team.

Putting Together a Testing Team

As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.

Oct’22 Updates: New Analytics And App Automation Dashboard, Test On Google Pixel 7 Series, And More

Hey everyone! We hope you had a great Hacktober. At LambdaTest, we thrive to bring you the best with each update. Our engineering and tech teams work at lightning speed to deliver you a seamless testing experience.

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mailosaur-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful