Script to write message to file

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
mikedibella
Senior user
Senior user
Posts: 390
Joined: 2016-12-08 02:21

Script to write message to file

Post by mikedibella » 2020-12-08 23:00

I have a need to create a drop mailbox. Messages received at this mailbox would be written to a file for further processing. I used to do this with an IIS SMTP "local" domain. Messages sent to the local domain would just get written in RFC 822 format into the mailroot\drop directory.

Now I'd like to emulate that functionality with an HMS script as a rule action. I see I can code it by accessing the many properties exposed by the Message class, but I rather just copy the .eml file underlying the Message instance. I see these filenames are exposed in the Queue object, but I don't see a way to access the filename via Message properties or methods.

Any ideas?

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

Re: Script to write message to file

Post by mattg » 2020-12-08 23:31

filename is a Message property
https://www.hmailserver.com/documentati ... ct_message

Depends on when you call it, or how you find your message

You could use this property during OnDeliveryStart or OnAcceptMessage if you wanted to catch current messages live

You could also iterate through the domains > Accounts > IMAP Folders > Messages to get your message
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
jimimaseye
Moderator
Moderator
Posts: 8917
Joined: 2011-09-08 17:48

Re: Script to write message to file

Post by jimimaseye » 2020-12-08 23:36

Take a look at this. Palinka did some good work and maybe there are some useful bits in there to help you. https://www.hmailserver.com/forum/viewtopic.php?t=33062
5.7 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

mikedibella
Senior user
Senior user
Posts: 390
Joined: 2016-12-08 02:21

Re: Script to write message to file

Post by mikedibella » 2020-12-08 23:43

mattg wrote:
2020-12-08 23:31
filename is a Message property
https://www.hmailserver.com/documentati ... ct_message

Depends on when you call it, or how you find your message

You could use this property during OnDeliveryStart or OnAcceptMessage if you wanted to catch current messages live

You could also iterate through the domains > Accounts > IMAP Folders > Messages to get your message
Excellent. I don't know how I missed that in the docs. I do see it is there know.

I plan to keep this simple. Regular mailbox with two rules that match all messages. First one has action to fire script to write to file, second deletes the email.

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

Re: Script to write message to file

Post by SorenR » 2020-12-09 01:33

mikedibella wrote:
2020-12-08 23:00
I have a need to create a drop mailbox. Messages received at this mailbox would be written to a file for further processing. I used to do this with an IIS SMTP "local" domain. Messages sent to the local domain would just get written in RFC 822 format into the mailroot\drop directory.

Now I'd like to emulate that functionality with an HMS script as a rule action. I see I can code it by accessing the many properties exposed by the Message class, but I rather just copy the .eml file underlying the Message instance. I see these filenames are exposed in the Queue object, but I don't see a way to access the filename via Message properties or methods.

Any ideas?
Add this to Eventhandlers.vbs:

Code: Select all

Sub MailDrop(oMessage)
    With CreateObject("Scripting.FileSystemObject")
        .CopyFile oMessage.Filename, "C:\mailroot\drop\", True
    End With
End Sub
Create a "MailDrop" account with a rule "MailDrop" ... If MessageSize > 1 Then ... 1) Run function "maildrop" 2) delete email.

Don't forget to create the directory "C:\mailroot\drop"

Monitoring the folder is another matter ... A suggestion is here...

https://stackoverflow.com/questions/365 ... o-a-folder
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

mikedibella
Senior user
Senior user
Posts: 390
Joined: 2016-12-08 02:21

Re: Script to write message to file

Post by mikedibella » 2020-12-09 04:34

Thanks to all who responded. I think I'm all set now.

palinka
Senior user
Senior user
Posts: 2475
Joined: 2017-09-12 17:57

Re: Script to write message to file

Post by palinka » 2020-12-09 14:40

I was toying around with this and found out that messages in a folder apparently always iterate in date order from oldest to newest. So if you have to call up the message from outside eventhandlers, you can always just pull the last iteration. Skip all the others.

Message = Folder.Messages.Item(Folder.Messages.Count - 1)
FileName = Message.FileName

Disclaimer - I do not know FOR SURE that the iteration is ALWAYS in date order, but it does appear to be that way based on the few folders I was messing around with.

Post Reply