def _inbound_logic(json_body): message_data = json_body['Message'] email_to = parseaddr(message_data['To'][0]['EmailAddress'])[1] local_portion = email_to.split('@')[0] from_address = parseaddr(message_data['From']['EmailAddress'])[1] subject = message_data.get('Subject') text = message_data.get('TextBody') html = message_data.get('HtmlBody') # TODO: do we need this in SocketLabs? # 404s make sendgrid retry the email, so respond with 200 even if # the address isn't found try: relay_address = RelayAddress.objects.get(address=local_portion) except RelayAddress.DoesNotExist as e: print(e) return HttpResponse("Address does not exist") # Forward to real email address sl_message = BasicMessage() sl_message.subject = subject sl_message.html_body = html sl_message.plain_text_body = text relay_from_address, relay_from_display = _generate_relay_From(from_address) sl_message.from_email_address = EmailAddress( relay_from_address, relay_from_display ) sl_message.to_email_address.append(EmailAddress(relay_address.user.email)) socketlabs_client = SocketLabsClient( settings.SOCKETLABS_SERVER_ID, settings.SOCKETLABS_API_KEY ) response = socketlabs_client.send(sl_message) print(response)
def _inbound_logic(json_body): message_data = json_body['Message'] email_to = parseaddr(message_data['To'][0]['EmailAddress'])[1] local_portion = email_to.split('@')[0] from_address = parseaddr(message_data['From']['EmailAddress'])[1] subject = message_data.get('Subject') text = message_data.get('TextBody') html = message_data.get('HtmlBody') # TODO: do we need this in SocketLabs? # 404s make sendgrid retry the email, so respond with 200 even if # the address isn't found try: relay_address = RelayAddress.objects.get(address=local_portion) if not relay_address.enabled: relay_address.num_blocked += 1 relay_address.save(update_fields=['num_blocked']) return HttpResponse("Address does not exist") except RelayAddress.DoesNotExist as e: # TODO?: if sha256 of the address is in DeletedAddresses, # create a hard bounce receipt rule print(e) return HttpResponse("Address does not exist") logger.info( 'email_relay', extra={ 'fxa_uid': (relay_address.user.socialaccount_set.first().uid), 'relay_address_id': relay_address.id, 'relay_address': sha256(local_portion.encode('utf-8')).hexdigest(), 'real_address': sha256(relay_address.user.email.encode('utf-8')).hexdigest(), }) # Forward to real email address sl_message = BasicMessage() sl_message.subject = subject sl_message.html_body = html sl_message.plain_text_body = text relay_from_address, relay_from_display = _generate_relay_From(from_address) sl_message.from_email_address = EmailAddress(relay_from_address, relay_from_display) sl_message.to_email_address.append(EmailAddress(relay_address.user.email)) sl_client = _get_socketlabs_client() response = _socketlabs_send(sl_client, sl_message) # if _socketlabs_send returns a django HttpResponse return it immediately if type(response) == HttpResponse: return response if not response.result.name == 'Success': logger.error('socketlabs_error', extra=response.to_json()) return HttpResponse("Internal Server Error", status=500) relay_address.num_forwarded += 1 relay_address.last_used_at = datetime.now() relay_address.save(update_fields=['num_forwarded', 'last_used_at']) return HttpResponse("Created", status=201)
def test_has_message_body_ReturnsTrue_WhenApiTemplateIsNotEmptyAndPlainTextBodyAndHtmlBodyIsEmpty(self): # Arrange message = BasicMessage() message.html_body = None message.plain_text_body = None message.api_template = self.random_helper.random_int() # Act actual = has_message_body(message) # Assert self.assertTrue(actual)
def test_has_message_body_ReturnsFalse_WhenHtmlBodyAndPlainTextBodyAndApiTemplateIsEmpty(self): # Arrange message = BasicMessage() message.html_body = None message.plain_text_body = None message.api_template = None # Act actual = has_message_body(message) # Assert self.assertFalse(actual)
def test_validate_base_message_ReturnsSuccess_WhenSubjectAndFromRecipientAndPlainTextBodyIsNotEmpty(self): # Arrange message = BasicMessage() message.subject = self.random_helper.random_string() message.from_email_address = self.random_helper.random_email_address() message.plain_text_body = self.random_helper.random_string() # Act actual = validate_base_message(message) # Assert self.assertEqual(SendResult.Success, actual)
def test_validate_base_message_ReturnsMessageValidationEmptyMessage_WhenAllBodyFieldsAreEmpty(self): # Arrange message = BasicMessage() message.subject = self.random_helper.random_string() message.from_email_address = self.random_helper.random_email_address() message.html_body = None message.plain_text_body = None message.api_template = None # Act actual = validate_base_message(message) # Assert self.assertEqual(SendResult.MessageValidationEmptyMessage, actual)
def email_invited_user(invitee, invitation): context = { 'email': invitee.primary_email, 'current_domain': settings.SITE_ORIGIN, } # Send email invite sl_message = BasicMessage() sl_message.subject = ( "Firefox Relay beta: Protect your real email address from hackers and trackers" ) sl_message.html_body = render_to_string( 'emails/beta_invite_html_email.html', context, ) sl_message.plain_text_body = render_to_string( 'emails/beta_invite_text_email.txt', context, ) relay_display_name, relay_from_address = parseaddr( settings.RELAY_FROM_ADDRESS) sl_message.from_email_address = EmailAddress(relay_from_address) sl_message.to_email_address.append(EmailAddress(invitee.primary_email)) sl_client = get_socketlabs_client() response = socketlabs_send(sl_client, sl_message) if not response.result.name == 'Success': logger.error('socketlabs_error', extra=response.to_json()) return response invitation.date_sent = datetime.now(timezone.utc) invitation.save(update_fields=['date_sent']) invitee.waitlists_joined['email_relay']['notified'] = True invitee.save(update_fields=['waitlists_joined']) return response
from socketlabs.injectionapi import SocketLabsClient from socketlabs.injectionapi.message.basicmessage import BasicMessage from socketlabs.injectionapi.message.emailaddress import EmailAddress serverId = ####### injectionApiKey = "????????????????" client = SocketLabsClient(serverId, injectionApiKey); message = BasicMessage() message.subject = "Prove of Concept - Socketlab Email API" message.html_body = "<html>This is a test email sent through a .py file testing the email API. Text me if you get this email. Thanks - Ron</html>" message.plain_text_body = "This is a test email sent through a .py file testing the email API. Text me if you get this email. Thanks - Ron"; message.from_email_address = EmailAddress("no-reply@pencil_it_in.com") message.to_email_address.append(EmailAddress("*****@*****.**", "Ron Villena")) message.to_email_address.append(EmailAddress("*****@*****.**", "Zachary Lake")) message.to_email_address.append(EmailAddress("*****@*****.**", "Jeannie Halvorson")) message.to_email_address.append(EmailAddress("*****@*****.**", "Depinder Kaur")) response = client.send(message) # Instructions for creating email # https://cp.socketlabs.com/servers/32586/launch/languageselection/codesample?language=json