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: 1942
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: 20845
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

Post Reply