slimta.policy

Package containing useful policies, which can be configured to run before queuing or before relaying the message with the slimta.queue.Queue.add_policy() and slimta.relay.Relay.add_policy(), respectively.

If a policy is applied before queuing, it is executed only once and any changes it makes to the Envelope will be stored persistently. This is especially useful for tasks such as header and content modification, since these may be more expensive operations and should only run once.

If a policy is applied before relaying, it is executed before each delivery attempt and no resulting changes will be persisted to storage. This is useful for policies that have to do with delivery, such as forwarding.

exception slimta.policy.PolicyError([*args[, **kwargs]])

Bases: slimta.core.SlimtaError

Base exception for all custom slimta.policy errors.

class slimta.policy.QueuePolicy([*args[, **kwargs]])

Bases: object

Base class for queue policies. These are run before a message is persistently queued and may overwrite the original Envelope with one or many new Envelope objects.

class MyQueuePolicy(QueuePolicy):
    def apply(self, env):
        env['X-When-Queued'] = str(time.time())

my_queue.add_policy(MyQueuePolicy())
apply(envelope)

QueuePolicy sub-classes must override this method, which will be called by the Queue before storage.

Parameters:envelope – The Envelope object the policy execution should apply any changes to. This envelope object may be modified, though if new envelopes are returned this object is discarded.
Returns:Optionally return or generate an iterable of Envelope objects to replace the given envelope going forward. Returning None or an empty list will keep using envelope.
class slimta.policy.RelayPolicy([*args[, **kwargs]])

Bases: object

Base class for relay policies. These are run immediately before a relay attempt is made.

class MyRelayPolicy(RelayPolicy):
    def apply(self, env):
        env['X-When-Delivered'] = str(time.time())

my_relay.add_policy(MyRelayPolicy())
apply(envelope)

RelayPolicy sub-classes must override this method, which will be called by the Relay before delivery. Unlike QueuePolicy.apply(), the return value of this method is discarded.

Much like attempt(), these methods may raise PermanentRelayError or TransientRelayError to mark the relay attempt as failed for the entire message.

Modifications to the envelope will be passed on to the Relay. However, it is unlikely these modifications will be persisted by the QueueStorage implementation.

Parameters:envelope – The Envelope object the policy execution should apply any changes to.
Raises:PermanentRelayError, TransientRelayError

Sub-Modules: