Message.Copy trouble

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
ras07
Normal user
Normal user
Posts: 196
Joined: 2010-03-11 08:51

Message.Copy trouble

Post by ras07 » 2014-08-26 05:20

I'm having trouble with the Message.Copy method.

First off, I'm trying to call it in OnAcceptMessage, and I'm not positive this is legit (but I can't find any documentation that it isn't). If the message has to already exist in one IMAP folder before you copy it to another, then that's my problem, and this isn't going to work. Can somebody confirm or deny this?

Assuming that it is legit, here's the code that I can't get to work:

Code: Select all

Dim oApp, oAcct
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(hMSAuthUser, hMSAuthPass)
Set oAcct = oApp.Domains.ItemByName(myDomain).Accounts.ItemByAddress(myAccount)
oMsg.Copy(oAcct.IMAPFolders.ItemByName(myFolder).ID)
I'm pretty sure I'm identifying the right folder ID and I don't think I have any authorization issues. oAcct.IMAPFolders.ItemByName(myFolder).Name gives me the expected folder name, and oAcct.IMAPFolders.ItemByName(myFolder).Messages.Count gives me the expected number of messages that are in that folder. So I think I'm on the right track.

Am I getting IDs mixed up? The docs for Message.Copy are pretty sparse - I'm not positive that iDestinationFolderID is the same database identifier as IMAPFolder.ID (but if it isn't, then I don't know what it is).

Any ideas appreciated

Thanks

ras

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

Re: Message.Copy trouble

Post by mattg » 2014-08-26 06:25

OnAcceptMessage is before it is sent to the account

Try OnDeliverMessage


is oMsg what is in the brackets of the sub??
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
ras07
Normal user
Normal user
Posts: 196
Joined: 2010-03-11 08:51

Re: Message.Copy trouble

Post by ras07 » 2014-08-26 07:01

It's not working on OnDeliverMessage either.
mattg wrote:is oMsg what is in the brackets of the sub??
Sorry, oMsg should be oMessage (the parameter for, in this case, OnDeliverMessage).

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

Re: Message.Copy trouble

Post by mattg » 2014-08-26 07:47

Just thinking that this is a permissions issue.

Authenticate as your account and see it that works, or even try authenticating with 'Administrator' and the relevant password

Oh, and are there any errors logged?
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
ras07
Normal user
Normal user
Posts: 196
Joined: 2010-03-11 08:51

Re: Message.Copy trouble

Post by ras07 » 2014-08-26 08:16

I don't think it's permissions - I'm authenticating with the Administrator account.

For clarity I restructured it as follows - no errors are logged:

Code: Select all

Sub OnDeliverMessage(oMessage)
	Dim oApp, oDom, oAcct, oFldr
	Set oApp = CreateObject("hMailServer.Application")
	Call oApp.Authenticate(hMSAuthUser, hMSAuthPass)
	Set oDom = oApp.Domains.ItemByName(myDomain)
		LogStr("Domain ID: " & oDom.ID)	
	Set oAcct = oDom.Accounts.ItemByAddress(myAcct)
		LogStr("Account ID: " & oAcct.ID)
		LogStr("Folder count: " & oAcct.IMAPFolders.Count)
	Set oFldr = oAcct.IMAPFolders.ItemByName(myFolder)
		LogStr("Folder ID: " & oFldr.ID)
		LogStr("Folder name: " & oFldr.Name)
		LogStr("Folder count: " & oFldr.Messages.Count)
	oMessage.Copy(oFldr.ID)
		LogStr("Message UID: " & oMessage.UID)
'               LogStr("Message ID: " & oMessage.ID)    ' This will throw an error
		LogStr("DONE")
'	oFldr.Save      ' I've tried it with and without this; it doesn't seem to matter
End Sub
LogStr is just a simple subroutine to write a string to a file. The output of the file is as follows:

Code: Select all

Domain ID: 2
Account ID: 23
Folder count: 5
Folder ID: 299
Folder name: tst
Folder count: 4
Message UID: 0
All this is as expected, except that I don't understand why the message UID is zero. Also, trying to access oMessage.ID results in a "Variable uses an Automation type not supported in VBScript" error.

Are you sure the message exists in an IMAP folder when OnDeliverMessage is called? (The way I read the docs it seems like it certainly should be, but the oMessage.UID and oMessage.ID results make me wonder.)

Thanks for looking at this

ras

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

Re: Message.Copy trouble

Post by mattg » 2014-08-26 08:43

I do something similar with a set of rules

1. Global rule
If from contains "@domain.com" AND
recipient list Not contains sent@domain.com
THEN
Forward to sent@domain.com


2. account level rule for Sent
If message size > 0 THEN
move to IMAP folder (public folder)

It requires setting up public folders and permissions for them, but then say the entire reception team can see all mail sent by anyone from the domain

I'll have a closer look at your script later when I get a few minutes...
ras07 wrote:Are you sure the message exists in an IMAP folder when OnDeliverMessage is called?
No not sure, but that's what I'll check
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
mattg
Moderator
Moderator
Posts: 20134
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Message.Copy trouble

Post by mattg » 2014-08-26 08:47

ALSO, you could to run function from a global rule
That should be almost last thing in the process. Only account rules after global rules I think.
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
SorenR
Senior user
Senior user
Posts: 3190
Joined: 2006-08-21 15:38
Location: Denmark

Re: Message.Copy trouble

Post by SorenR » 2014-08-26 13:33

Well....

Code: Select all

3280	"2014-08-26 13:24:40.021"	"OnAcceptMessage(oClient, oMessage) - oMessage.ID = 0"
1188	"2014-08-26 13:24:40.083"	"OnDeliveryStart(oMessage) - oMessage.ID = 827748"
1188	"2014-08-26 13:24:40.115"	"OnDeliverMessage(oMessage) - oMessage.ID = 827748"

Code: Select all

   Sub OnAcceptMessage(oClient, oMessage)

      On Error Resume Next
      EventLog.Write ("OnAcceptMessage(oClient, oMessage) - oMessage.ID = " & CStr(oMessage.ID))
      On Error GoTo 0

   End Sub

   Sub OnDeliveryStart(oMessage)
   
      On Error Resume Next
      EventLog.Write ("OnDeliveryStart(oMessage) - oMessage.ID = " & CStr(oMessage.ID))
      On Error GoTo 0

   End Sub

   Sub OnDeliverMessage(oMessage)

      On Error Resume Next
      EventLog.Write ("OnDeliverMessage(oMessage) - oMessage.ID = " & CStr(oMessage.ID))
      On Error GoTo 0

   End Sub
Looking into the database, table 'hm_messages' I can find the oMessage.ID so it keeps the .ID when delivered to the user.

I found this piece of code by Martin a while back, it may give you some inspiration on what to do...

Code: Select all

   Dim obApp
   Set obApp = CreateObject("hMailServer.Application")

   ' Authenticate. Without doing this, we won't have permission
   ' to change any server settings or add any objects to the
   ' installation.   
   Call obApp.Authenticate("Administrator", "secret")
   
   ' Locate the domain we want to add the account to
   Dim obDomain
   Set obDomain = obApp.Domains.ItemByName("example.com")
   
   Dim obAccount
   Set obAccount = obDomain.Accounts.ItemByAddress("test@example.com")
   
   Dim obInboxFolder 
   Set obInboxFolder = obAccount.IMAPFolders.ItemByName("Inbox")

   ' Assume that the backup folder already exists. This may
   ' not be the case.      
   Dim obBackupFolder 
   Set obBackupFolder = obAccount.IMAPFolders.ItemByName("Backup")
   
   ' Copy all messages in Inbox to the backup folder
   Dim obMessages
   Set obMessages = obInboxFolder.Messages
   
   For i = 0 to obMessages.Count - 1
      Call obMessages.Item(i).Copy(obBackupFolder.ID)
   Next 
SørenR.

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

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

Re: Message.Copy trouble

Post by SorenR » 2014-08-26 16:02

Hmm... I made it come up with an error..

Code: Select all

"DEBUG"	1188	"2014-08-26 15:56:37.708"	"Applying rule CopyMessage"
"DEBUG"	1188	"2014-08-26 15:56:37.708"	"Performing rule action"
"DEBUG"	1188	"2014-08-26 15:56:37.708"	"ScriptServer::FireEvent-Unknown"
"ERROR"	1188	"2014-08-26 15:56:37.724"	"Script Error: Source: hMailServer COM library - Error: 800403E9 - Description: Unable to copy message. - Line: 264 Column: 6 - Code: (null)"
"DEBUG"	1188	"2014-08-26 15:56:37.724"	"ScriptServer:~FireEvent"
None of the triggers gave any errors like OP also concluded... Global Rule -> Run function gave no error, however on Account Rule -> Run function I managed to get an error...

Perhaps we are not supposed use ".Copy" in that way :wink:
SørenR.

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

Post Reply