Forward on move to IMAP foder

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Forward on move to IMAP foder

Post by mschumann » 2020-07-25 09:10

I would like to automatically forward messages to a differend account if I move them into a special IMAP folder.

Background: I use a helpdesk system that polls a service account. But sometimes service related mails arrive in my personal inbox. Currently I create the case manually by copy/pasting sender address and content. If I forward the message in the client the case is of course created for me. So I thought it would be great if I could just move the message into an IMAP folder "service" and it is moved to the service account in the background and later picked up by the helpdesk system. I would like not to have to include the service mail account in my client.

User avatar
mattg
Moderator
Moderator
Posts: 20960
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Forward on move to IMAP foder

Post by mattg » 2020-07-25 09:37

Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Re: Forward on move to IMAP foder

Post by mschumann » 2020-07-25 17:29

Thanks for that hint, unfortunately my Helpdesk system does not pick up mails from the public folder.

User avatar
SorenR
Senior user
Senior user
Posts: 3684
Joined: 2006-08-21 15:38
Location: Denmark

Re: Forward on move to IMAP foder

Post by SorenR » 2020-07-25 18:46

mschumann wrote:
2020-07-25 17:29
Thanks for that hint, unfortunately my Helpdesk system does not pick up mails from the public folder.
So you don't know how to write a script ??
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Re: Forward on move to IMAP foder

Post by mschumann » 2020-07-25 18:55

I do know but I did not find a hook to react on. Something like "something arrives in a folder, forward it to that address.

I thought about writing a small delphi app that polls the folder and the forwards to my service account but I thought it could be done more easy.

User avatar
katip
Senior user
Senior user
Posts: 765
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Forward on move to IMAP foder

Post by katip » 2020-07-25 19:01

mschumann wrote:
2020-07-25 18:55
I do know but I did not find a hook to react on. Something like "something arrives in a folder, forward it to that address.
what client are you using? this can also be achived with some rules at client level i think.
Katip
--
HMS 5.7.0 x64, MariaDB 10.4.10 x64, SA 3.4.2, ClamAV 0.101.2 + SaneS

mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Re: Forward on move to IMAP foder

Post by mschumann » 2020-07-25 19:09

I prefer a server solution as I often use a very simple Mail app on my Android phone on the road.

User avatar
SorenR
Senior user
Senior user
Posts: 3684
Joined: 2006-08-21 15:38
Location: Denmark

Re: Forward on move to IMAP foder

Post by SorenR » 2020-07-25 19:50

mschumann wrote:
2020-07-25 18:55
I do know but I did not find a hook to react on. Something like "something arrives in a folder, forward it to that address.

I thought about writing a small delphi app that polls the folder and the forwards to my service account but I thought it could be done more easy.
Well... I have various hMailServer scripts running on my server - all triggered by Windows Scheduler and not events. One script runs every 30 seconds and check my IDS system and create AutoBans ... Most of my high CPU scripts run outside EventHandlers.vbs in order not to stress the core business of the server (sending and receiving email).

Anyways ... This small piece of code IS NOT VERIFIED! IT MAY WORK OR IT MAY CRASH! I will know more when I can free up my spare server and try it out.

VBS code... DO NOT RUN!

User named "HelpDesk" with password "Password" must exist and have permission to Read/Delete items in Public Folder "HelpDesk".

Code: Select all

Dim i, oApp, oDomain, oAccount, oInboxFolder, oPublicFolder, oMessages

Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate("HelpDesk", "Password")

Set oPublicFolder = oApp.Settings.PublicFolders.ItemByName("HelpDesk")
Set oMessages = oPublicFolder.Messages

If oMessages.Count > 0 Then
    Set oDomain = oApp.Domains.ItemByName("domain.tld")
    Set oAccount = oDomain.Accounts.ItemByAddress("helpdesk@domain.tld")
    Set oInboxFolder = oAccount.IMAPFolders.ItemByName("INBOX")

    For i = 0 To oMessages.Count - 1
        Rem Following two lines are commented out in case someone tries to run the script
        Rem Call oMessages.Item(i).Copy(oInboxFolder.ID)
        Rem Call oMessages.DeleteByDBID(oMessages.Item(i).ID)
    Next
End If
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Re: Forward on move to IMAP foder

Post by mschumann » 2020-07-25 20:00

Thanks a lot this is great and a good starting point, the rest should be easy. I was tied to writing code in the HMailServer events and found no suitable one.

User avatar
SorenR
Senior user
Senior user
Posts: 3684
Joined: 2006-08-21 15:38
Location: Denmark

Re: Forward on move to IMAP foder

Post by SorenR » 2020-07-26 01:02

Server still runs, nothing crashed... Code changed due to weird internal IMAPFolder handling.

Script lists messages in public folder "HelpDesk" and create a clone adressed to "helpdesk@domain.tld". When done the list of processed messages in public folder "HelpDesk" is deleted.

Set Windows Scheduler to run e.g. every 5 minutes ...

Script: helpdesk.vbs

Code: Select all

Dim i, oApp, oPublicFolder, oMessages, newMessage, strFilename, oFSO, DBID, MessageArray

Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate("Administrator", "Secret")  ' <=== CHANGE THIS!

Set oPublicFolder = oApp.Settings.PublicFolders.ItemByName("HelpDesk")  ' <=== CHANGE THIS!
Set oMessages = oPublicFolder.Messages
Set MessageArray = CreateObject("System.Collections.ArrayList")

If oMessages.Count > 0 Then
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    MessageArray.Clear
    
    For i = 0 To oMessages.Count - 1
        MessageArray.Add oMessages.Item(i).ID
        Set newMessage = CreateObject("hMailServer.Message")
        strFilename = newMessage.Filename
        oFSO.CopyFile oMessages.Item(i).Filename, strFilename, True
        newMessage.RefreshContent
        newMessage.AddRecipient "HelpDesk", "helpdesk@domain.tld"  ' <=== CHANGE THIS!
        newMessage.Save
    Next
    
    For Each DBID In MessageArray
        oMessages.DeleteByDBID(DBID)
    Next
    
End If
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Re: Forward on move to IMAP foder

Post by mschumann » 2020-07-26 11:57

I am just speechless.

I created the public folder, installed your script and it worked immediately. If we both were near by and there was no corona I would have liked to invite you for a meal of your choice!

Thanks a lot. This will very much improve my workflows!

User avatar
SorenR
Senior user
Senior user
Posts: 3684
Joined: 2006-08-21 15:38
Location: Denmark

Re: Forward on move to IMAP foder

Post by SorenR » 2020-07-26 13:43

mschumann wrote:
2020-07-26 11:57
I am just speechless.

I created the public folder, installed your script and it worked immediately. If we both were near by and there was no corona I would have liked to invite you for a meal of your choice!

Thanks a lot. This will very much improve my workflows!
No problem, When Corona is over and you find yourself in Denmark (Havnsø) you can buy me a beer :mrgreen:
SørenR.

“Those who don't know history are doomed to repeat it.”
― Edmund Burke

mschumann
New user
New user
Posts: 12
Joined: 2020-05-06 16:08

Re: Forward on move to IMAP foder

Post by mschumann » 2020-07-26 14:04

Deal!

Post Reply