def post(self, request, *args, **kwargs): logger.info("Incoming email") logger.debug("POST: %s" % request.POST) if self.verify: logger.debug("Verifying Signature") verified = self.verify_signature(request.POST.get('token', ''), request.POST.get('timestamp', ''), request.POST.get('signature', '')) if not verified: logger.debug("Signature verification failed.") return HttpResponseBadRequest("Invalid signature") try: form = self.get_form()(request.POST) email = form.save() except DroppedMailException: # This is because we got a ListID or something. It's OK. logger.debug("Quietly dropping the message") return HttpResponse("OK") # Try to link the sender to a user user = User.helper.by_email(email.sender) if user: email.user = user email.save() attachments = [] if form.cleaned_data.get('attachment-count', False): # reverse mapping in content_ids dict content_ids = dict( (attnr, cid) for cid, attnr in (email.content_ids or {}).items()) i = 1 for file in list(request.FILES.values()): attachment = self.attachment_model( attached_to=email, file=file, content_id=content_ids.get('attachment-{0!s}'.format(i), '')) attachment.save() attachments.append(attachment) i = i + 1 # See if any attached signal handlers throw an error try: email_received.send(sender=self.email_model, instance=email, attachments=attachments or []) return HttpResponse("OK") except RejectedMailException as e: logger.debug("Email was rejected: %s" % str(e)) return HttpResponse("Email not accepted", status=406)
def handle_email(self, email, attachments=None): logger.debug("handle_email: email='%s'" % email) email_received.send( sender=self.email_model, instance=email, attachments=attachments or [])