Copy oMessage to IMAP Folder

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
katip
Senior user
Senior user
Posts: 683
Joined: 2006-12-22 07:58
Location: Istanbul

Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 09:00

Hi,
account rule calls a function where it tries to copy oMessage to an IMAP folder of another account.

Code: Select all

Dim obDomain, obAccount, obFwdFolder
Set obDomain = obApp.Domains.ItemByName(strDomain)
Set obAccount = obDomain.Accounts.ItemByAddress(arrFwdAdd) '<-  copy message to this account's inbox
Set obFwdFolder = obAccount.IMAPFolders.ItemByName("Inbox")
oMessage.Copy(obFwdFolder.ID)
oMessage.Copy(obFwdFolder.ID) fails with "Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Unable to copy message."

app authenticate is with administrator account. so, no permission issue i think.
i found this : https://www.hmailserver.com/forum/viewt ... 52#p165620
@Soren, do you still think such copying is not possible? or is my case something irrelevant?
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

User avatar
jimimaseye
Moderator
Moderator
Posts: 8125
Joined: 2011-09-08 17:48

Re: Copy oMessage to IMAP Folder

Post by jimimaseye » 2019-04-03 09:06

You cannot do it.

It is stated in the documentation that you cannot copy emails between accounts - only folders within the same account (except for public accounts).
HMS 5.6.6 B2383 on Win Server 2008 R2 Foundation, + 5.6.7-B2415 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 09:16

thanks, i haven't seen it.
any suggestion how to copy this message to another acccount (like forward in rules)?
i already do this by creating new message from current one and addRecipient etc..
but just looking for a more complete solution where message is copied "as is".
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Copy oMessage to IMAP Folder

Post by mattg » 2019-04-03 10:09

You can probably just 'add receipient' and save the message early in the journey through your server, somewhere like 'OnAcceptMessage'
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 10:49

or better i mark it with a header such as "X-HMS-ToBeForwarded = Yes"
put a subsequent rule in account which reads this and if Yes forwards to x,y,z
let me try. thanks.
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 11:45

katip wrote:
2019-04-03 10:49
or better i mark it with a header such as "X-HMS-ToBeForwarded = Yes"
put a subsequent rule in account which reads this and if Yes forwards to x,y,z
no, silly me, then i don't know which of x,y,z or all 3 should get a copy (function knows it!!).
setting X-HMS-ToBeForwarded = x,y,z may do the job but then i must write 3 rules for each of them (if X-HMS-ToBeForwarded contains x then forward to x etc...)
not feasible to maintain if x,y,z... change frequently (such as in our office).
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-03 13:56

katip wrote:
2019-04-03 09:00
Hi,
account rule calls a function where it tries to copy oMessage to an IMAP folder of another account.

Code: Select all

Dim obDomain, obAccount, obFwdFolder
Set obDomain = obApp.Domains.ItemByName(strDomain)
Set obAccount = obDomain.Accounts.ItemByAddress(arrFwdAdd) '<-  copy message to this account's inbox
Set obFwdFolder = obAccount.IMAPFolders.ItemByName("Inbox")
oMessage.Copy(obFwdFolder.ID)
oMessage.Copy(obFwdFolder.ID) fails with "Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Unable to copy message."

app authenticate is with administrator account. so, no permission issue i think.
i found this : https://www.hmailserver.com/forum/viewt ... 52#p165620
@Soren, do you still think such copying is not possible? or is my case something irrelevant?
Alternative would include something like this... IIRC you need to tamper with the header "Message-ID" for hMailServer to accept and index the message...

NB: Code is untested but passes Syntax Check :wink:

Code: Select all

Dim oApp : Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate("ADMIN", "PASSWORD")

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

strFile = "C:\SomeDirectory\" & Mid(oMessage.filename, InStr(oMessage.filename, "{"))
Call oFSO.CopyFile(oMessage.filename, strFile, True)

Dim oDomain : Set oDomain = oApp.Domains.ItemByName("acme.inc")
Dim oAccount : Set oAccount = oDomain.Accounts.ItemByAddress("wile.e.coyote@acme.inc")

Call oApp.Utilities.ImportMessageFromFileToIMAPFolder(strFile, oAccount.ID, "Inbox.Spam")

Set oFSO = Nothing
Set oApp = Nothing
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 15:50

SorenR wrote:
2019-04-03 13:56
IIRC you need to tamper with the header "Message-ID" for hMailServer to accept and index the message...
thanks for clever suggestion. yes exactly, that Message-ID thing.

i adjusted your code and tried.
all runs perfectly. file written to temp dir with correct full name. no any errors at all. but action below silently fails somehow. no message arrives.

Code: Select all

Call oApp.Utilities.ImportMessageFromFileToIMAPFolder(strFile, oAccount.ID, "Inbox")
even oEventLog.Write fires my own "...successfully forwarded... blah.." info from near end function.
all is fine but message don't show up in "Inbox". i checked it on file system too. nowhere except that copy in temp dir.
that would be a perfect solution for me, if it'd work.

//EDIT: Call oApp.Utilities.ImportMessageFromFile(strFile, oAccount.ID) don't work either, again silently.
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-03 16:10

katip wrote:
2019-04-03 15:50
SorenR wrote:
2019-04-03 13:56
IIRC you need to tamper with the header "Message-ID" for hMailServer to accept and index the message...
thanks for clever suggestion. yes exactly, that Message-ID thing.

i adjusted your code and tried.
all runs perfectly. file written to temp dir with correct full name. no any errors at all. but action below silently fails somehow. no message arrives.

Code: Select all

Call oApp.Utilities.ImportMessageFromFileToIMAPFolder(strFile, oAccount.ID, "Inbox")
even oEventLog.Write fires my own "...successfully forwarded... blah.." info from near end function.
all is fine but message don't show up in "Inbox". i checked it on file system too. nowhere except that copy in temp dir.
that would be a perfect solution for me, if it'd work.

//EDIT: Call oApp.Utilities.ImportMessageFromFile(strFile, oAccount.ID) don't work either, again silently.
I have it test on my system right now. Header "Message-ID" must be altered in the file outside hMailServer database... I remember we had some some fun with it a couple of years ago but it was quite tricky IIRC.
I have not given up on this yet :mrgreen:
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-03 16:36

I knew I had it working at some point...

https://www.hmailserver.com/forum/viewt ... 15#p166615

Will continue experimenting :mrgreen:
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-03 17:03

Partial success ...

It appears to work... The header "Message-ID" is changed and file is read (and removed) from Data directory and the call to oApp.Utilities.ImportMessageFromFileToIMAPFolder() returns "True" ... Only ... I'm having trouble finding the imported message :roll:

I have switched off Performance/Indexing and cleared it since this is the only table where I have found "Message-ID", just to be on the safe side.

Code: Select all

Sub OnDeliveryStart(oMessage)

   '
   ' Give permission to access all hMailServer settings.
   '
   Dim oApp : Set oApp = CreateObject("hMailServer.Application")
   Call oApp.Authenticate(ADMIN, PASSWORD)
   '
   ' Copy message file to external directory.
   '
   Dim strFile, oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
   strFile = "C:\hMailServer\Data\" & Mid(oMessage.filename, InStr(oMessage.filename, "{"))
   Call oFSO.CopyFile(oMessage.filename, strFile, True)
   '
   ' Change "Message-ID"
   '
   Dim strOldText, strNewText
   With oFSO.OpenTextFile(strFile, 1)
      strOldText = .ReadAll
   End With
   strNewText = Replace(strOldText, "Message-ID: <", "Message-ID: <copy.")
   With oFSO.OpenTextFile(strFile, 2)
      .WriteLine strNewText
   End With
   '
   ' Import message.
   '
   Dim rc, oDomain : Set oDomain = oApp.Domains.ItemByName("acme.inc")
   Dim oAccount : Set oAccount = oDomain.Accounts.ItemByAddress("wile.e.coyote@acme.inc")
   rc = oApp.Utilities.ImportMessageFromFileToIMAPFolder(strFile, oAccount.ID, "INBOX")
   EventLog.Write( "rc = " & rc ) ' <-- Will return "True" if successful
   '
   ' Clean up.
   '
   Set oFSO = Nothing
   Set oApp = Nothing

End Sub
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 18:55

SorenR wrote:
2019-04-03 17:03
Partial success ...

It appears to work... The header "Message-ID" is changed and file is read (and removed) from Data directory and the call to oApp.Utilities.ImportMessageFromFileToIMAPFolder() returns "True" ... Only ... I'm having trouble finding the imported message :roll:
yes, me too :?

1.
docus say :
ImportMessageFromFile(string sFilename, long iAccountID)
Imports a message from a file on disk for delivery or to a specific account.
i understand that import can be done from any location on disk, not necessarily from HMS\data directory.
anyway i tried also data directory. nothing changes.

2.
there is definitely no error. also debug + app log shows all clean.

3.
frankly spoken i don't understand this Message-ID issue. this is nowhere in any table on HMS database. or am i blind. you mean you see this somewhere in source code? it should stay in cache then? anyway i don't think this affects delivery, we have seen this on the other thread. seperate deliveries do coexist with identical Message-ID's.
ok, it's not a problem to rewrite it but i'm really confused now.
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-03 19:27

katip wrote:
2019-04-03 18:55
SorenR wrote:
2019-04-03 17:03
Partial success ...

It appears to work... The header "Message-ID" is changed and file is read (and removed) from Data directory and the call to oApp.Utilities.ImportMessageFromFileToIMAPFolder() returns "True" ... Only ... I'm having trouble finding the imported message :roll:
yes, me too :?

1.
docus say :
ImportMessageFromFile(string sFilename, long iAccountID)
Imports a message from a file on disk for delivery or to a specific account.
i understand that import can be done from any location on disk, not necessarily from HMS\data directory.
anyway i tried also data directory. nothing changes.

2.
there is definitely no error. also debug + app log shows all clean.

3.
frankly spoken i don't understand this Message-ID issue. this is nowhere in any table on HMS database. or am i blind. you mean you see this somewhere in source code? it should stay in cache then? anyway i don't think this affects delivery, we have seen this on the other thread. seperate deliveries do coexist with identical Message-ID's.
ok, it's not a problem to rewrite it but i'm really confused now.
2: The function returns "True" if successful and "False" if fail.

3: Message-ID is indexed if "Performance" settings are used.

I'm looking at the source while doing this... There are about 15 ways this can fail and there are NO (!) error messages in the source - it only says "return false" :evil:

What is confusing is that it seems like it can also be used to "update" (?!?) existing message files... But why?

The internal function is shared between "ImportMessageFromFile" and "ImportMessageFromFileToIMAPFolder" and the first I have used before with no issues.

I think I may have to modify the source to include error messages - if I can figure out how :mrgreen:
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-03 19:46

Time for dinner break.

I'm going to go through the source so I may have some news tomorrow.
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-03 19:57

Thank you, have a nice evening.
we see tomorrow..
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

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

Re: Copy oMessage to IMAP Folder

Post by SorenR » 2019-04-04 02:09

Well... Gave up... Started to poke around my old posts here and found this...

You can call it from e.g. OnAcceptMessage but I made an Account rule to call function "CopyMail".
It will clone the message, change Message-ID, add recipient and put it back in the queue.

Optionally you can add a custom header and have a rule check for it and "Move to Folder" ;-)

Code: Select all

Sub CopyMail(oMessage)
   Dim sFile, sMessageFilename, sNewMessageFilename, sMessageID
   With CreateObject("hMailServer.Message")
      sNewMessageFilename = .Filename
      sMessageId = oMessage.HeaderValue("Message-ID")
      sMessageFilename = oMessage.Filename
      With CreateObject("Scripting.FileSystemObject")
         .CopyFile sMessageFilename, sNewMessageFilename, True
      End With
      .RefreshContent
'*    .From = "John Doe <info@mydomain.tld>"
'*    .FromAddress = "info@mydomain.tld"
      .AddRecipient "Wile E. Coyote", "wile.e.coyote@acme.inc" ' <--- NEW RECIPIENT HERE !!
      sMessageID = "<1." & Mid(sMessageID,2) & ">"
      .HeaderValue("Message-ID") = sMessageID
      .Save
   End With
End Sub
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

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

Re: Copy oMessage to IMAP Folder

Post by katip » 2019-04-04 09:06

SorenR wrote:
2019-04-04 02:09
Well... Gave up... Started to poke around my old posts here and found this...

You can call it from e.g. OnAcceptMessage but I made an Account rule to call function "CopyMail".
It will clone the message, change Message-ID, add recipient and put it back in the queue.

Optionally you can add a custom header and have a rule check for it and "Move to Folder" ;-)

Code: Select all

Sub CopyMail(oMessage)
   Dim sFile, sMessageFilename, sNewMessageFilename, sMessageID
   With CreateObject("hMailServer.Message")
      sNewMessageFilename = .Filename
      sMessageId = oMessage.HeaderValue("Message-ID")
      sMessageFilename = oMessage.Filename
      With CreateObject("Scripting.FileSystemObject")
         .CopyFile sMessageFilename, sNewMessageFilename, True
      End With
      .RefreshContent
'*    .From = "John Doe <info@mydomain.tld>"
'*    .FromAddress = "info@mydomain.tld"
      .AddRecipient "Wile E. Coyote", "wile.e.coyote@acme.inc" ' <--- NEW RECIPIENT HERE !!
      sMessageID = "<1." & Mid(sMessageID,2) & ">"
      .HeaderValue("Message-ID") = sMessageID
      .Save
   End With
End Sub
very nice!
lesson for me:
1. yes, in fact we need a new file name. i fell into trap by ignoring the fact that file names are unique and tried to import from same file name. HMS likely discarded it silently.
2. Message-ID is crucial, but how, i didn't understand. i tried both with original and &1.&original. the one with original failed again. so, Message-Id must be changed.
3. as Message-ID changes, my query on external table must check for both (messageid = strMessageId OR messageid =sMessageID) . otherwise dilemma :lol: but no problem, done!

thank you again! have a nice day..
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

Post Reply