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()) mail.save() return HttpResponse('OK') else: return HttpResponse('not OK')
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
def mail_from_postmark(request): if request.method == 'POST': json_data = request.body body = json.loads(json_data)['HtmlBody'] inbound = PostmarkInbound(json=json_data) mail = Inboundmail(html_body=inbound.text_body(), send_date=inbound.send_date(), subject=inbound.subject(), reply_to=inbound.reply_to(), sender=inbound.sender()) mail.save() return HttpResponse('OK') else: return HttpResponse('not OK')
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')
def email_handle_incoming(request): if request.method == 'GET': return HttpResponse("Webhook Endpoint") elif request.method == 'POST': print request.body logger.error(request.body) inbound = PostmarkInbound(json=request.body) print inbound print inbound.subject() print inbound.sender()['Email'] print inbound.text_body() email = Email.objects.get_or_create( message_id=inbound.message_id(), subject=inbound.subject(), recieved=inbound.send_date(), from_address=inbound.sender()['Email'], text_body=inbound.text_body(), html_body=inbound.html_body(), )[0] print email email.populate_data() print "Youtube ID found in email: %s" % email.youtube_id video = Video.objects.get_or_create( youtube_id=email.youtube_id )[0] video.populate_data() video.save() return HttpResponse(request.body)
def postmark_inbound(): """ View to handle inbound postmark e-mails. @return: """ try: # parse inbound postmark JSON inbound = PostmarkInbound(json=request.get_data()) # create or get the user and his default information user = db_first_or_create(User, email=inbound.sender()['Email'].lower()) umi = db_first_or_create(UserMessageInfo, user_id=user.id, default=True) 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() # check if message exists already first if Message.query.filter_by(email_uid=inbound.message_id()).first() is None: new_msg = Message(created_at=inbound.send_date(), to_originally=json.dumps([r['Email'].lower() for r in inbound.to()]), subject=inbound.subject(), msgbody=inbound.text_body(), email_uid=msg_id, user_message_info_id=umi.id) db_add_and_commit(new_msg) # first time user or it has been a long time since they've updated their address info if umi.should_update_address_info(): emailer.NoReply.validate_user(user, new_msg).send() return jsonify({'status': 'user must accept tos / update their address info'}) else: new_msg.update_status() return process_inbound_message(user, umi, new_msg, send_email=True) else: return jsonify({'status': 'message already received'}) except: print traceback.format_exc() return "Unable to parse postmark message.", 500
class PostmarkInboundTest(unittest.TestCase): def setUp(self): json_data = open('tests/fixtures/valid_http_post.json').read() self.inbound = PostmarkInbound(json=json_data) def tearDown(self): if os.path.exists('./tests/chart.png'): os.remove('./tests/chart.png') if os.path.exists('./tests/chart2.png'): os.remove('./tests/chart2.png') def test_should_have_a_subject(self): assert 'Hi There' == self.inbound.subject() def test_should_have_a_bcc(self): assert '*****@*****.**' == self.inbound.bcc() def test_should_have_a_cc(self): assert '*****@*****.**' == self.inbound.cc()[0]['Email'] def test_should_have_a_reply_to(self): assert '*****@*****.**' == self.inbound.reply_to() def test_should_have_a_mailbox_hash(self): assert 'moitoken' == self.inbound.mailbox_hash() def test_should_have_a_tag(self): assert 'yourit' == self.inbound.tag() def test_should_have_a_message_id(self): assert 'a8c1040e-db1c-4e18-ac79-bc5f64c7ce2c' == self.inbound.message_id( ) def test_should_be_from_someone(self): assert self.inbound.sender()['Name'] == 'Bob Bobson' and \ self.inbound.sender()['Email'] == '*****@*****.**' def test_should_have_a_html_body(self): assert '<p>We no speak americano</p>' == self.inbound.html_body() def test_should_have_a_text_body(self): assert '\nThis is awesome!\n\n' == self.inbound.text_body() def test_should_be_to_someone(self): assert '*****@*****.**' == self.inbound.to( )[0]['Email'] def test_should_have_header_mime_version(self): assert '1.0' == self.inbound.headers('MIME-Version') def test_should_have_header_received_spf(self): assert 'Pass (sender SPF authorized) identity=mailfrom; client-ip=209.85.160.180; helo=mail-gy0-f180.google.com; [email protected]; receiver=451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com' == self.inbound.headers( 'Received-SPF') def test_should_have_spam_version(self): assert 'SpamAssassin 3.3.1 (2010-03-16) onrs-ord-pm-inbound1.wildbit.com' == self.inbound.headers( 'X-Spam-Checker-Version') def test_should_have_spam_status(self): assert 'No' == self.inbound.headers('X-Spam-Status') def test_should_have_spam_score(self): assert '-0.1' == self.inbound.headers('X-Spam-Score') def test_should_have_spam_test(self): assert 'DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_PASS' == self.inbound.headers( 'X-Spam-Tests') def test_should_have_two_attachments(self): assert 2 == len(self.inbound.attachments()) def test_should_have_attachment(self): assert True == self.inbound.has_attachments() def test_attachment_should_have_content_length(self): for a in self.inbound.attachments(): assert a.content_length() is not None def test_attachment_should_have_conent_type(self): for a in self.inbound.attachments(): assert a.content_type() is not None def test_attachment_should_have_name(self): for a in self.inbound.attachments(): assert a.name() is not None def test_attachment_should_download(self): for a in self.inbound.attachments(): a.download('./tests/') assert True == os.path.exists('./tests/chart.png') assert True == os.path.exists('./tests/chart2.png') def test_attachment_to_mime(self): for a in self.inbound.attachments(): mime = a.to_mime() assert isinstance(mime, MIMEBase) assert mime.get_filename() == a.name() assert mime.get_content_type() == a.content_type() def test_attachments_as_mime(self): for a in self.inbound.attachments(as_mime=True): assert isinstance(a, MIMEBase) def test_send_date(self): assert 2012 == self.inbound.send_date().year
class PostmarkInboundTest(unittest.TestCase): def setUp(self): json_data = open('tests/fixtures/valid_http_post.json').read() self.inbound = PostmarkInbound(json=json_data) def tearDown(self): if os.path.exists('./tests/chart.png'): os.remove('./tests/chart.png') if os.path.exists('./tests/chart2.png'): os.remove('./tests/chart2.png') def test_should_have_a_subject(self): assert 'Hi There' == self.inbound.subject() def test_should_have_a_bcc(self): assert '*****@*****.**' == self.inbound.bcc() def test_should_have_a_cc(self): assert '*****@*****.**' == self.inbound.cc()[0]['Email'] def test_should_have_a_reply_to(self): assert '*****@*****.**' == self.inbound.reply_to() def test_should_have_a_mailbox_hash(self): assert 'moitoken' == self.inbound.mailbox_hash() def test_should_have_a_tag(self): assert 'yourit' == self.inbound.tag() def test_should_have_a_message_id(self): assert 'a8c1040e-db1c-4e18-ac79-bc5f64c7ce2c' == self.inbound.message_id() def test_should_be_from_someone(self): assert self.inbound.sender()['Name'] == 'Bob Bobson' and \ self.inbound.sender()['Email'] == '*****@*****.**' def test_should_have_a_html_body(self): assert '<p>We no speak americano</p>' == self.inbound.html_body() def test_should_have_a_text_body(self): assert '\nThis is awesome!\n\n' == self.inbound.text_body() def test_should_be_to_someone(self): assert '*****@*****.**' == self.inbound.to()[0]['Email'] def test_should_have_header_mime_version(self): assert '1.0' == self.inbound.headers('MIME-Version') def test_should_have_header_received_spf(self): assert 'Pass (sender SPF authorized) identity=mailfrom; client-ip=209.85.160.180; helo=mail-gy0-f180.google.com; [email protected]; receiver=451d9b70cf9364d23ff6f9d51d870251569e+ahoy@inbound.postmarkapp.com' == self.inbound.headers('Received-SPF') def test_should_have_spam_version(self): assert 'SpamAssassin 3.3.1 (2010-03-16) onrs-ord-pm-inbound1.wildbit.com' == self.inbound.headers('X-Spam-Checker-Version') def test_should_have_spam_status(self): assert 'No' == self.inbound.headers('X-Spam-Status') def test_should_have_spam_score(self): assert '-0.1' == self.inbound.headers('X-Spam-Score') def test_should_have_spam_test(self): assert 'DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_PASS' == self.inbound.headers('X-Spam-Tests') def test_should_have_two_attachments(self): assert 2 == len(self.inbound.attachments()) def test_should_have_attachment(self): assert True == self.inbound.has_attachments() def test_attachment_should_have_content_length(self): for a in self.inbound.attachments(): assert a.content_length() is not None def test_attachment_should_have_conent_type(self): for a in self.inbound.attachments(): assert a.content_type() is not None def test_attachment_should_have_name(self): for a in self.inbound.attachments(): assert a.name() is not None def test_attachment_should_download(self): for a in self.inbound.attachments(): a.download('./tests/') assert True == os.path.exists('./tests/chart.png') assert True == os.path.exists('./tests/chart2.png') def test_attachment_to_mime(self): for a in self.inbound.attachments(): mime = a.to_mime() assert isinstance(mime, MIMEBase) assert mime.get_filename() == a.name() assert mime.get_content_type() == a.content_type() def test_attachments_as_mime(self): for a in self.inbound.attachments(as_mime=True): assert isinstance(a, MIMEBase) def test_send_date(self): assert 2012 == self.inbound.send_date().year