zone-eu / zone-mta

📤 Modern outbound MTA cross platform and extendable server application
European Union Public License 1.2
599 stars 96 forks source link

Change the zone by a plugin #299

Closed Luskan777 closed 2 years ago

Luskan777 commented 2 years ago

Hi,

I'm trying to change the zone as per this plugin code:

app.addHook('sender:headers', (delivery, connection, next) => {
    if (!delivery.updates) {
        // make sure the updates object exists
        delivery.updates = {};
    }
    // override Zone for this message if it is deferred, no effect if message bounces or is accepted
    delivery.updates.sendingZone = 'zonex';

    next();
});

The goal is to change the zone value directly in the database with the above code, but I'm not having success, can you tell me what I'm doing wrong?

matteomattei commented 2 years ago

Try with this:

app.addHook('queue:route', (envelope, routing, next) => {
    routing.deliveryZone = 'zonex';
    return next();
});
dazoot commented 2 years ago

I think it has to be in sender:fetch state.

So try this:


app.addHook('sender:fetch', (delivery, next) => {
        if (!delivery.updates)
        {
            // make sure the updates object exists
            delivery.updates = {};
        }
        // override Zone for this message if it is deferred, no effect if message bounces or is accepted
        delivery.updates.sendingZone = 'newzone';

        next();
});
Luskan777 commented 2 years ago

Hi,

@dazoot this helped me, thanks a lot, I finally managed to complete the Fallback plugin, and I want to share it with you.

To use the plugin through "zone-mta-template" with wild-config, you will need to create the file in the path "./zone-mta-template/config/plugins/fallback.toml" in the following template:

["fallback"]
enabled=["sender"]
attemptNumber="2" 
fallbackZone="fallback"

plugin fallback - "./zone-mta-template/plugins/fallback.js"

'use strict';

// Set module title
module.exports.title = 'Fallback';

// Initialize the module
module.exports.init = (app, done) => {

        const attemptNumber = app.config.attemptNumber || "4"
        const fallbackZone = app.config.fallbackZone || "default"

app.addHook('sender:fetch', (delivery, next) => {
        if( !delivery._deferred || typeof delivery._deferred == undefined ){
          //Checks if the object "_deferred" exists
                next()
        }

        if (!delivery.updates)
        {
            // make sure the updates object exists
            delivery.updates = {};
        }

        if( delivery._deferred.count >= attemptNumber ){
           //If the value of the "deferred" number is equal to or greater than the number of retries, it will change to the fallback zone
                delivery.updates.sendingZone = fallbackZone
        next();
        }
        next()
});

        done();

}

If you are using the "zone-mta" repository and not the "zone-mta-template", the plugin configuration would look like this:

./zone-mta/config/default.js

plugins: {
        'core/fallback': {
            enabled: ['sender'],
            attemptNumber="2",
            fallbackZone="fallback"

        },

./zone-mta/plugins/core/fallback.js

'use strict';
module.exports.title = 'Fallback';
module.exports.init = (app, done) => {
        const attemptNumber = app.config.attemptNumber || "4"
        const fallbackZone = app.config.fallbackZone || "default"
app.addHook('sender:fetch', (delivery, next) => {
        if( !delivery._deferred || typeof delivery._deferred == undefined ){
          //Checks if the object "_deferred" exists
                next()
       }
        if (!delivery.updates)
        {
            // make sure the updates object exists
            delivery.updates = {};
        }
        if( delivery._deferred.count >= attemptNumber ){
           //If the value of the "deferred" number is equal to or greater than the number of retries, it will change to the fallback zone
                delivery.updates.sendingZone = fallbackZone
        next();
        }
        next()
});
        done();
}

The plugin is working fine here, and it has helped me a lot, especially in network problems, so I can change to a zone that has a relayhost configured.