Limit outbound mails by user, domain or server average

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
ObiWan
Senior user
Senior user
Posts: 278
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Limit outbound mails by user, domain or server average

Post by ObiWan » 2011-09-05 17:58

andyp wrote:EventLog.Write would be the right solution, if you like to use the hm log
Which is what I suggested; it would just be a matter of adding a "tag" to messages, for example something like the following

Code: Select all


Sub LogMsg(sMsg)
  Dim sBuffer
  
  sBuffer = "[SCRIPT] " & sMsg
  EventLog.Write sBuffer
End Sub

at this point, to extract "your" records from the log you may just run something like

Code: Select all


type logfilename.log|find "[SCRIPT]" >mylog.log

where, by the way, "logfilename.log" will be the hMailServer log file

not all that difficult imHo and could solve the "lock" issue :)

ObiWan
Senior user
Senior user
Posts: 278
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Limit outbound mails by user, domain or server average

Post by ObiWan » 2011-09-05 18:02

philg wrote: AndyP's use of a database via an ODBC (or OLEDB I guess) is a good solution because its much easier to maintain data integrity in that environment than in VBScript, and you can use transactions and database locks etc. to protect the data and the user of the data.
Well... if logging to a DB fits your bill, fine with me, I just wonder how much overhead such an approach may introduce, see, opening a DB connection and writing to a table requires more time than just appending lines to a text file or (better) leveraging the "Eventlog.Write" native function and given that a script should run as fast as possible to avoid slowing down the server and causing issues under heavy load...

philg
Normal user
Normal user
Posts: 38
Joined: 2008-05-03 15:49

Re: Limit outbound mails by user, domain or server average

Post by philg » 2011-09-05 18:43

Well... if logging to a DB fits your bill, fine with me
No I didn't say logging to the database (I know AndyP said he is doing that)!
In my opinion that is not necessary for the reasons I gave previously.

I mean saving the data in the database instead of text files.

Anyway we seem to all be saying the same things in slightly different ways. I guess its up the individual exactly how they implement the scripts at the end of the day.

Its always good to have guidelines and things to watch out for when writing scripts, that way scripts are likely to be of better quality. Maybe there should be a HMS scripting wiki or FAQ (unless there is already one :oops:)

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: Limit outbound mails by user, domain or server average

Post by ^DooM^ » 2011-09-05 18:45

There is only the COMAPI docs, there is no other information other than whats on the forum.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

philg
Normal user
Normal user
Posts: 38
Joined: 2008-05-03 15:49

Re: Limit outbound mails by user, domain or server average

Post by philg » 2011-09-05 18:48

Thought so.

A simple guide might be good - but I understand that these things take time, and most of us just want to get our individual scripts/servers working! I guess all the information is in the forums (and quite a lot in this thread now) for anyone that knows how to use search :)

Thanks

andyp
Normal user
Normal user
Posts: 191
Joined: 2008-01-18 21:00

Re: Limit outbound mails by user, domain or server average

Post by andyp » 2011-09-05 19:03

Which hm COM API object has that customlog method?

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2011-09-06 01:46

My understanding is that you can use custom logging at any time during any of the included events

http://www.hmailserver.com/documentatio ... t_eventlog
(I couldn't find it just then from the help tree menu - but a search of the documentation for 'eventlog' found this and a couple of examples. I guess you need to know about it to find it... :? )

From memory, it creates an 'event log' similar to the daily 'error logs', but test and see.

Also here >> http://www.hmailserver.com/forum/viewto ... 20&t=13890
Martin adds additional info to the hMailserver database using a script.
You might get some ideas from there. The connection to the database is already (and always) open...

Matt
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ObiWan
Senior user
Senior user
Posts: 278
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Limit outbound mails by user, domain or server average

Post by ObiWan » 2011-09-06 08:43

philg wrote:A simple guide might be good - but I understand that these things take time, and most of us just want to get our individual scripts/servers working! I guess all the information is in the forums (and quite a lot in this thread now) for anyone that knows how to use search :)
Well... not sure a "guide" may really be needed, see, the COM API documentation covers most properties/methods exposed by the hMS object model and the scripting section of the forums offers a number of "examples" (ok, working scripts, but those may be used as example); then, a tool like this one may further ease (and speed-up) editing and putting together decent scripts (carefully read the text at the bottom of page :wink: )

andyp
Normal user
Normal user
Posts: 191
Joined: 2008-01-18 21:00

Re: Limit outbound mails by user, domain or server average

Post by andyp » 2011-09-06 19:10

ObiWan wrote: Well... if logging to a DB fits your bill, fine with me, I just wonder how much overhead such an approach may introduce, see, opening a DB connection and writing to a table requires more time than just appending lines to a text file or (better) leveraging the "Eventlog.Write" native function and given that a script should run as fast as possible to avoid slowing down the server and causing issues under heavy load...
The new script I am using write the time consumption into the database. The script is running for a year now and takes for each event about one second. Never had any performance issues, but a lot of problems with the file / lock file method.

ObiWan
Senior user
Senior user
Posts: 278
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Limit outbound mails by user, domain or server average

Post by ObiWan » 2011-09-07 09:36

andyp wrote:The new script I am using write the time consumption into the database. The script is running for a year now and takes for each event about one second. Never had any performance issues, but a lot of problems with the file / lock file method.
Andy, I'm not saying your approach is bad or whatever, but sincerely, 1 second may be too much for a loaded server, consider an instance of hMS getting 200 parallel connections (just an example), the above would mean spending 200 seconds logging messages... I think it's easy to see why I'm talking about "overhead" - what I want to pinpoint here is that a logging function should require milliseconds to avoid it becoming a potential source of "denial of service", that's why I personally prefer using the hMS native logging function, it introduces minimal overhead and deals with all the needed stuff (serialization, lock avoidance...)

As for the outbound mail (which is what this whole thread is about), I use ASSP v2 as the "frontend filter" and it has a good rate-limiter function which allows you to configure the max number of messages which can be sent from a given IP in a given interval of time; going over such a limit will result in an SMTP "tempfail" message and (optionally) in an alert email sent to whatever "admin" address you configured; in my experience the whole thing works quite well and allows to immediately spot "mass senders" which may be either "spammers" which got an account on your server or infected boxes; in both cases having an alert about the issue and at the same time "delaying" the send helps keeping things clean :)

andyp
Normal user
Normal user
Posts: 191
Joined: 2008-01-18 21:00

Re: Limit outbound mails by user, domain or server average

Post by andyp » 2011-09-07 21:18

I am not saying, I don't understand you or you are wrong. But my entire scripts which are about 5-6 the size of this script take in average 1 second for each event in hm. This includes the logging and not 1 second only for the logging. So this is absolutely fine with my bill.

But I seriously doubt that someone can use the script with 200 parallel connections. Currently everything (including the log) is stored in a file and every outbound mail stores something in the storage file. If you use a file here with read and write operation this get extremely difficult to handle. Logging in a file is then the smallest of your problems.

So the current script is simply not designed for heavy loaded servers, discussing logging in a file or in a database at this point doesn't get you anywhere, because I doubt this script (only the file based version) can handle servers with over 10 parallel connections.

yenanlee
New user
New user
Posts: 1
Joined: 2011-09-27 08:09

Re: Limit outbound mails by user, domain or server average

Post by yenanlee » 2011-09-27 09:07

Hi andyp,

I am looking for a script that put a 'pause' or 'delay' before HMailServer will process next batch.
And will process a long 'pause' / 'delay' after certain amount of sending.

For example:

'Pause' / 'Delay' = 10 sec before process another batch
'Pause' / 'Delay' = 300sec after sending 'x' batches (100 batches)

I will CC 20 emails in a batch, so I am not sure when the script said 'number',
a batch with 20 emails in consider 1 email, or 20 emails.

Thanks a lot.

ObiWan
Senior user
Senior user
Posts: 278
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Limit outbound mails by user, domain or server average

Post by ObiWan » 2011-09-27 09:31

yenanlee wrote: I am looking for a script that put a 'pause' or 'delay' before HMailServer will process next batch.
And will process a long 'pause' / 'delay' after certain amount of sending.

For example:

'Pause' / 'Delay' = 10 sec before process another batch
'Pause' / 'Delay' = 300sec after sending 'x' batches (100 batches)

I will CC 20 emails in a batch, so I am not sure when the script said 'number',
a batch with 20 emails in consider 1 email, or 20 emails.
Hmm... not sure it may be a good idea, then by the way, it all depends from what you want to achieve; see, if your purpose is to "limit the damage" which may be caused by a compromised machine/account "pumping out junk" through your server, it would be a better idea performing checks at the initial "accept" stage and, if the bulk-send goes over a given "rate limit" (that is, more than "N" emails [or "R" recipients] in a "T" interval) then the script should reject (it would be good issuing a 4xx aka "tempfail" SMTP code) any further send requests from that account/IP for a given amount of time and, at the same time, should raise an alert to the "postmaster"; the whole mechanism should then also include some kind of "whitelisting" so allowing certain senders (addresses or IPs) to bypass the limiter (you probably don't want to limit distribution lists, newsletters or the like)

indiannic
Normal user
Normal user
Posts: 141
Joined: 2007-09-30 15:55

Re: Limit outbound mails by user, domain or server average

Post by indiannic » 2011-11-17 11:41

hi

i am using this script for a server with around 1200 email ids

it works pretty well

but sometimes i note that the outboundstore.txt contains part domain names ie domain name is not inserted completely

example
40864 1 @maritimelogi

the actual entry should be
40864 1 @maritimelogistics.in

what could be the reason.

rajesh

ObiWan
Senior user
Senior user
Posts: 278
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Limit outbound mails by user, domain or server average

Post by ObiWan » 2011-11-17 17:04

indiannic wrote:hi
what could be the reason.
The simple reason is probably the fact that you are trying to hijack another discussion and posting an issue totally unrelated to the original subject; if you want to get an answer, open a new thread and explain what you are doing or (better) post portions of your script, otherwise, please AVOID hijacking discussions, it's a VERY bad habit

Bhargavpandya
New user
New user
Posts: 5
Joined: 2012-01-09 05:14

Re: Limit outbound mails by user, domain or server average

Post by Bhargavpandya » 2012-01-14 17:53

I Tried using this script to restrict emails per hour.. But as said in the installation manual.. When i reload the script no files are created in event folder.. no log file is created in logfile folder...

I dont know how do i restrict emails of one domain per hour..
I am not intrested in emails of one user per hour..

I just wannt emails per domain per hour

Can someone please help..

Bhargavpandya
New user
New user
Posts: 5
Joined: 2012-01-09 05:14

Re: Limit outbound mails by user, domain or server average

Post by Bhargavpandya » 2012-01-14 17:54

Cant use the script.. Nither the log file is created nor the txt files are created when i reload the script..

Regards

Bhargavpandya
New user
New user
Posts: 5
Joined: 2012-01-09 05:14

Re: Limit outbound mails by user, domain or server average

Post by Bhargavpandya » 2012-01-14 18:22

this is the error recieved..

No files created.. Neither logs nor txt
Error: 800A004C - Description: Path not found - Line: 462 Column: 2 - Code:

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2012-01-15 04:01

can you tell us at least what line 462 says in your eventhandlers.vbs?

That line is empty in the script on the front page of this thread, so I'm guessing that you made some modifications to the script.
Actually, it would be best if you could show the entire eventhandlers.vbs
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

alexnts
New user
New user
Posts: 6
Joined: 2012-04-25 10:53

Re: Limit outbound mails by user, domain or server average

Post by alexnts » 2012-06-11 14:09

Hello.
Is there a possibility to alter the time limit from 1 day to 5 minutes?

I want to set a limit of max 200/messages per 5 minutes.
When this limit is reached to hold the queue and continue the delivery after 5 minutes. And so on.

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2012-06-11 15:46

Yes I'd guess that that is possible.

I'd expect that you'd probably want to create an array for each 5 minute interval in a day, and substitute that for the determination of todays date in that script. Alternately you'd need to keep a tally of number of emails for each user in the last five minutes which would require constant updating and would load the server I'd expect.

How good is your VBS?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

alexnts
New user
New user
Posts: 6
Joined: 2012-04-25 10:53

Re: Limit outbound mails by user, domain or server average

Post by alexnts » 2012-06-11 18:23

My VBS knowledge?
Almost none, unfortunately...

P.S. I see some topics regarding the smtp throttling but no solution found..

infoservi
New user
New user
Posts: 2
Joined: 2012-08-14 06:58

Re: Limit outbound mails by user, domain or server average

Post by infoservi » 2012-08-14 07:04

Hi, I'm using the script and it works great, I want to do is that outboundstore.txt file, insert the date on each line, for example.
# SMTP outgoing storage
2012-01-01 41129 154 infoservi@isdc3.com.ar
2012-01-01 41129 254 @isdc3.com.ar

thanks!
Martin

infoservi
New user
New user
Posts: 2
Joined: 2012-08-14 06:58

Re: Limit outbound mails by user, domain or server average

Post by infoservi » 2012-08-17 05:40

Now, a have this error in ERROR_hmailserver_2012-08-17.log

"ERROR" 5180 "2012-08-17 00:28:47.671" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5180 "2012-08-17 00:29:05.061" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5180 "2012-08-17 00:29:05.249" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5184 "2012-08-17 00:29:09.030" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5184 "2012-08-17 00:29:28.327" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5184 "2012-08-17 00:29:33.358" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5180 "2012-08-17 00:29:51.077" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"
"ERROR" 5188 "2012-08-17 00:29:51.077" "Script Error: Source: Microsoft VBScript runtime error - Error: 800A0046 - Description: Permission denied - Line: 452 Column: 2 - Code: (null)"

in EventHandlers.vbs...

Sub write_log(txt)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs
Dim f

If write_log_active then
Set fs = CreateObject("scripting.filesystemobject")

Dim fn
Dim tmp
fn = logspath & "hmailserver_event_" & get_date & ".log"
Set f = fs.opentextfile(fn, ForAppending, true) -------------------------------------------->> Line 452 <<
tmp = """" & FormatDateTime(Date + time,0) & """" & Chr(9) & """" & txt & """" & nl
f.Write(tmp)
f.Close
End if
End Sub

best regards!

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2012-08-17 06:42

What user do you run the hmailserver service under?
What folder is the hmailserver data folder
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

stephenlwells
New user
New user
Posts: 4
Joined: 2013-04-24 00:58

Re: Limit outbound mails by user, domain or server average

Post by stephenlwells » 2013-04-24 01:03

sorry to raise a thread from the dead :twisted:
-
I'm trying to get this to work and i have 0 experience with scripting i can change the paths and all of that i'm just not sure where to put what in the script could someone post their eventhandler.vbs for me with this script installed i think i could get it from there with any personal info removed from it of course

thanks in advance!

on a side note im using hmail server to host over a 150+ domains in a production environment for over 5 years and it is f reeking awesome i ran into an issue where one of the accounts got hacked (thats what i get for letting users choose their passwords/could have been a virus too) and was sending out a ton of spam. I need to make sure this doesn't happen again or i'm going to have a lot of people mad at me again.

stephenlwells
New user
New user
Posts: 4
Joined: 2013-04-24 00:58

Re: Limit outbound mails by user, domain or server average

Post by stephenlwells » 2013-04-24 03:17

Sorry couldn't figure out how to edit the orginal post so...

I figured it out was user error i didn't have the proper path's set thanks again for this script

:D

stephenlwells wrote:sorry to raise a thread from the dead :twisted:
-
I'm trying to get this to work and i have 0 experience with scripting i can change the paths and all of that i'm just not sure where to put what in the script could someone post their eventhandler.vbs for me with this script installed i think i could get it from there with any personal info removed from it of course

thanks in advance!

on a side note im using hmail server to host over a 150+ domains in a production environment for over 5 years and it is f reeking awesome i ran into an issue where one of the accounts got hacked (thats what i get for letting users choose their passwords/could have been a virus too) and was sending out a ton of spam. I need to make sure this doesn't happen again or i'm going to have a lot of people mad at me again.

xpda
Normal user
Normal user
Posts: 33
Joined: 2013-03-28 22:08

Re: Limit outbound mails by user, domain or server average

Post by xpda » 2013-04-29 19:29

Thanks for the script!

I ran across one problem -- Someone in Nigeria logged into a user's account and was blocked after sending 200 emails. But they kept trying to send 17,000 more emails from the same account. Since the domain count is incremented with an unsuccessful send, it eventually ran over the domain limit of 10,000 and blocked everybody on the domain for a few hours.

I haven't looked into the script, but it seems like it might be easy to increment the counters only for non-denied sends. (I've fixed the problem by raising the domain limits.)

echoDreamz
New user
New user
Posts: 22
Joined: 2010-12-02 20:25

Re: Limit outbound mails by user, domain or server average

Post by echoDreamz » 2013-05-21 02:57

How is the times stored in the outboundstore.txt?

Code: Select all

# SMTP outgoing storage

41414 2 test@xxxxxx
What exactly is 41414?

xpda
Normal user
Normal user
Posts: 33
Joined: 2013-03-28 22:08

Re: Limit outbound mails by user, domain or server average

Post by xpda » 2013-05-21 03:37

echoDreamz wrote:How is the times stored in the outboundstore.txt?
The first number might be the date. The second number is the times.

echoDreamz
New user
New user
Posts: 22
Joined: 2010-12-02 20:25

Re: Limit outbound mails by user, domain or server average

Post by echoDreamz » 2013-05-21 05:28

Well it is 5-20-2013. Which is why I am confused at what 41414 is...

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-05-21 05:34

echoDreamz wrote:Well it is 5-20-2013. Which is why I am confused at what 41414 is...
They are the same value.
That is how Microsoft records dates.

If you open excel, and type

=today()

into any cell, today's date will appear
If you right click and select format, and change the format to 'general' rather than 'date' (today) you will see 41414
Microsoft Office wrote:Microsoft Excel stores dates as sequential numbers that are called serial values. For example, in Microsoft Excel for Windows, January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,448 days after January 1, 1900. Excel stores times as decimal fractions because time is considered a portion of a day. The decimal number is a value ranging from 0 (zero) to 0.99999999, representing the times from 0:00:00 (12:00:00 A.M.) to 23:59:59 (11:59:59 P.M.).
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

echoDreamz
New user
New user
Posts: 22
Joined: 2010-12-02 20:25

Re: Limit outbound mails by user, domain or server average

Post by echoDreamz » 2013-05-21 05:44

Best answer ever. Thank you sir for that explanation!

Is this file updated each day? Or does it create a file per day?

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-05-21 08:08

echoDreamz wrote:Best answer ever. Thank you sir for that explanation!

Is this file updated each day? Or does it create a file per day?
Without looking at the script in detail, I'd expect that the txt file is updated for EACH mail message that is sent
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

echoDreamz
New user
New user
Posts: 22
Joined: 2010-12-02 20:25

Re: Limit outbound mails by user, domain or server average

Post by echoDreamz » 2013-05-23 01:18

Is andyp still active? I believe he said he got this working with ODBC and a database? Is this available? I think this would perform better than the file method.

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-14 19:21

Trying to use this script and having problems with the "limit warning" and "limit passed" email notifications.

I know very little about VB scripts, but I don't think there's a problem with the script. I think I may have something wrong with configuration somewhere.

Below is what I see in the hmail log:



"APPLICATION" 1716 "2013-07-14 09:32:27.842" "SMTPDeliverer - Message 1884688: Delivering message from emailadmin@MYDOMAIN.com to SystemBackup. File: E:\hMailServer\Data\{9E16B752-83EF-426B-BF19-CC3EADD210FD}.eml"
"TCPIP" 1716 "2013-07-14 09:32:27.842" "DNS - MX Lookup: systembackup"
"SMTPD" 1628 3303 "2013-07-14 09:32:27.842" "192.168.168.172" "SENT: 554 Your account/Mailserver has passed SMTP outgoing limits."
"TCPIP" 1716 "2013-07-14 09:32:30.092" "DNS - MX Result: 0 IP addresses were found."
"APPLICATION" 1716 "2013-07-14 09:32:30.092" "SMTPDeliverer - Message 1884688: No mail servers could be found for the address SystemBackup."
"TCPIP" 1816 "2013-07-14 09:32:35.092" "TCPConnection - Posting AcceptEx on 0.0.0.0:995"


The sender is an account called SystemBackup with an email address of SystemBackup@MYDOMAIN.com and the msg_fromaddress = "emailadmin@MYDOMAIN.com"

I am getting a bounce email to emailadmin@MYDOMAIN.com with the following in the body:


Your message did not reach some or all of the intended recipients.

Sent: Sun, 14 Jul 2013 09:39:19 -0400
Subject: Locked: Account limits passed

The following recipient(s) could not be reached:

SystemBackup
Error Type: SMTP
Error Description: No mail servers appear to exists for the recipients address.
Additional information: Please check that you have not misspelled the recipients email address.


What am I doing wrong???

I should mention that neither the user emails nor the admin emails are being sent.

Thanks in advance for any help you can provide.

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-15 00:45

SystemBackup doesn't seem to have a domain name as part of the recipient address

ie It should be 'SystemBackup@yourDomain.com' rather than 'SystemBackup'
Look where you have set that variable...
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-15 04:12

mattg wrote:SystemBackup doesn't seem to have a domain name as part of the recipient address

ie It should be 'SystemBackup@yourDomain.com' rather than 'SystemBackup'
Look where you have set that variable...

Thanks for the response.

I have not altered the script at all. This is the "user" that has exceeded the sending limit. In this case it's actually an account used to send system generated email, but the account is set up exactly as any other user account. Same is happening with a regular user accounts. No email warnings sent. Also I have the script set to send an email to the admin, and I don't even see a bounce for that one.

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-15 04:28

Is this script actually being fired?

Have you enabled scripts?
Have you reloaded scripts?

where did you save this script?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-15 05:21

mattg wrote:Is this script actually being fired?

Have you enabled scripts?
Have you reloaded scripts?

where did you save this script?

This is the only script I'm using, script seems to be running fine although I have not tested to verify it's stopping mail after the limit is hit. Log files are updating, counts seem correct, only problem is the notification emails. I did reload after I set all the constants. The script itself is in the hmail folder where it belongs.

I read in a previous post in this thread something about the default domain and the way users can log in with username instead of email address when having an account on the default domain, which is what my users do. Maybe there's an adjustment to the script I have to do?

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-15 08:17

Is your default domain, the domain that used to send these email notifications?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-15 13:46

mattg wrote:Is your default domain, the domain that used to send these email notifications?

Thanks so much for your interest in my problem!

Yes. In fact it's the only domain on this install of hmailserver and I checked to make sure it's set as the default in the hmail settings.

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-15 14:56

And do you have an account called 'systemBackup'?
What happens if you don't use default domain?
(I normally don't use default domain as it creates too much SPAM, and I don't use this script)
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-15 23:04

mattg wrote:And do you have an account called 'systemBackup'?
What happens if you don't use default domain?
(I normally don't use default domain as it creates too much SPAM, and I don't use this script)
I appreciate your interest mattg, and no disrespect intended, but you're asking questions that I have already answered in my first post. I'm afraid that you're not really going to be able to help unless you were to dive into this script and understand how it works, which I'm not asking you to do.

Let me ask this... Do you have some other way to limit the total number of emails that a user sends out on a daily basis? If so how?

This seems to be a limitation that every mail admin would want to put in place. Is anyone else limiting the number of out going emails? How else can this be done through hmailserver?

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-16 00:54

so did you use the script in the first post of this thread?
Or one of the variants further down?
Perhaps if you show your script as it is currently, in its entirety, then perhaps someone can have a look at the issue that is facing you.
ttambellini wrote:I appreciate your interest mattg, and no disrespect intended, but you're asking questions that I have already answered in my first post. I'm afraid that you're not really going to be able to help unless you were to dive into this script and understand how it works, which I'm not asking you to do.
I don't believe that this one was answered earlier in your posts
mattg wrote:What happens if you don't use default domain?


And yes I saw this
ttambellini wrote:The sender is an account called SystemBackup with an email address of SystemBackup@MYDOMAIN.com and the msg_fromaddress = "emailadmin@MYDOMAIN.com"
, but that isn't the same as
mattg wrote:do you have an account called 'systemBackup'?
Your error message is clearly stating that the script can't find the recipient 'SystemBackup'. My questions are asking if you have such an account on your hMailserver.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-16 04:25

I'm using the script in the first post. Yes, SystemBackup has a domain account and a regular email box on my hmailserver. None of the warning emails are sending though, including those that should send to regular human users, and including the admin as configured in the constants in the beginning of the script.

I don't really know what it means to use a default domain. I'm only using the server to support a small office with about 30 users and one domain. I do have the domain (mydomain.com) set in the default domain area, but I don't know what it actually means to "use a default domain". I could clear that setting but I'm afraid my users and my automated emails would fail because they authenticate with just a username (not the full email address) and password.

what confuses me is that I would think users authentication with username instead of email might have something to do with the failed emails, but the log file registers counts by email address, and even shows different counts for each when there's a login as systembackup and also SystemBackup.

I can post the full script tomorrow, but it's the one in the first post with no changes except for the constants. Maybe I can also post a couple records from the logs the script creates.

But...

I'm not thrilled about using this script. I read in this post where performance may be an issue. I'm running on a vm with limited resources, and I'm noticing it's struggling more than usual when we have heavy activity.

So...

Is there any other option to accomplish this?

By the way, I did vote for the feature. Surprising it has so little support.

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-16 16:59

Below is the only section of the script I've edited. The only thing I've done is to set the Global Variables. Perhaps the IP should be 127.0.0.1 instead of the internal IP of the machine?

Scratch that. Just tested and same results.

I should mention that I've tested and the script does in fact prevent emails from being sent when a user goes over the quota. Just can't get it to send the warning emails to the user and admin.


'------------------------------------------------------------------
' Global variables and settings
'------------------------------------------------------------------

'General
Public obApp
Public domain_buffer
Public Const ipslocalhost = "192.168.168.222" 'separated by #
Public Const user = "XXXXXXXX"
Public Const pw = "XXXXXXXX"
Public Const logspath = "E:\hMailServer\Logs\" 'ends with a backslash
Public Const write_log_active = true

'User and Domain outgoing limitation
Public Const outgoingstore = "E:\hMailServer\Events\outboundstore.txt"
Public Const outgoingexceptions = "E:\hMailServer\Events\outboundexceptions.txt"
Public Const outgoingstoreavg = "E:\hMailServer\Events\outboundstoreavg.txt"
Public Const max_emails_per_user = 100
Public Const max_emails_per_domain = 2000
Public Const warning_factor = 0.8
Public Const server_average_days = 14 ' 0 will deactivate
Public Const server_average_threshold_factor = 10
Public Const warning_factor_avg = 0.6
Public Const msg_admin_warning = True
Public Const msg_admin_passed = True
Public Const msg_user_warning = True
Public Const msg_user_passed = True
Public Const msg_from = "Email Admin <emailadmin@MYDOMAIN.com>"
Public Const msg_fromaddress = "emailadmin@MYDOMAIN.com"


'------------------------------------------------------------------
' Hmailserver Eventhandlers
'------------------------------------------------------------------

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-17 00:11

I just tested this and it seems to work for me. I receive warning of approaching limit, notification of limit reached and send of mail is stopped with appropriate message to sender.

The difference is I took the version of script posted by philg at 2011-01-18 20:55

http://www.hmailserver.com/forum/viewto ... 91#p120891

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-17 01:06

ttambellini wrote:I can post the full script tomorrow, but it's the one in the first post with no changes except for the constants.
but that doesn't mean you've implemented it properly so if you don't post your eventhandlers script in full we can't tell if you've done it correctly.

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-17 23:34

percepts wrote:
ttambellini wrote:I can post the full script tomorrow, but it's the one in the first post with no changes except for the constants.
but that doesn't mean you've implemented it properly so if you don't post your eventhandlers script in full we can't tell if you've done it correctly.

You make a good point. I've attached the entire script. Thanks so much for the help with this!!
Attachments
script.zip
(3.59 KiB) Downloaded 257 times

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 00:15

script looks Ok as far as I can tell. Since last posting I've been playing with script on my system.

As Matt has already suggested, your problem is following message

"TCPIP" 1716 "2013-07-14 09:32:27.842" "DNS - MX Lookup: systembackup"

where that says systembackup it should say systembackup.com (or whatever your TLD is)

you can't lookup MX records without a full domain name. This has nothing to do with script itself.

Somewhere in your setup you are not specifying full domain name.

on my system I use 0.0.0.0 in following but you are using your router/internal IP which may not be correct, I'm not sure, try 0.0.0.0 which will, I think, find the correct IP automatically.

Public Const ipslocalhost = "192.168.168.222" 'separated by #

The default domain is found in hmail admin / settings / advanced

mine is left blank but if you set it, it should be domain.tld and I'm not sure that it affects this script but if you have it set then it must be domain.tld

so basically you need to find where you have specified systembackup without its TLD or an email address without its domain, i.e. user only and not user@domain.tld which could be picking the default domain which may be incorrectly set so check default domain setting as suggested above.

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-18 01:04

I see the issue

Code: Select all

Sub outgoing_limitations_send_message(oClient, oMessage, iswarning, nr, max, isdomain)
	Dim txt
	Dim tmp
	If oclient.username <> "" then
		tmp = oclient.username
	Else
		tmp = oMessage.FromAddress
	End If
...
This bit of the code doesn't check that your sender had a FQDN, and in fact your sender doesn't because they authenticate with just an account name...

The solution is to replace the above code in your eventhandlers with something like

Code: Select all

Sub outgoing_limitations_send_message(oClient, oMessage, iswarning, nr, max, isdomain)
	Dim txt
	Dim tmp
	If oclient.username <> "" then
		tmp = oclient.username
	Else
		tmp = oMessage.FromAddress
	End If
       If instr(2, tmp, "@DOMAIN.com") = 0 then tmp=tmp & "@DOMAIN.com"
...
(I've just added the last line)
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 01:12

Matt,

I also notice that an undefined and unset variable "emailadmin" is used in AddRecipient in several places. How is that working? Does com api default it to sender emailaddress or client username internally if inputs are undefined?

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

Re: Limit outbound mails by user, domain or server average

Post by mattg » 2013-07-18 01:26

if it is undefined and un-set then VB will assume (if Option Explicit is not used at the top) that the variable has the value of a null string - standard VB behaviour.

I ALWAYS type 'Option Explicit' as the very first line of all VB that I write because my spelling is really bad, and I frequently mistype variable names.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 01:34

I took out option explicit because I have other code in eventhandlers.vbs written with undefined variables. Maybe I should clean it up.

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 04:31

This script is very buggy. I added what Matt suggested and also corrected/added some code to fix error of mail not being sent to an admin account. So now user gets warnings and admin gets warnings unless you switch them off in setup CONST values (assuming that works).

I have also switched the code from writing its own event log to using the hmailserver_events.log file. This file will need to be rotated using script at bottom of this post.

Use at your own risk. There may well be more bugs in the code for all I know.

Code: Select all

'Force error on undeclared variables
'Option Explicit

'------------------------------------------------------------------
' Global variables and settings
'------------------------------------------------------------------

'General
Public obApp
Public domain_buffer
Public Const ipslocalhost = "0.0.0.0"  'separated by #  
Public Const user = "Administrator"
Public Const pw = "password"    ' ## enter admin password here

Public Const write_log_active = True

'User and Domain outgoing limitation
Public Const outgoingstore = "c:\program files(x86)\hMailServer\Events\outboundstore.txt"
Public Const outgoingexceptions = "c:\program files(x86)\hMailServer\Events\outboundexceptions.txt"
Public Const outgoingstoreavg = "c:\program files(x86)\hMailServer\Events\outboundstoreavg.txt"
Public Const max_emails_per_user = 1000
Public Const max_emails_per_domain = 10000
Public Const warning_factor = 0.8
Public Const server_average_days = 20     ' 0 will deactivate
Public Const server_average_threshold_factor = 10
Public Const warning_factor_avg = 0.6
Public Const msg_admin_warning = True
Public Const msg_admin_passed = True
Public Const msg_user_warning = True
Public Const msg_user_passed = True
Public Const msg_from         = "Email Admin <user@domain.com>"
Public Const msg_fromaddress  = "user@domain.com"
Public Const msg_ToAdminName  = "Admin User"
Public Const msg_ToAdminEmail = "user@admindomain.com"
Public Const DefaultDomain    = "defaultdomain.com"

Sub OnAcceptMessage(oClient, oMessage)
	Result.Value = 0
	Set obApp = CreateObject("hMailServer.Application")
	Call obApp.Authenticate(user, pw)
	
	If has_client_authenticated(oClient) Then
		write_log ("  User has authenticated. User " & oCLient.username & ", Client " & oClient.IPAddress)
		if not check_outgoing_limitations(oClient, oMessage) Then
			Result.Message = "Your account/Mailserver has passed SMTP outgoing limits."
			Result.Value = 2
		End if
	End if
End Sub


'------------------------------------------------------------------
' SMTP limit outgoing emails of domain and user 
'------------------------------------------------------------------

function check_outgoing_limitations(oClient, oMessage)
	check_outgoing_limitations = true
	Const ForReading = 1, ForWriting = 2, ForAppending = 8
	Dim fs , f
	Set fs = CreateObject("scripting.filesystemobject")
	Dim idt
	Dim content
	Dim ln
	Dim arr
	Dim usern
	Dim usernadd
	Dim usernnr
	Dim usernnrmax
	Dim domn
	Dim domnadd
	Dim domnnr
	Dim domnnrmax
	Dim reason
	Dim rcptscnt
	Dim dayamounts(200)
	Dim i, k
	
	For i = 0 To 200
		dayamounts(i) = 0
	Next 
	Dim pos
	Dim avg
	Dim minday
	minday = 999999
	Dim toindex
	Dim excptn
	
	write_log("  SMTP outgoing limitations")
	
	If oclient.username <> "" Then
		If instr(1,oclient.username,"@") = 0 Then
			usern = oclient.username & "@" & obApp.Settings.DefaultDomain
			domn = "@" & obApp.Settings.DefaultDomain
		Else
			usern = oclient.username
			domn = Mid(oclient.username,InStr(1,oclient.username,"@"))
		End If
	ElseIf is_local_domain(omessage.fromaddress) then
		usern = omessage.fromaddress
		domn = Mid(omessage.fromaddress,InStr(1,omessage.fromaddress,"@"))
	Else
		usern = "local"
		domn = "@local"
	End If
	content = "# SMTP outgoing storage" & nl & nl
	usernadd = true
	domnadd = true
	usernnr = 1
	domnnr = 1
	usernnrmax = max_emails_per_user
	domnnrmax = max_emails_per_domain
	idt = CLng(Date())
	rcptscnt = omessage.Recipients.count
	write_log("   Number of recipients " & rcptscnt)
	
	write_log("   Reading exceptions file " & outgoingexceptions)
	If fs.FileExists(outgoingexceptions) Then
		Set f = fs.OpenTextFile(outgoingexceptions, ForReading)
		Do While Not f.AtEndOfStream
			ln = f.ReadLine
			If ln <> "" And Mid(ln,1,1) <> "#" And Len(ln) > 3 Then
				arr = Split(ln,Chr(9))
				If UBound(arr) = 1 Then
					If arr(0) = usern Then
						usernnrmax = CLng(arr(1))
						write_log ("    new user limit " & ln)
					End if
					If arr(0) = domn Then
						domnnrmax = CLng(arr(1))
						write_log ("    new domain limit " & ln)
					End if
				Else
					write_log ("    cannot process line " & Mid(ln,1,25))
				End If
			ElseIf Len(ln) > 5 And f.Line > 4 + 1 then
				write_log ("    skipping line " & Mid(ln,1,25))
			End If
		Loop
	Else
		Set f = fs.OpenTextFile(outgoingexceptions, ForWriting, true)
		f.Write("# Outgoing limitation exceptions tab / chr(9) separated" & nl)
		f.Write("# Examples (without # at the beginning)" & nl)
		f.Write("# @yourdomain.com	10000" & nl)
		f.Write("# address@yourdomain.com	5000" & nl & nl)
		f.Close 
	End If
	
	write_log("   Reading storage file " & outgoingstore)
	If fs.FileExists(outgoingstore) Then
		Set f = fs.OpenTextFile(outgoingstore, ForReading)
		Do While Not f.AtEndOfStream
			ln = f.ReadLine
			If ln <> "" And Mid(ln,1,1) <> "#" And Len(ln) > 5 Then
				arr = Split(ln," ")
				If UBound(arr) > 1 Then
					If minday > CLng(arr(0)) Then
						minday = CLng(arr(0))
					End If
				End If
				If UBound(arr) = 2 Or UBound(arr) = 3 Then
					If CLng(arr(0)) = idt And arr(2) = usern Then
						usernnr = CLng(arr(1)) + rcptscnt
						usernadd = False
						write_log ("    adding to line " & ln)
						If usernnr > usernnrmax Then
							If UBound(arr) = 3 Then
								If arr(3) = "X" then
									write_log ("    deny already sent")
								Else
									write_log ("    sending deny")
									outgoing_limitations_send_message oClient, oMessage, false, usernnr, usernnrmax, false
								End if
							Else
								write_log ("    sending deny")
								outgoing_limitations_send_message oClient, oMessage, false, usernnr, usernnrmax, false
							End If
							content = content & arr(0) & " " & usernnr & " " & arr(2) & " X" & nl
						ElseIf usernnr > usernnrmax * warning_factor then
							If UBound(arr) = 3 Then
								If arr(3) = "W" then
									write_log ("    warning already sent")
								Else
									write_log ("    sending warning")
									outgoing_limitations_send_message oClient, oMessage, true, usernnr, usernnrmax, false
								End if
							Else
								write_log ("    sending warning")
								outgoing_limitations_send_message oClient, oMessage, true, usernnr, usernnrmax, false
							End If
							content = content & arr(0) & " " & usernnr & " " & arr(2) & " W" & nl
						Else
							content = content & arr(0) & " " & usernnr & " " & arr(2) & nl
						End if
					elseIf CLng(arr(0)) = idt And arr(2) = domn Then
						domnnr = CLng(arr(1)) + rcptscnt
						domnadd = false
						write_log ("    adding to line " & ln)
						If domnnr > domnnrmax Then
							If UBound(arr) = 3 Then
								If arr(3) = "X" then
									write_log ("    deny already sent")
								Else
									write_log ("    sending deny")
									outgoing_limitations_send_message oClient, oMessage, false, domnnr, domnnrmax, true
								End if
							Else
								write_log ("    sending deny")
								outgoing_limitations_send_message oClient, oMessage, false, domnnr, domnnrmax, true
							End If
							content = content & arr(0) & " " & domnnr & " " & arr(2) & " X" & nl
						ElseIf domnnr > domnnrmax * warning_factor then
							If UBound(arr) = 3 Then
								If arr(3) = "W" then
									write_log ("    warning already sent")
								Else
									write_log ("    sending warning")
									outgoing_limitations_send_message oClient, oMessage, true, domnnr, domnnrmax, true
								End if
							Else
								write_log ("    sending warning")
								outgoing_limitations_send_message oClient, oMessage, true, domnnr, domnnrmax, true
							End If
							content = content & arr(0) & " " & domnnr & " " & arr(2) & " W" & nl
						Else
							content = content & arr(0) & " " & domnnr & " " & arr(2) & nl
						End if
					ElseIf CLng(arr(0)) < idt - server_average_days Then
						write_log ("    deleting line " & ln)
					Else
						content = content & arr(0) & " " & arr(1) & " " & arr(2) & nl
						'write_log ("    copying line " & ln)
					End If
					If Mid(arr(2),1,1) <> "@" Then
						pos = idt - CLng(arr(0))
						dayamounts(pos) = dayamounts(pos) + CLng(arr(1))
					End If
				Else
					write_log ("    cannot process line " & Mid(ln,1,25))
				End If
			ElseIf Len(ln) > 5 And f.Line > 1 + 1 then
				write_log ("    skipping line " & Mid(ln,1,25))
			End If
		Loop
		f.Close
		If usernadd Then
			content = content & idt & " " & usernnr & " " & usern & nl
		End If
		If domnadd Then
			content = content & idt & " " & domnnr & " " & domn & nl
		End If
		Set f = fs.OpenTextFile(outgoingstore, ForWriting, true)
		f.Write(content)
		f.Close 
	Else
		content = content & idt & " " & usernnr & " " & usern & nl
		content = content & idt & " " & domnnr & " " & domn & nl
		Set f = fs.OpenTextFile(outgoingstore, ForWriting, true)
		f.Write(content)
		f.Close 
	End If
	
	toindex = idt - minday
	avg = CDbl(0)
	If toindex >=5 then
		For i = 1 To toindex
			avg = avg + CDbl(dayamounts(i))
		Next
		avg = CDbl(avg) / CDbl(toindex)
		write_log("   Statistic calculation over " & server_average_days & " days")
		write_log("     todays amount " & dayamounts(0) & "   average " & avg & "   maximum " & avg * server_average_threshold_factor)
		write_log("   Checking statistics")
	End If
	If toindex < 5 then
		write_log("   Statistic calculation is only done over at least 5 days. Available days: " & toindex)
	ElseIf avg < 5 Then
		write_log("     average below 5 mails per day, ignoring average statistic")
	ElseIf dayamounts(0) > avg * server_average_threshold_factor Then
		write_log("     todays amount has passed limit of " & avg * server_average_threshold_factor)
		outgoing_limitations_avg_send_admin dayamounts(0),avg * server_average_threshold_factor,false
		check_outgoing_limitations = False
	ElseIf dayamounts(0) > avg * server_average_threshold_factor * warning_factor_avg Then
		write_log("     todays amount has passed warning level of " & avg * server_average_threshold_factor * warning_factor_avg)
		outgoing_limitations_avg_send_admin dayamounts(0),avg * server_average_threshold_factor,true
		check_outgoing_limitations = False
	Else
		write_log("     within limits")
	End If
	
	write_log("   Checking limits")
	If usernnrmax < usernnr Then
		check_outgoing_limitations = false
		write_log("     max of user passed!")
	ElseIf domnnrmax < domnnr Then
		check_outgoing_limitations = false
		write_log("     max of domain passed!")
	Else
		write_log("     within limits")
	End If
	
	excptn = false
	If oMessage.FromAddress = msg_ToAdminEmail Then
		excptn = true
	Else
		For k = 0 To oMessage.recipients.count - 1
			If oMessage.recipients(k).OriginalAddress = msg_ToAdminEmail Then
				excptn = True
			End If
		Next
	End If
	If excptn = True Then
		write_log("   Mail from/to admin -> passes lock")
		check_outgoing_limitations = true
	End if
End function

Sub outgoing_limitations_avg_send_admin(nr, max, iswarning)
	Const ForReading = 1, ForWriting = 2, ForAppending = 8
	Dim fs , f
	Set fs = CreateObject("scripting.filesystemobject")
	Dim txt
	Dim tmp
	Dim str
	Dim out
	Dim snd
	
	If iswarning Then
		tmp = "Warning: Todays outgoing emails will reach lock soon"
		
		txt = "Hello " & msg_ToAdminEmail & nl & nl
		txt = txt & "todays outgoing email will reach avg limit soon." & nl & nl
		txt = txt & "Current amount is " & nr & nl
		txt = txt & "Limit is " & max & nl & nl
		txt = txt & "Regards" & nl
		txt = txt & msg_ToAdminEmail
		
		str = "W" & CLng(Date())
	Else
		tmp = "Locked: Todays outgoing emails have passed avg limit"
		
		txt = "Hello " & msg_ToAdminEmail & nl & nl
		txt = txt & "todays outgoing email have passed avg limit." & nl & nl
		txt = txt & "Current amount is " & nr & nl
		txt = txt & "Limit is " & max & nl & nl
		txt = txt & "Regards" & nl
		txt = txt & msg_ToAdminEmail
		
		str = "X" & CLng(Date())
	End If
	
	snd = true
	If fs.FileExists(outgoingstoreavg) Then
		Set f = fs.OpenTextFile(outgoingstoreavg,ForReading)
		out = f.ReadAll
		f.Close
		If out = str Then
			snd = false
		End If
	End If
	
	If snd then
		Set nMessage = CreateObject("hMailServer.Message")
		nMessage.From = msg_from
		nMessage.FromAddress = msg_fromaddress
		nMessage.AddRecipient msg_ToAdminName, msg_ToAdminEmail
		nMessage.Subject = tmp
		nMessage.Body = txt
		nMessage.Save
		
		Set f = fs.OpenTextFile(outgoingstoreavg,ForWriting,True)
		f.Write(str)
		f.Close
	End If
End Sub

Sub outgoing_limitations_send_message(oClient, oMessage, iswarning, nr, max, isdomain)
	Dim txt
	Dim tmp
	Dim nMessage
	
	If oclient.username <> "" then
		tmp = oclient.username
	Else
		tmp = oMessage.FromAddress
	End If
	
	If instr(2, tmp, DefaultDomain) = 0 then 
	 tmp=tmp & "@" & DefaultDomain
	End If
	 
	If iswarning Then
		txt = "Hello " & tmp & nl & nl
		txt = txt & "you will soon reach your account limits." & nl & nl
		txt = txt & "Current amount is " & nr & nl
		txt = txt & "Limit is " & max & nl & nl
		If isdomain Then
			txt = txt & "This is a limit of the your domain." & nl & nl
		Else
			txt = txt & "This is a limit of the your account." & nl & nl
		End If
		txt = txt & "Regards" & nl
		txt = txt & msg_ToAdminEmail
		
		If msg_admin_warning then
		End If
		If msg_user_warning then
			Set nMessage = CreateObject("hMailServer.Message")
			nMessage.From = msg_from
			nMessage.FromAddress = msg_fromaddress
			nMessage.AddRecipient tmp, tmp
			nMessage.Subject = "Warning: Account limits will be reached soon"
			nMessage.Body = txt
			nMessage.Save
		End if
	Else
		txt = "Hello " & tmp & nl & nl
		txt = txt & "you have passed your account limits." & nl & nl
		txt = txt & "Current amount is " & nr & nl
		txt = txt & "Limit is " & max & nl & nl
		If isdomain Then
			txt = txt & "This is a limit of the your domain." & nl & nl
		Else
			txt = txt & "This is a limit of the your account." & nl & nl
		End If
		txt = txt & "Regards" & nl
		txt = txt & msg_ToAdminEmail
		
		If msg_admin_passed then
			Set nMessage = CreateObject("hMailServer.Message")
			nMessage.From = msg_from
			nMessage.FromAddress = msg_fromaddress
			nMessage.AddRecipient msg_ToAdminName, msg_ToAdminEmail
			nMessage.Subject = "Locked: Account limits passed"
			nMessage.Body = txt
			nMessage.Save
		End If
		If msg_user_passed then
			Set nMessage = CreateObject("hMailServer.Message")
			nMessage.From = msg_from
			nMessage.FromAddress = msg_fromaddress
			nMessage.AddRecipient tmp, tmp
			nMessage.Subject = "Locked: Account limits passed"
			nMessage.Body = txt
			nMessage.Save
		End if
	End If
End Sub

'------------------------------------------------------------------
' General functions of all scripts
'------------------------------------------------------------------

Sub write_log(txt)
	If write_log_active then
		EventLog.Write("Limit SMTP Script:"+txt)
	End if
End Sub

Function get_date
	Dim tmp
	Dim erg
	tmp = Year(Date)
	erg = CStr(tmp)
	
	If Month(Date) < 10 Then
		tmp = "0" & Month(Date)
	Else
		tmp = Month(Date)
	End If
	erg = erg & "-" & tmp
	
	If day(Date) < 10 Then
		tmp = "0" & day(Date)
	Else
		tmp = day(Date)
	End If
	erg = erg & "-" & tmp
	
	get_date = erg
End Function

Function nl
	nl = Chr(13) & Chr(10)
End function

Function is_local_domain(domain_or_email)
	is_local_domain = False
	Dim domain
	Dim doms
	Dim alss
	Dim i
	Dim j
	
	If InStr(1,"  " & domain_or_email,"@") > 0 Then
		domain = Mid(domain_or_email, InStr(1,domain_or_email,"@") + 1)
	Else
		domain = domain_or_email
	End If
	
	If domain_buffer = "" then
		i = 0
		Set doms = obapp.Domains
		Do While i <= doms.Count - 1
			Set dom = doms.Item(i)
			domain_buffer = domain_buffer & "#" & dom.Name
			j = 0
			Set alss = dom.DomainAliases
			Do While j <= alss.Count - 1
				Set als = alss.item(j)
				domain_buffer = domain_buffer & "#" & als.AliasName
				j = j + 1
			Loop
			i = i + 1
		Loop
	End If
	
	If InStr(1, "  " & domain_buffer, domain) > 0 Then
		is_local_domain = True
	End If
End Function

Function has_client_authenticated(oclient)
	has_client_authenticated = false
	If oCLient.username <> "" Or InStr(1,"  " & ipslocalhost, oClient.IPAddress) > 0 Then
		has_client_authenticated = true
	End if
End Function

to cycle hmailserver_events.log file run following script from windows task scheduler daily

Code: Select all

Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set oLog = CreateObject("hMailServer.Logging")
Set oApp = CreateObject("hMailServer.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")

LogDate = DateAdd("d", -0, Now)
LogDate = Replace (LogDate, " ", "_")
LogDate = Replace (LogDate, ":", ".")

OldName = oLog.CurrentEventLog
NewName = Left(oldName,(Len(OldName) - 4)) + "_" + LogDate + ".log"
Call oApp.Authenticate("Administrator", "password")  ' ## set password here
Call oApp.Stop
oFSO.MoveFile OldName , NewName
Call oApp.Start
Set WshShell = Nothing

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 05:05

in code above the following

Code: Select all

Sub outgoing_limitations_send_message(oClient, oMessage, iswarning, nr, max, isdomain)
   Dim txt
   Dim tmp
   Dim nMessage
   
   If oclient.username <> "" then
      tmp = oclient.username
   Else
      tmp = oMessage.FromAddress
   End If
   
   If instr(2, tmp, DefaultDomain) = 0 then 
    tmp=tmp & "@" & DefaultDomain
   End If
should be

Code: Select all

Sub outgoing_limitations_send_message(oClient, oMessage, iswarning, nr, max, isdomain)
   Dim txt
   Dim tmp
   Dim nMessage
   
   If oclient.username <> "" then
      tmp = oclient.username
   Else
      tmp = oMessage.FromAddress
   End If
   
   If instr(1, tmp, DefaultDomain) = 0 then 
    tmp=tmp & "@" & DefaultDomain
   End If

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 16:02

Limit SMTP VERSION 6

I have made quite a few more modifications and cleanup to script and recreated zip file which contains everything you need with install instructions. Consider this zip file an updated alternative to the one in post 1 of this topic.
I have therefore called it Version 6 of script.

It uses built in hmailserver_events.log file and a script to cycle/rotate that file.

Option to email admin wasn't working due to script bug. I have made script default to notifying specified admin user of exceeded limits since offending end user won't be able to email admin when they are blocked. i.e. I removed the optional part of that which wasn't working anyway.

I have forced script to use message fromAdress instead of optionally using oclient username (which doesn't contain domain). I did this on the asumption that since this script is fired in onAcceptMessage there will always be a Message with fromaddress in it.

some other cleanup by removing redundant/empty code and unecessary options (IMO)

Hopefully it will be a little more reliable now but I haven't checked every line of code but its seems to work OK for me.

Note: Since this script uses filesystem to open, read, write, close upto 4 files for each email processed, it has the potential to slow down message processing. This may be significant on a busy server.
You have been warned.

Good luck with it...

ttambellini
New user
New user
Posts: 22
Joined: 2013-07-14 19:03
Location: Pittsburgh, PA

Re: Limit outbound mails by user, domain or server average

Post by ttambellini » 2013-07-18 20:00

Awesome work on this!

I must apologize for being a bit short initially. I'm used to working with software companies that sell overpriced crappy software and offer very little support. My experience with hmailserver has been the exact opposite; Great software at no cost, and folks willing to help because they like helping out their fellow hmailserver users.

Thanks!!

I've done some initial testing and everything seems to work perfectly.

I like the changes you've made. Seems to clean things up quite a bit.

As you mentioned, the new script uses the built in hmailserver_events.log file and a script to cycle/rotate that file. To make this happen I see you changed:

Sub write_log(txt)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs
Dim f

If write_log_active then
Set fs = CreateObject("scripting.filesystemobject")

Dim fn
Dim tmp
fn = logspath & "hmailserver_event_" & get_date & ".log"
Set f = fs.opentextfile(fn, ForAppending, true)
tmp = """" & FormatDateTime(Date + time,0) & """" & Chr(9) & """" & txt & """" & nl
f.Write(tmp)
f.Close
End if
End Sub


to:


Sub write_log(txt)
If write_log_active then
EventLog.Write("Limit SMTP Script:"+txt)
End if
End Sub


The custom script log doesn't require the CycleEventLog script to run via the Windows scheduler because it creates/writes to a log file name that contains the date (e.g. hmailserver_event_2013-07-18.log) and would seem to me that it's a simpler solution. Why use the built in hmailserver_events.log file instead of the custom one? Not challenging, just trying to understand.

Thanks again for all the work on this!!!

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit outbound mails by user, domain or server average

Post by percepts » 2013-07-18 20:20

if you look at hmail admin admin logging options you will see an option to leave files open. This only applies to hmail internal log files and not files people define themselves in scripts. That opening of a file slows down processing. See the help on that admin panel.

So using the hmail inbuilt log file has potential performance benefit. so that's one reason. Secondly very little if anything uses it except user writtten scripts so you may as well use it since its there. Also see comments earlier in thread about file opens. And thirdly using a user defined file just adds another log file which isn't really necessary.

The cycle log file script just renames the file which is very quick. i.e. no overhead, although it does stop and restart hmail. You can take the stop and restart out if you don't have "keep files open" set in admin logging options. And the cycle puts a datetime into the archived log file name.

But you can do what you like with the script if you prefer. And infact you will probably switch off logging to that file anyway once you've established its working properly so it really doesn't make much difference which method you use.
I just happen to prefer that option.

Post Reply