Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
    def test_has_invalid_email_addresses_BasicMessage_ReturnsListOfThree_WhenEachRecipientHasOneInvalid(self):
        # Arrange
        message = BasicMessage()
        message.to_email_address = [EmailAddress(self.random_helper.random_string())]
        message.cc_email_address = [EmailAddress(self.random_helper.random_string())]
        message.bcc_email_address = [EmailAddress(self.random_helper.random_string())]

        # Act
        actual = has_invalid_email_addresses(message)

        # Assert
        self.assertEqual(3, len(actual))
Beispiel #4
0
    def test_find_invalid_email_addresses_ListOfEmailAddress_ReturnsList_WhenInvalidRecipientsFound(self):
        # Arrange
        addresses = [EmailAddress(self.random_helper.random_string())]

        # Act
        actual = find_invalid_email_addresses(addresses)

        # Assert
        self.assertEqual(1, len(actual))
Beispiel #5
0
    def test_has_valid_reply_to_email_address_ReturnsFalse_WhenReplyToEmailIsEmpty(self):
        # Arrange
        message = BasicMessage()
        message.reply_to_email_address = EmailAddress('')

        # Act
        actual = has_valid_reply_to_email_address(message)

        # Assert
        self.assertFalse(actual)
Beispiel #6
0
    def test_has_from_email_address_ReturnsFalse_WhenFromEmailIsEmpty(self):
        # Arrange
        message = BasicMessage()
        message.from_email_address = EmailAddress('')

        # Act
        actual = has_from_email_address(message)

        # Assert
        self.assertFalse(actual)
Beispiel #7
0
    def test_validate_base_message_ReturnsEmailAddressValidationInvalidFrom_WhenFromRecipientIsInvalid(self):
        #  Arrange
        message = BasicMessage()
        message.subject = self.random_helper.random_string()
        message.from_email_address = EmailAddress("$$##%%")

        #  Act
        actual = validate_base_message(message)

        #  Assert
        self.assertEqual(SendResult.EmailAddressValidationInvalidFrom, 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
 def random_list_of_email_addresses(self, count: int):
     emails = []
     for _ in range(count):
         emails.append(EmailAddress(self.random_email_string()))
     return emails
 def random_email_address(self,
                          name_cnt: int = 8,
                          domain_cnt: int = 8,
                          suffix_cnt: int = 3):
     return EmailAddress(
         self.random_email_string(name_cnt, domain_cnt, suffix_cnt))