def test_invalid_cc(self): with self.assertRaises(TypeError): mail_.Email(sender=self._valid_sender, recipients=self._valid_e_mail_list, subject=self._valid_string, content=self._valid_string, cc="")
def test_invalid_reply_to(self): with self.assertRaises(AssertionError): mail_.Email(sender=self._valid_sender, recipients=self._valid_e_mail_list, subject=self._valid_string, content=self._valid_string, cc=self._valid_e_mail_list, bcc=self._valid_e_mail_list, reply_to="")
def setUp(self): self._config = config_.ServiceConfig("sendgrid", os.environ.get('MAILGUN_API_KEY'), 1) self._domain = os.environ.get('MAIL_DOMAIN') self._service = mailgun.MailgunEmailService(config=self._config) self._email = mail_.Email(sender=("Test", "*****@*****.**"), recipients=["*****@*****.**"], subject="test", content="test_mailgun")
def test_message_creation(self): _message = mail_.Email(sender=self._valid_sender, recipients=self._valid_e_mail_list, subject=self._valid_string, content=self._valid_string, cc=self._valid_e_mail_list, bcc=self._valid_e_mail_list, reply_to=self._valid_sender[1]) self.assertEqual(self._valid_sender[0], _message._from_name) self.assertEqual(self._valid_sender[1], _message._from_address) self.assertEqual(self._valid_e_mail_list, _message._to) self.assertEqual(self._valid_string, _message._subject) self.assertEqual(self._valid_string, _message._body) self.assertEqual(self._valid_e_mail_list, _message._cc) self.assertEqual(self._valid_e_mail_list, _message._bcc) self.assertEqual(self._valid_sender[1], _message._reply_to)
def test_serialization(self): _message = mail_.Email(sender=self._valid_sender, recipients=self._valid_e_mail_list, subject=self._valid_string, content=self._valid_string, cc=self._valid_e_mail_list, bcc=self._valid_e_mail_list, reply_to=self._valid_sender[1]) expected_output_dict = { "sender": _message._from_address, "recipients": _message._to, "subject": _message._subject, "content": _message._body, "cc": _message._cc, "bcc": _message._bcc, "reply_to": _message._reply_to } self.assertEqual(json.dumps(expected_output_dict), str(_message))
def send_pooled_email(domain): """ Send an email via the Stampman PooledEmailService on the domain associate with the pool. Parameters: - "pool_api_key" (required): API Key associated with a specific pool - "from_email" (required): E-mail address of the sender - "from_name" (required): Name of the sender - "recipients" (required): Recipients of the E-mail (Either a list of strings or a single string in case fo a single recipient) - "cc" (optional): CC Recipients of the E-mail (Either a list of strings or a single string in case fo a single recipient) - "bcc" (optional): BCC Recipients of the E-mail (Either a list of strings or a single string in case fo a single recipient) - "subject" (optional): Subject of the E-mail - "content" (optional): Body of the E-mail - "reply_to" (optional): Reply address for the E-mail Data must be JSON encoded. """ if request.method == 'GET': return {"error": "Unexpected request; Expected POST."} if domain not in _pooled_service.domains: logging.error("{} requested unknown domain {}".format( request.remote_addr, domain)) return {"error": "Unknown Domain '{}'".format(domain)} if request.method == 'POST' and request.data.get( "pool_api_key") in _pooled_service.service_map.keys(): logging.info("Attempting to send email from {}".format( request.remote_addr)) try: sender_email = request.data['from_email'] recipients = request.data['recipients'] sender_name = request.data.get('from_name') sender_name = sender_name if sender_name else "" subject = request.data.get('subject') subject = subject if subject else "" content = request.data.get('content') content = content if content else "" cc = request.data.get('cc') cc = cc if cc else [] bcc = request.data.get('bcc') bcc = bcc if bcc else [] reply_to = request.data.get('reply_to') reply_to = reply_to if reply_to else sender_email except TypeError: return {"error": "Invalid Parameter Type"} except KeyError: return {"error": "Missing either from_email or recipients"} if not isinstance(recipients, list): recipients = [recipients] if not isinstance(cc, list): cc = [cc] if not isinstance(bcc, list): bcc = [bcc] response = _pooled_service.send_email( mail_.Email(sender=(sender_name, sender_email), recipients=recipients, subject=subject, content=content, cc=cc, bcc=bcc, reply_to=reply_to), request.data.get('pool_api_key')) if response["status"] == "failure": logging.error("Unable to send E-mail") return {"error": "Service temporarily unavailable"} else: logging.info("Email dispatched using {}".format( response["service_used"])) return response else: logging.error("{} requested unauthorized access to domain {}".format( request.remote_addr, domain)) return {"error": "Unauthorized Access: Please check your API key"}
def test_invalid_subject(self): with self.assertRaises(TypeError): mail_.Email(sender=self._valid_sender, recipients=self._valid_e_mail_list, subject=1)
def test_invalid_recipients(self): with self.assertRaises(AssertionError): mail_.Email(sender=self._valid_sender, recipients=self._invalid_e_mail_list)
def test_empty_recipients(self): with self.assertRaises(TypeError): mail_.Email(sender=self._valid_sender, recipients=[])
def test_invalid_sender_address(self): with self.assertRaises(AssertionError): mail_.Email(sender=self._invalid_sender)
def test_empty_sender(self): with self.assertRaises(TypeError): mail_.Email(sender="")