slimta / python-slimta

Python libraries to send, receive, and queue email.
https://slimta.org/
MIT License
171 stars 43 forks source link

Add support for kwargs for MX new relay creation #135

Closed n11c closed 6 years ago

n11c commented 6 years ago

Our use case is the following: source IP / EHLO to use for outbound connection is determined for each attempt, so we needed a way to pass this on to create a source-bound relay.

I found this to be the less invasive way to handle this.

Let me know what you think.

coveralls commented 6 years ago

Coverage Status

Coverage remained the same at 92.521% when pulling 272a9756afd15b005e60b5f8ba764975e5038d28 on oasiswork:mx-new-relay-kwargs into 3a99706980832b9dd68ad915d517fa5e6172cda9 on slimta:master.

icgood commented 6 years ago

Closing this -- reopen if passing a function as ehlo_as (as described above) won't work for you.

n11c commented 6 years ago

Thanks for the feedback @icgood
I've been a bit too fast submitting this PR.

The use case is a bit more complicated.
The source IP and EHLO are determined before calling attempt() and we wanted to be able to pass them dynamically for each attempt. Existing mechanisms (socket_creator and ehlo_as function) don't quite fulfil our needs.

We've actually already sub-classed the MxSmtpRelay to override new_static_relay() and other reasons.
The idea here was to avoid having to copy-paste its attempt() just to achieve this, by adding a way to pass along kwargs to the actual static relay creator.

But after reviewing our code, it turns out that it actually requires even more changes than what I submitted (eg. kwargs on _attempt() as well to be able to get relay policies to apply).
So I agree that it may not be a good candidate for an upstream merge.
I'd be happy to get your input on this if you find the time (see example code below). But in any case, feel free to close this PR as wontmerge.

class BoundMXRelay(MxSmtpRelay):

    def new_static_relay(
            self, destination, port, source_ip=None, ehlo_as=None):
        return SourceBoundStaticSmtpRelay(
            source_ip, destination, port=port, ehlo_as=ehlo_as,
            **self._client_kwargs)

    def _attempt(self, envelope, attempts, **kwargs):
        self._run_policies(envelope)
        return self.attempt(envelope, attempts, **kwargs)

    def attempt(self, envelope, attempts, source_ip=None, ehlo_as=None):
        return super().attempt(
            envelope, attempts, source_ip=source_ip, ehlo_as=ehlo_as)