def apply(self, envelope): if not envelope.recipients: raise QueueError('Missing recipient') recipient = envelope.recipients[0] if extract_domain(recipient) != settings.RETURNPATH_DOMAIN: raise QueueError('Domain not valid') message = email.message_from_bytes(envelope.flatten()[0] + envelope.flatten()[1]) # Check recipient prefix prefix_allowed = False for prefix in ['return-', 'unsubscribe-', 'abuse']: if recipient.startswith(prefix): prefix_allowed = True break if not prefix_allowed: raise QueueError('Prefix not allowed') # Find handler handler_found = False for handler in [DSNHandler, ARFHandler, UnsubscribeHandler]: if handler.can_handle(message): log.info('Handler {} can handle'.format(handler.__name__)) handler = handler(envelope, message) handler_found = True break if not handler_found: raise QueueError('No handler found') handler.apply()
def test_queueerror(self): self.queue.enqueue(IsA(Envelope)).AndReturn([(Envelope(), QueueError()) ]) self.start_response.__call__('500 Internal Server Error', IsA(list)) self.mox.ReplayAll() w = WsgiEdge(self.queue) self.assertEqual([], w(self.environ, self.start_response))
def _initiate_attempt(self, envelope, attempts=0, wait=None): attempt = self.attempt_task.s(envelope, attempts) if wait: attempt.set(countdown=wait) try: return attempt.apply_async().id except Exception as exc: return QueueError(exc)
def test_have_data_queueerror(self): env = Envelope() handoff = self.mox.CreateMockAnything() handoff(env).AndReturn([(env, QueueError())]) self.mox.ReplayAll() h = SmtpSession(('127.0.0.1', 0), None, handoff) h.envelope = env reply = Reply('250') h.HAVE_DATA(reply, b'', None) self.assertEqual('451', reply.code) self.assertEqual('4.3.0 Error queuing message', reply.message)
def apply(self, envelope): sender_email = parseaddr(envelope.headers.get('From'))[1] try: validate_email(sender_email) except ValidationError: sender_email = None if not sender_email: error = QueueError() error.reply = Reply( code='554', message=( '5.7.1 <{}>: Relay access denied: ' 'Sending domain invalid').format(envelope.recipients[0])) raise error organization_domain = SendingDomain.objects.filter( organization=envelope.organization) domain = organization_domain.get_from_email_addr( sender_email, must_raise=False) if not domain: error = QueueError() error.reply = Reply( code='554', message=( '5.7.1 <{}>: Relay access denied: Sending ' 'domain for <{}> is not properly configured').format( envelope.recipients[0], sender_email)) raise error envelope.sending_domain = domain