Esempio n. 1
0
def email_handler():

    email = PostmarkInbound(json=request.data)

    app.logger.debug('recipient addr = %s' % email.to())
    app.logger.debug('sender addr = %s' % email.sender())

    valid_recipients = [
        e for e in email.to() if e.get('Email').startswith(SERVICE_EMAIL)
    ]

    if valid_recipients:

        sender = email.sender()

        addr = sender.get('Email')
        name = sender.get('Name')

        if REGISTRATION_ENABLED and email.mailbox_hash() in ENABLED_HASHES:

            key = register_key(addr, name)

            if key:
                key_notification(key, addr)
                app.logger.debug('key = %s' % key)
            else:
                disabled_notification(addr)

        else:
            disabled_notification(addr)

    return ''
Esempio n. 2
0
    def post(self, request, *args, **kwargs):
        try:
            inbound = PostmarkInbound(json=request.body.decode('utf-8'))
            django_user, user, umi = User.get_or_create_user_from_email(inbound.sender()['Email'].lower())

            # get message id for email threading
            if 'Headers' in inbound.source and inbound.headers('Message-ID') is not None:
                msg_id = inbound.headers('Message-ID')
            else:
                msg_id = inbound.message_id()

            if not Message.objects.filter(email_uid=inbound.message_id()).exists():

                new_msg = Message.objects.create(created_at=inbound.send_date(),
                                                 to_originally=[r['Email'].lower() for r in inbound.to()],
                                                 subject=inbound.subject(),
                                                 msgbody=inbound.text_body(),
                                                 email_uid=msg_id,
                                                 user_message_info=umi)

                # first time user or it has been a long time since they've updated their address info
                if umi.must_update_address_info():
                    emailer.NoReply(django_user).email_confirm(new_msg).send()
                    return JsonResponse({'status': 'User must accept tos / update their address info.'})
                else:
                    MessageViewSet.process_inbound_message(django_user, umi, new_msg)
                    return JsonResponse({'status': 'Message queued for processing.'})
            else:
                return JsonResponse({'status': 'Message with provided ID already received.'})
                # TODO robust error handling
        except:
            client.captureException()
            return 'Failure', 500
Esempio n. 3
0
def mail_from_postmark(request):
    if request.method == 'POST':
        json_data = request.body
        #body = json.loads(json_data)['HtmlBody']
        inbound = PostmarkInbound(json=json_data)
        if inbound.has_attachments():
            attachments = inbound.attachments()
            names = []
            #absolue_uri = "<a href='"+request.build_absolute_uri(name1)+"'>" + name + "</a>"
            for attachment in attachments:
                name = attachment.name()
                name1 = settings.MEDIA_URL + 'attachments/' + name
                name2 = settings.MEDIA_ROOT + '/attachments/' + name
                names.append(name1)
                with open(name2, 'w') as f:
                    myFile = File(f)
                    myFile.write(attachment.read())
            mail = Inboundmail(html_body=inbound.text_body(),
                               send_date=inbound.send_date(),
                               subject=inbound.subject(),
                               reply_to=inbound.reply_to(),
                               sender=inbound.sender(),
                               attachment=','.join(names))
            #pdb.set_trace()
        else:
            mail = Inboundmail(html_body=inbound.text_body(),
                               send_date=inbound.send_date(),
                               subject=inbound.subject(),
                               reply_to=inbound.reply_to(),
                               sender=inbound.sender())
        send_mail(
            inbound.subject(),
            inbound.text_body() + '\n\n' + 'Email: ' +
            inbound.sender().get('Email') + '\n' + 'Name: ' +
            inbound.sender().get('Name'),
            '*****@*****.**',
            ['*****@*****.**'],
            fail_silently=True,
        )
        mail.save()
        return HttpResponse('OK')
    else:
        return HttpResponse('not OK')
Esempio n. 4
0
 def setUp(self):
     json_data = open('tests/fixtures/valid_http_post.json').read()
     self.inbound = PostmarkInbound(json=json_data)