Email Snap batch or delay between sending each message

I have a pipeline which sends hundreds of messages, however my email gateway start to block the messages. We this this is because we are logging into the mail account to many times in a short period of time.

Is there a way to batch up the message or have a short delay between sending each message?

1 Like

Hey @peter,

Sending emails is based on the incoming documents. So if you have 10 documents on input, the email snap will send 10 emails. There are several options to fix this, depending on the use case.

• Use chunking. Group By will help to group the documents into an array and include that array in the contents of the email if needed.

• Use script. You can easily add a Script snap and write a simple code to wait certain amount of time on every document.

• Use async emailing. If the incoming data is not included in the content of the email, you can separate the email as a lone snap, with closed input. This will send email right from the start of the pipeline.

If none of this helps, please share some more details.

1 Like

The pipeline has several hundred html emails, each is specific to each recipient.

Ideally I would like to send in small batches with a delay between each batch.

If I group the emails into batches of 10 using group by, how do I put a delay between each batch?

1 Like

Try this Python script:

# Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
import time
class TransformScript(ScriptHook):
    def __init__(self, input, output, error, log):
        self.input = input
        self.output = output
        self.error = error
        self.log = log

    # The "execute()" method is called once when the pipeline is started
    # and allowed to process its inputs or just send data to its outputs.
    def execute(self):
        self.log.info("Executing Transform script")
        while self.input.hasNext():
            try:
                # Read the next input document, store it in a new dictionary, and write this as an output document.
                inDoc = self.input.next()
                time.sleep(20)
                outDoc = inDoc
                self.output.write(inDoc, outDoc)
            except Exception as e:
                errDoc = {
                    'error' : str(e)
                }
                self.log.error("Error in python script")
                self.error.write(errDoc)

        self.log.info("Script executed")

    # The "cleanup()" method is called after the snap has exited the execute() method
    def cleanup(self):
        self.log.info("Cleaning up")

# The Script Snap will look for a ScriptHook object in the "hook"
# variable.  The snap will then call the hook's "execute" method.
hook = TransformScript(input, output, error, log)

You can change the delay in the Time.sleep() function. Currently is 20 seconds.

2 Likes

Thanks, will test.

1 Like

Great solution, thanks @bojanvelevski

1 Like

I get an error when I run the script, Failure: Failed to connect Remote Python Executor,

Do I need to set up an account?
If so where do I get the token?

1 Like

Use the Script snap instead of Remote Python Script

1 Like

Worked like a dream.

Thanks

1 Like