(PoC) Batch sending with delay (PoC)

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
User avatar
SorenR
Senior user
Senior user
Posts: 6308
Joined: 2006-08-21 15:38
Location: Denmark

(PoC) Batch sending with delay (PoC)

Post by SorenR » 2020-08-14 18:05

1: Create account "mailman@mydomain.tld"
2: Create account rule for above; If Message size > 0 Run function mailman
3: Create folder on server (hMailServer) to hold XML files
4: Modify parameters:

Code: Select all

    Const XMLDIR      = "C:\hMailServer\Events"
    Const From        = "Carrot top"
    Const FromAddress = "agent.orange@acme.inc"
5: Copy sample XML file to "C:\hMailServer\Events\mailman.xml"
6: Compose message and send to "mailman@mydomain.tld" with subject: "mailman|10|30|Have a nice day with Looney Tunes"

mailman = mailman.xml
10 = 10 recipients in a batch
30 = 30 seconds delay between batches
"Have a nice day with Looney Tunes" is the new email subject

Recipients are added as BCC thus not visiable. The "official" recipient of the email is the "mailman@mydomain.tld" account.

NOTE: THERE IS NO ERRORHANDLING
This is Proof of Concept code, it works but can probably not be used in production until further errorhandling has been added.


This code comes as is and is intended as a framework to be built on!

Sample XML file with recipients

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Address>Barnyard Dawg, barnyard.dawg@acme.inc</Address>
  <Address>Bosko, bosko@acme.inc</Address>
  <Address>Bugs Bunny, bugs.bunny@acme.inc</Address>
  <Address>Daffy Duck, daffy.duck@acme.inc</Address>
  <Address>Elmer Fudd, elmer.fudd@acme.inc</Address>
  <Address>Foghorn Leghorn, foghorn.leghorn@acme.inc</Address>
  <Address>Granny, granny@acme.inc</Address>
  <Address>Hector the Bulldog, hector.the.bulldog@acme.inc</Address>
  <Address>Honey Bunny, honey.bunny@acme.inc</Address>
  <Address>Lola Bunny, lola.bunny@acme.inc</Address>
  <Address>Marvin The Martian, marvin.the.martian@acme.inc</Address>
  <Address>Penelope Pussycat, penelope.pussycat@acme.inc</Address>
  <Address>Pepe Le Pew, pepe.le.pew@acme.inc</Address>
  <Address>Porky Pig, porky.pig@acme.inc</Address>
  <Address>Road Runner, road.runner@acme.inc</Address>
  <Address>Speedy Gonzales, speedy.gonzales@acme.inc</Address>
  <Address>Sylvester, sylvester@acme.inc</Address>
  <Address>The Tasmanian Devil, taz@acme.inc</Address>
  <Address>Tweety Bird, tweety@acme.inc</Address>
  <Address>Wile E. Coyote, wile.e.coyote@acme.inc</Address>
  <Address>Witch Hazel, witch.hazel@acme.inc</Address>
  <Address>Yosemite Sam, yosemite.sam@acme.inc</Address>
</Root>
Code that goes into eventhandlers.vbs

Code: Select all

Sub MailMan(oMessage)
    '   Email subject: "<list>|<batch>|<delay>|<subject>"
    '
    Const XMLDIR      = "C:\hMailServer\Events"
    Const MyNode      = "//Address"
    Const From        = "Carrot top"
    Const FromAddress = "agent.orange@acme.inc"

    Dim a, b, i, j, XMLFile, strFilename, strSubject, iBatch, iDelay, oMatch, oMatches, oClone

    a = Split(oMessage.Subject, "|")
    XMLFile  = a(0) & ".xml"
    iBatch = CInt(a(1))
    iDelay = CInt(a(2))
    strSubject = Trim(CStr(a(3)))

    Dim oXML : Set oXML = CreateObject("MSXML2.DOMDocument")
    If oXML.Load(XMLDIR & "\" & XMLFile) Then
        Set oMatches = oXML.selectNodes(MyNode)
        j = 0
        For Each oMatch In oMatches

            If (j Mod iBatch) = 0 Or j = 0 Then
                Set oClone = CreateObject("hMailServer.Message")
                strFilename = oClone.Filename
                With CreateObject("Scripting.FileSystemObject")
                    .CopyFile oMessage.Filename, strFilename, True
                End With
                oClone.RefreshContent
            End If

            j = j + 1
            b = Split(oMatch.text,",")
            oClone.AddRecipient Chr(34) & Trim(b(0)) & Chr(34), Trim(b(1))

            If (j Mod iBatch) = 0 Or j = oMatches.length Then
                oClone.From = From
                oClone.FromAddress = FromAddress
                oClone.HeaderValue("To") = oMessage.HeaderValue("To")
                oClone.HeaderValue("Subject") = strSubject
                oClone.Save
                With CreateObject("WScript.Shell")
                    .Run "powershell Start-Sleep -Milliseconds " & Int(iDelay * 1000), 0, True
                End With
            End If

        Next
        Set oClone = Nothing
        Set oMatch = Nothing
        Set oMatches = Nothing
    Else
        EventLog.Write( "Your XML Document " & XMLFile & " failed to load due the following error." & vbCrLf & _
        "Error #: " & oXML.ParseError.errorCode & ": " & oXML.ParseError.reason & _
        "Line #: " & oXML.ParseError.line & vbCrLf & _
        "Line Position: " & oXML.ParseError.linePos & vbCrLf & _
        "Position In File: " & oXML.ParseError.filePos & vbCrLf & _
        "Source Text: " & oXML.ParseError.srcText & vbCrLf & _
        "Document URL: " & oXML.ParseError.url )
    End If
    Set oXML = Nothing
End Sub
SørenR.

Woke is Marxism advancing through Maoist cultural revolution.

Post Reply