Script Conditionally Save Attachement

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
jtellier
New user
New user
Posts: 4
Joined: 2020-05-28 23:25

Script Conditionally Save Attachement

Post by jtellier » 2020-05-28 23:27

Has anyone done a script that will save attchements to a location for messages sent to someone and from someone?

Like

if (from=="test@test.com" && to=="to@test.com") SaveFiles("C:\Here")

Sorry I am a NEWB in VBScript and just wanted to see if anyone else has done this.

Thanks!
Jeremy

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

Re: Script Conditionally Save Attachement

Post by palinka » 2020-05-29 04:37

Sure.

Rule:
Use AND
Criteria: from contains from@address.com
Criteria: to contains to@address.com
Action: Run Function SaveAttachment

Code: Select all

Sub SaveAttachment(oMessage)
	Dim i, strDir, strFile
	strDir = "C:\path\to\attachment_folder\"
	For i = 0 To oMessage.Attachments.Count-1
		strFile = oMessage.Attachments.item(i).Filename
		oMessage.Attachments.item(i).SaveAs(strDir & strFile)
	Next
End Sub
I use it for a note taking service called RocketBook. Something my wife got me. Each category of note has a unique email address using plus addressing (me+category1@mydomain.tld) and the rule only has to pick up the TO address to match. Each category has a separate folder. Works great except my 6 year old used to write in my notebook with pens that don't erase. :mrgreen:

If you want multiple folders, you need multiple subs and multiple rules.

jtellier
New user
New user
Posts: 4
Joined: 2020-05-28 23:25

Re: Script Conditionally Save Attachement

Post by jtellier » 2020-05-29 16:58

Awesome thanks... now I am going to show you how green I am on this..... Where do I put the Rule, is it in the normal vbs file in the program directory?

jtellier
New user
New user
Posts: 4
Joined: 2020-05-28 23:25

Re: Script Conditionally Save Attachement

Post by jtellier » 2020-05-29 17:02

Oh I see it in the mailbox, perfect thanks!

jtellier
New user
New user
Posts: 4
Joined: 2020-05-28 23:25

Re: Script Conditionally Save Attachement

Post by jtellier » 2020-05-29 21:18

So I spoke too soon, I put it in the vbs, reloaded and setup the rules and tested but the files never moved over. Is there anywhere specific you need to put the rules or is there an error log somewhere?

Thanks,
Jeremy

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

Re: Script Conditionally Save Attachement

Post by mattg » 2020-05-30 03:58

add the vbs provided to your eventhandlers.vbs (must be this file, in it's existing location)
In the admin GUI enable scripts
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

buskeyl
New user
New user
Posts: 5
Joined: 2020-12-01 01:12

Re: Script Conditionally Save Attachement

Post by buskeyl » 2020-12-01 01:30

Hoping I can get a few pointers here. Just trying to facilitate attachments that get sent into Hmail get saved off to a local folder.

I edited C:\Program Files (x86)\hMailServer\Events\EventHandlers.vbs to add the following to the bottom of the file (It was not clear if it needed to be anyplace specific..

Sub SaveAttachment(oMessage)
Dim i, strDir, strFile
strDir = "D:\Attachments\"
For i = 0 To oMessage.Attachments.Count-1
strFile = oMessage.Attachments.item(i).Filename
oMessage.Attachments.item(i).SaveAs(strDir & strFile)
Next
End Sub


I created "D:\Attachments\"

I enabled Scripts in advanced settings and clicked reload scripts.

I created a rule called Save Attachments, it's action is to "Run a Function" and it's specified script function is "SaveAttachment" Did this as both a main rule, and a rule on the actual account I was sending the test attachment into.

I get no ERROR logs in the log directory, and in the normal operational log, I do see entries in the log file, "Applying rule Save Attachments", however, nothing shows up in d:\attachments

Did I miss something?

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

Re: Script Conditionally Save Attachement

Post by palinka » 2020-12-01 02:25

buskeyl wrote:
2020-12-01 01:30
Hoping I can get a few pointers here. Just trying to facilitate attachments that get sent into Hmail get saved off to a local folder.

I edited C:\Program Files (x86)\hMailServer\Events\EventHandlers.vbs to add the following to the bottom of the file (It was not clear if it needed to be anyplace specific..

Sub SaveAttachment(oMessage)
Dim i, strDir, strFile
strDir = "D:\Attachments\"
For i = 0 To oMessage.Attachments.Count-1
strFile = oMessage.Attachments.item(i).Filename
oMessage.Attachments.item(i).SaveAs(strDir & strFile)
Next
End Sub


I created "D:\Attachments\"

I enabled Scripts in advanced settings and clicked reload scripts.

I created a rule called Save Attachments, it's action is to "Run a Function" and it's specified script function is "SaveAttachment" Did this as both a main rule, and a rule on the actual account I was sending the test attachment into.

I get no ERROR logs in the log directory, and in the normal operational log, I do see entries in the log file, "Applying rule Save Attachments", however, nothing shows up in d:\attachments

Did I miss something?
Did you use "contains"?

Criteria: from contains from@address.com
Criteria: to contains to@address.com

Did you use some other criteria?

buskeyl
New user
New user
Posts: 5
Joined: 2020-12-01 01:12

Re: Script Conditionally Save Attachement

Post by buskeyl » 2020-12-01 02:54

Sorry, didn’t mention the criteria did I? In both cases, the rule criteria was: To: field equals <the email address I was sending my tests into>

Left the TEST field value blank, as I have no idea what’s that’s there for :p.

Would that not work?

buskeyl
New user
New user
Posts: 5
Joined: 2020-12-01 01:12

Re: Script Conditionally Save Attachement

Post by buskeyl » 2020-12-01 02:56

Ha! Changed it to “contains” from “equals” and it worked. Sweet. Would like to understand why that was necessary?

buskeyl
New user
New user
Posts: 5
Joined: 2020-12-01 01:12

Re: Script Conditionally Save Attachement

Post by buskeyl » 2020-12-01 03:14

Also, is there a way for me to invoke a specific PowerShell script? Can I set up a function like the one we did above that will run a PowerShell script wit a message of particular criteria is received? I am fairly proficient with PowerShell, much less so with VBS. I admit I did not search for it yet, it just occurred to me to ask while I had some attention.

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

Re: Script Conditionally Save Attachement

Post by palinka » 2020-12-01 03:45

buskeyl wrote:
2020-12-01 03:14
Also, is there a way for me to invoke a specific PowerShell script? Can I set up a function like the one we did above that will run a PowerShell script wit a message of particular criteria is received? I am fairly proficient with PowerShell, much less so with VBS. I admit I did not search for it yet, it just occurred to me to ask while I had some attention.
haha. Me too. Here's a couple simple ones that can get you started.

Code: Select all

With CreateObject("WScript.Shell")
	.Run "Powershell Start-Sleep -Milliseconds 1000", 0, True
End With


Set PowerShell = CreateObject("WScript.Shell")
PowerShell.Run "Powershell Start-Sleep -Milliseconds 1000", 0, True
Set PowerShell = Nothing


Set PowerShell = CreateObject("WScript.Shell")
PowerShell.Run "Powershell -ExecutionPolicy Bypass -File C:\scripts\powershellscript.ps1", 0, True
Set PowerShell = Nothing
Last edited by palinka on 2020-12-01 03:58, edited 1 time in total.

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

Re: Script Conditionally Save Attachement

Post by palinka » 2020-12-01 03:49

buskeyl wrote:
2020-12-01 02:56
Ha! Changed it to “contains” from “equals” and it worked. Sweet. Would like to understand why that was necessary?
If you don't specify "contains", it attempts to match the entire string.

Code: Select all

"John Doe" <john@doe.com>
does CONTAIN but does NOT EQUAL john@doe.com

^string$ does not necessarily equal ^.*string.*$

Sometimes it does, most times it doesn't when it comes to email addresses.

Edit - if you're more comfortable with regex, there is an option to use it in rules. Its very helpful when you need to mix AND & OR in your criteria.

buskeyl
New user
New user
Posts: 5
Joined: 2020-12-01 01:12

Re: Script Conditionally Save Attachement

Post by buskeyl » 2020-12-01 06:45

“if your more comfortable with REGEX”. Ha ha. Funny. Not even. I’ll learn them eventually. Spent may an hour on regexer.com. Still makes my head hurt. Good to know though. Thanks for all the help. It made total sense once you ‘splained it.

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

Re: Script Conditionally Save Attachement

Post by jimimaseye » 2020-12-01 12:05

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

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

Re: Script Conditionally Save Attachement

Post by SorenR » 2020-12-01 15:40

Easy peasy ...

Image

:mrgreen:
SørenR.

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

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

Re: Script Conditionally Save Attachement

Post by palinka » 2020-12-01 15:55

SorenR wrote:
2020-12-01 15:40
Easy peasy ...

Image

:mrgreen:
A dot is a matching character when enclosed in square brackets (no escaping)?

I just tested it on regex101 and yes, it matches as dot and NOT as any character. You can also escape it - there's no difference.

https://regex101.com/r/7qhw5I/1

I love new scraps of knowledge. This one might actually be useful one day. :mrgreen:

Post Reply