Local Send Only

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Local Send Only

Post by Tezcatlipoca » 2010-01-04 13:32

Hi all,

Is it possible to tell hMail to only allow some users in a domain to make internal emails only? So, for example, I have a domain called mydomain.co.uk in hMail that contains three users, usera@, userb@ and userc@.

I want usera@ to be able to send emails to any email account in the world. This is the default, and works great. I want userb@ and userc@, however, to only be able to send emails to mydomain.co.uk accounts.

I originally posted this query in the main forum, and was advised that scripting could do this. I have a reasonable level of VBScripting ability, but wanted to ascertain first as to whether this had come up before and whether or not there was an existing script I could use/modify for my purposes.

Looking at the scripting documentation, I suspect the function I need to be looking at is bool IsLocalUser, which should check to see if the recipient is a local user. I understand the principle that if this check comes back as a non-match, i.e. the user is trying to email an external email account, then the process should be blocked, and, preferably, the user should recieve a notice telling them the email has not been sent and why.

This script would need to be attached to users, rather than IP addresses, since I DHCP all my client machines and a great many people hot-desk, so there's no assurance that a person is at the same desk every time.

Can anybody help with pointing me in the right direction for this, or, if a script like this already exists, could somebody let me know where to find it?

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

Re: Local Send Only

Post by mattg » 2010-01-04 17:55

Try something like this...

(I was intending setting something like this up for my little kids anyway - I'll probably also add an 'approval' requirement to incoming e-mail one day soon)

I have this in OnDeliverMessage, but it doesn't have to be I don't reckon. OnDeliverMessage is silent to the sender...
I also assumed less than 100 such local accounts, and less than 100 recipients per e-mail. You could either change this or perhaps even force this...

Code: Select all

Sub OnDeliverMessage(oMessage)
	Dim LocalOnlyAccounts(100)
	Dim temprecipients(100)
	Dim i, j, k, l
	Dim varDomain
	Dim varAddress

	varDomain = "mydomain.com"
	LocalOnlyAccounts(0)= "user1" & "@" & varDomain
	LocalOnlyAccounts(1)= "user2" & "@" & varDomain

	For i = 0 to uBound(LocalOnlyAccounts)
		If oMessage.FromAddress = LocalOnlyAccounts(i) then 'sender is restricted account
			for j = 0 to oMessage.recipients.count -1
				temprecipients(j) = oMessage.recipients.item(j).address
			next 'j
			j = 0
			k = oMessage.recipients.count -1
			while j <= k
				If oMessage.recipients.item(j).islocaluser = false then 'remove non-local recipients
					l= j					
					while l < k 
						temprecipients(l) = temprecipients(l+1)
						l=l+1
					wend
					temprecipients(k) = ""
					k=k-1
				else
					j=j+1						
				end if
			wend			
			oMessage.clearrecipients()
			For j = 0 to k
				varAddress = temprecipients(j)
				oMessage.AddRecipient "", varAddress
			Next 'j
			oMessage.save

		End if
	Next 'i

End 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

Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Re: Local Send Only

Post by Tezcatlipoca » 2010-01-04 19:37

Hmm...I just tried that, putting it into the correct part of my hMail's EventHandlers.vbs script, but nothing.

I changed mydomain.com to be my actual domain in question and changed user1 and user2 to two test users, both still genuine accounts, I have in the domain.

Both test users can still email out to both internal (so <user>@<myrealdomain>) and external (to, for example, my gmail account).

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

Re: Local Send Only

Post by mattg » 2010-01-05 01:43

Did you get an error log created?
Also, do you have a default domain set?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Re: Local Send Only

Post by Tezcatlipoca » 2010-01-05 15:10

mattg wrote:Did you get an error log created?
Also, do you have a default domain set?
No error log that I can see. Er...excuse my ignorance, default domain set...?

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

Re: Local Send Only

Post by mattg » 2010-01-05 15:31

logs are typically in C:\Program files\hmailserver\logs but this can be changed within your hmailserver.ini
The error log will be named like 'ERROR_hmailserver_DATE.log'

A default domain is set in the hMailserver Administrator under Settings >> Advanced
This is blank by default, but you may have added a domain name.

Can you show your entire EventHandler.vbs?

Also, did you enable scripts, reload the script and then check syntax? Was there any errors?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Re: Local Send Only

Post by Tezcatlipoca » 2010-01-05 15:48

mattg wrote:logs are typically in C:\Program files\hmailserver\logs but this can be changed within your hmailserver.ini
The error log will be named like 'ERROR_hmailserver_DATE.log'
No errors listed.
mattg wrote:A default domain is set in the hMailserver Administrator under Settings >> Advanced
This is blank by default, but you may have added a domain name.
Ok, mine is indeed blank.
mattg wrote:Can you show your entire EventHandler.vbs?
Sure. Here you go:

Code: Select all

'   Sub OnClientConnect(oClient)
'   End Sub

'   Sub OnAcceptMessage(oClient, oMessage)
'   End Sub

'   Sub OnDeliveryStart(oMessage)
'   End Sub

Sub OnDeliverMessage(oMessage)
   Dim LocalOnlyAccounts(100)
   Dim temprecipients(100)
   Dim i, j, k, l
   Dim varDomain
   Dim varAddress

   varDomain = "mydomain.com"
   LocalOnlyAccounts(0)= "user1" & "@" & varDomain
   LocalOnlyAccounts(1)= "user2" & "@" & varDomain

   For i = 0 to uBound(LocalOnlyAccounts)
      If oMessage.FromAddress = LocalOnlyAccounts(i) then 'sender is restricted account
         for j = 0 to oMessage.recipients.count -1
            temprecipients(j) = oMessage.recipients.item(j).address
         next 'j
         j = 0
         k = oMessage.recipients.count -1
         while j <= k
            If oMessage.recipients.item(j).islocaluser = false then 'remove non-local recipients
               l= j               
               while l < k
                  temprecipients(l) = temprecipients(l+1)
                  l=l+1
               wend
               temprecipients(k) = ""
               k=k-1
            else
               j=j+1                  
            end if
         wend         
         oMessage.clearrecipients()
         For j = 0 to k
            varAddress = temprecipients(j)
            oMessage.AddRecipient "", varAddress
         Next 'j
         oMessage.save

      End if
   Next 'i

End Sub

'   Sub OnBackupFailed(sReason)
'   End Sub

'   Sub OnBackupCompleted()
'   End Sub

'   Sub OnError(iSeverity, iCode, sSource, sDescription)
'   End Sub

'   Sub OnDeliveryFailed(oMessage, sRecipient, sErrorMessage)
'   End Sub
The relevant sections have been edited to reflect my actual domain and two of my genuine users, removed here for purposes of privacy.

I can confirm that my test users can still email both internal and external addresses.
mattg wrote:Also, did you enable scripts, reload the script and then check syntax? Was there any errors?
Enabled, Reloaded and Checked. No errors.

I have also set Logging on whilst testing, and can see my outgoing mail being smoothly handled by hMail as its sent, just as it is with any genuine user, which leads to to believe that perhaps the script is fine but it's not properly activated?

Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Re: Local Send Only

Post by Tezcatlipoca » 2010-01-05 16:01

***UPDATE*** Ah ha! I restarted hMail, made sure the script was there, Enabled and Reloaded then tried again and it looks like it's working fine!

My test user can currently email to @mydomain.com accounts, which is correct, but an email to my external gMail account still isn't as having as having arrived, which was sent about 10 minutes ago! :D

mattg, you are an absolute star, sir!

I did initially want my users to get a message back saying that they couldn't email external accounts when they tried, but I'm guessing I can do this on a user by user basis via rules? Or is it fairly simple to add something to this script that says it?

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

Re: Local Send Only

Post by mattg » 2010-01-05 16:13

Do you require SMTP authentication to send from your mailserver in all IP ranges, especially the ranges that the users are located in?

Sorry, I see your update. Great. (I didn't have to restart my server to update the script though...)

You can send a message back to the sender as part of this script. Just create a new message using the information collected. You could incorporate which addresses you have rejected...

I tested with multiple recipients. All local accounts allowed, all external accounts dropped (silently).
If you logging enabled, you will see in the logs where the message is just dropped, or if it is sent to external accounts.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Re: Local Send Only

Post by Tezcatlipoca » 2010-01-05 17:26

mattg wrote:You can send a message back to the sender as part of this script. Just create a new message using the information collected. You could incorporate which addresses you have rejected...
Ok, so am I right in thinking that something like this:

Code: Select all

dim oMessage
Set oMessage = CreateObject("hMailServer.Message")
oMessage.From = "Server Admin"
oMessage.FromAddress = "admin@mydomain.com"
oMessage.Subject = "ACtion Disallowed"
oMessage.AddRecipient "Email User", LocalOnlyAccounts(i)
oMessage.Body = "You can only send emails to another @mydomain.com account.  Your email has not been sent."
oMessage.Save 
would suit the bill? If so, where exactly in your script would I plug it in?

In addition, I have a total of 17 users who should be thus affected. I've just listed their section in the code as:

LocalOnlyAccounts(0)=

through to

LocalOnlyAccounts(16)=

rather than using the usual computer convention of entering single digit numbers as 0# (so my list goes (1), (2), (3), rather than (01), (02), (03)). I assume this is fine?
Last edited by Tezcatlipoca on 2010-01-05 17:38, edited 1 time in total.

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

Re: Local Send Only

Post by mattg » 2010-01-05 17:38

Yep exactly like I was playing with, only I have the rejected recipients listed in there too :)

It needs to go somewhere where it only fires if a message is rejected, not every time a 'local only user' sends mail...

Also, I got my rejected recipients included and I noticed an error.
It looks like all recipients after the first rejection are thereafter rejected. That's not what I had in mind...
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: 19902
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Local Send Only

Post by mattg » 2010-01-05 18:14

So here's my script as it stands now.

It correctly removes non-local recipients and sends a message to the sender telling them what they have done wrong

Code: Select all

Sub OnDeliverMessage(oMessage)

	Dim LocalOnlyAccounts(100)
	Dim temprecipients(100)
	Dim i, j, k, l
	Dim varDomain
	Dim varAddress
	Dim RejectedAddress

	varDomain = "domain.com"   						'CHANGE ME
	LocalOnlyAccounts(0)= "user1" & "@" & varDomain		'CHANGE ME
	LocalOnlyAccounts(1)= "user2" & "@" & varDomain		'CHANGE ME

	RejectedAddress = ""

	For i = 0 to uBound(LocalOnlyAccounts)
		If LCase(oMessage.FromAddress) = LocalOnlyAccounts(i) then 'sender is restricted account
			for j = 0 to oMessage.recipients.count -1
				temprecipients(j) = oMessage.recipients.item(j).address
			next 'j
			j = 0
			k = oMessage.recipients.count -1
			while j <= k
				If instr(temprecipients(j),"@"& VarDomain) = 0 then 'remove non-local recipients
					l= j					
					RejectedAddress = RejectedAddress & temprecipients(l) & VBNewLine
					while l < k 
						temprecipients(l) = temprecipients(l+1)
						l=l+1
					wend
					temprecipients(k) = ""
					k=k-1
				else
					j=j+1						
				end if
			wend			
			oMessage.clearrecipients()
			For j = 0 to k
				varAddress = temprecipients(j)
				oMessage.AddRecipient "", varAddress
			Next 'j
			oMessage.save

		End if
	Next 'i

	If RejectedAddress <> "" then 'we found some invalid recipients
		dim oApp, newMessage
		dim MessageSubject
		dim MessageRecipientAddress

		Set oApp = CreateObject("hMailServer.Application")

		' Give this script permission to access all
		' hMailServer settings.
		Call oApp.Authenticate("Administrator", "AdminPassword")  		'CHANGE ME

		MessageRecipientAddress = oMessage.FromAddress
		MessageSubject = "Automatically rejected mail"

		Set newMessage = CreateObject("hMailServer.Message")
		newMessage.From = "Automated System Mail <admin@yourdomain.com>"		'CHANGE ME
		newMessage.FromAddress = "admin@yourdomain.com"				'CHANGE ME
		newMessage.Subject = MessageSubject
		newMessage.AddRecipient "", MessageRecipientAddress
		newMessage.Body = "Your Message with subject " & oMessage.subject & " was not delivered to some recipients due to your account"
		newMessage.Body = NewMessage.body & "being limited to only allow mail to be sent to users at " & VarDomain
		newMessage.Body = NewMessage.body & vbNewLine & vbNewLine
		newMessage.Body = NewMessage.body & "The following recipients did not recieve your e-mail"
		newMessage.Body = NewMessage.body & RejectedAddress
		newMessage.Save
	End if

End Sub
Edit - changed to ensure that address in any case will be checked as per this thread
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Tezcatlipoca
Senior user
Senior user
Posts: 309
Joined: 2009-07-21 12:33

Re: Local Send Only

Post by Tezcatlipoca » 2010-01-06 13:35

mattg, that is fabulous, thank you :D

I did have one small problem, but managed to locate it. Basically this line:

newMessage.FromAddress = admin@yourdomain.com

should read

newMessage.FromAddress = "admin@yourdomain.com"

Now my users cannot send out to external addresses, and get a warning message when they try! Thank you!

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

Re: Local Send Only

Post by mattg » 2010-01-06 14:02

Tezcatlipoca wrote:mattg, that is fabulous, thank you :D

I did have one small problem, but managed to locate it.
No worries, and I amended the fault you pointed out.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

jluisre
New user
New user
Posts: 1
Joined: 2010-05-03 23:29

Re: Local Send Only

Post by jluisre » 2010-07-01 00:45

mattg wrote:
Tezcatlipoca wrote:mattg, that is fabulous, thank you :D

I did have one small problem, but managed to locate it.
No worries, and I amended the fault you pointed out.

Hi Mattg and Tezcatlipoca
the script works fine, but I have actually another mail server with different domain name and I need to send mail to this domain locally. I create an smtp route and before the script I was send smtp mail perfectly, but now, I cannot, because the script allow only one domain name...or not?
Please can you help me for this case?
Thanks in advance!

Bill48105
Developer
Developer
Posts: 6189
Joined: 2010-04-24 23:16
Location: Michigan, USA

Re: Local Send Only

Post by Bill48105 » 2010-07-01 01:02

Hi,
It's not my script but 'simple' fix would be to have the meat of the script in select/case statements for each domain but seems the 'proper' fix would be to make varDomain an array & modify parts of script as needed to loop thru (or check if a value is in the array).. Then again perhaps an even simpler 'proper' fix would be to to put the script in your own function, calling it in onDeliverMessage with the domain as a passed in the function call.. There's the winner!
Bill
hMailServer build LIVE on my servers: 5.4-B2014050402
#hmailserver on FreeNode IRC https://webchat.freenode.net/?channels=#hmailserver
*** ABSENT FROM hMail! Those in IRC know how to find me if urgent. ***

edstevens
New user
New user
Posts: 1
Joined: 2013-01-09 08:28

Re: Local Send Only

Post by edstevens » 2013-01-09 08:33

Hi all

Thanks for the script. It was quite useful for me.

But, there is another thing. When restricted user send an e-mail, all 'CC/BCC' will changed to 'To'

Is there anyway to change the script?

Thanks

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

Re: Local Send Only

Post by mattg » 2013-01-09 10:27

Sure,

You would need to get all recipients as it currently does, and also find out which of these recipients are TO and CC recipients, and when adding them back, add them back as they were previously.
All additional recipients should be BCC recipients.

Feel free to make the changes you need, and please post here if you need help and to post the results of your work so that others can use it.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ssabbath
New user
New user
Posts: 9
Joined: 2014-07-22 16:34

Re: Local Send Only

Post by ssabbath » 2014-07-23 22:49

Hi mattg, so if i want to make 2 domains to "talk" to each other i need a second varDomain correct?

Btw, only one domain is hosted on this hmailserver, the other one is "far far away" lol...

I don´t know shi*t about programing and stuff... :P lol, so maybe i will need more help than usual! :P

Like this?!

Code: Select all

Sub OnDeliverMessage(oMessage)

	Dim LocalOnlyAccounts(100)
	Dim temprecipients(100)
	Dim i, j, k, l
	Dim varDomain
	Dim varDomain2
	Dim varAddress
	Dim RejectedAddress

	varDomain = "domain.com"   						'CHANGE ME
	varDomain2 = "domain2.com"

	LocalOnlyAccounts(0)= "user1" & "@" & varDomain		'CHANGE ME
	LocalOnlyAccounts(1)= "user2" & "@" & varDomain		'CHANGE ME
	LocalOnlyAccounts(1)= "user1" & "@" & varDomain2		'CHANGE ME
	LocalOnlyAccounts(1)= "user2" & "@" & varDomain2		'CHANGE ME

	RejectedAddress = ""

	For i = 0 to uBound(LocalOnlyAccounts)
		If LCase(oMessage.FromAddress) = LocalOnlyAccounts(i) then 'sender is restricted account
			for j = 0 to oMessage.recipients.count -1
				temprecipients(j) = oMessage.recipients.item(j).address
			next 'j
			j = 0
			k = oMessage.recipients.count -1
			while j <= k
				If instr(temprecipients(j),"@"& VarDomain, varDomain2) = 0 then 'remove non-local recipients
					l= j					
					RejectedAddress = RejectedAddress & temprecipients(l) & VBNewLine
					while l < k 
						temprecipients(l) = temprecipients(l+1)
						l=l+1
					wend
					temprecipients(k) = ""
					k=k-1
				else
					j=j+1						
				end if
			wend			
			oMessage.clearrecipients()
			For j = 0 to k
				varAddress = temprecipients(j)
				oMessage.AddRecipient "", varAddress
			Next 'j
			oMessage.save

		End if
	Next 'i

	If RejectedAddress <> "" then 'we found some invalid recipients
		dim oApp, newMessage
		dim MessageSubject
		dim MessageRecipientAddress

		Set oApp = CreateObject("hMailServer.Application")

		' Give this script permission to access all
		' hMailServer settings.
		Call oApp.Authenticate("Administrator", "AdminPassword")  		'CHANGE ME

		MessageRecipientAddress = oMessage.FromAddress
		MessageSubject = "Automatically rejected mail"

		Set newMessage = CreateObject("hMailServer.Message")
		newMessage.From = "Automated System Mail <admin@yourdomain.com>"		'CHANGE ME
		newMessage.FromAddress = "admin@yourdomain.com"				'CHANGE ME
		newMessage.Subject = MessageSubject
		newMessage.AddRecipient "", MessageRecipientAddress
		newMessage.Body = "Your Message with subject " & oMessage.subject & " was not delivered to some recipients due to your account"
		newMessage.Body = NewMessage.body & "being limited to only allow mail to be sent to users at " & VarDomain varDomain2
		newMessage.Body = NewMessage.body & vbNewLine & vbNewLine
		newMessage.Body = NewMessage.body & "The following recipients did not recieve your e-mail"
		newMessage.Body = NewMessage.body & RejectedAddress
		newMessage.Save
	End if

End Sub
Lol sorry, i´m so lame when it comes to coding :P

ssabbath
New user
New user
Posts: 9
Joined: 2014-07-22 16:34

Re: Local Send Only

Post by ssabbath » 2014-07-25 15:19

Anyone? :)

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

Re: Local Send Only

Post by mattg » 2014-07-26 06:12

ssabbath wrote:I don´t know shi*t about programing and stuff... :P lol, so maybe i will need more help than usual! :
That's apparent

Not many people here will write VB code for just because you can't

There is ONLY one line you need to change, and it is this one (It tests who the message is for)

Code: Select all

            If instr(temprecipients(j),"@"& VarDomain) = 0 then 'remove non-local recipients
It could be something like

Code: Select all

            If instr(temprecipients(j),"@"& VarDomain) + instr(temprecipients(j),"@"& "domain.com")  = 0 then 'remove non-allowed recipients
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ssabbath
New user
New user
Posts: 9
Joined: 2014-07-22 16:34

Re: Local Send Only

Post by ssabbath » 2014-07-31 18:05

Ok. Thanks. Will try that out and understand a little more.

Will try to read and learn some VB in the next days!

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-17 17:34

HI,mattg
with your code

oMessage.clearrecipients()
For j = 0 to k
varAddress = temprecipients(j)
oMessage.AddRecipient "", varAddress
Next 'j
oMessage.save


question:
if temprecipients(j)=nothing (all recipients are not local users),so your code will be like this:

oMessage.clearrecipients()
oMessage.save

is that ok?


if temprecipients=nothing, I think it would be better to delete omessage directly

but i don't know how to delete the omessage on (Sub OnDeliverMessage(oMessage)) directly,please help me ,thanks

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

Re: Local Send Only

Post by mattg » 2014-08-18 01:13

happylcg wrote:is that ok?
I think so

That's the point of the script isn't it? to not allow sending to External recipients.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-18 02:42

thanks

So you would say that call all recipients equal delete the message directly?

i wish i know the Omessage has been clear call recipients,but it will be saved on the server still?

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

Re: Local Send Only

Post by mattg » 2014-08-18 04:20

Yes I think the message gets dropped and an error is logged (No remaining recipients)
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-20 05:30

thanks

I make some tests ,but i don't understand the tests result


test1:

Code: Select all

  Sub OnAcceptMessage(oClient, oMessage)
        call ExcuteSql("insert into Mtable (record) values (" recorded") )     'this method used for record how many times on OnAcceptMessage when send an email
        oMessage.clearrecipients
   End Sub

the result shows'

1.Mysql added one record
2.hmailserver\logs\ERROR_hmailserver receive the Error information like " No remaining recipients " (just as you said)

i can understand this test result ...but test2……


'----------------------------------
test2:

Code: Select all

  Sub OnDeliverMessage(oMessage)
        call ExcuteSql("insert into Mtable (record) values (" recorded") )     'this method used for record how many times on OnDeliverMessage when send an email
        oMessage.clearrecipients
   End Sub

the result shows:

1.Mysql added many records ,no endless. I have to disable script to stop it add more records to mysql
2.hmailserver\logs\ERROR_hmailserver ...no any error info

i don't understand the result :no error infomation ,and method "ExcuteSql" excute many times ,it means "OnDeliverMessage" excute manys times after "oMessage.clearrecipients " ,why ?

is it while "oMessage.clearrecipients" ,Hmailserver send error info by mail to sender? so excute the method of "OnDeliverMessage" again?

if yes ,mail sender will receive some error info,so i change script like this:

Code: Select all

Sub OnDeliverMessage(oMessage)
          

      set RsqlInfo=Rsql("select * from Mtable where record='recorded'")      ' this function "Rsql" is used for get the records from mysql
    
       if   RsqlInfo.Eof then       'have no record on mysql ,it means  this is the first time to record.  if not the first time ,do not clear recipients.
            oMessage.clearrecipients
       end if
      
       call ExcuteSql("insert into Mtable (record) values (" recorded")      'this method used for record how many times on OnDeliverMessage when send an email

end sub


(is it while oMessage.clearrecipients,Hmailserver send error info to sender? so excute the method of "OnDeliverMessage" again?)
when i change to script (do not clear recipients after first time ),but i don't receive any error mail from hmailserver also..

by the way ,after i change the script . mysql only have two records.. no "Mysql added many records ,no endless "

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

Re: Local Send Only

Post by mattg » 2014-08-20 07:57

happylcg wrote: i don't understand the result :no error infomation ,and method "ExcuteSql" excute many times ,it means "OnDeliverMessage" excute manys times after "oMessage.clearrecipients " ,why ?
because you need to add
oMessage.save

or the message won't be saved after you clear the recipients, ie the message will still have all recipients
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-20 10:22

ok

but i have new doubts

Code: Select all

test1:
Sub OnAcceptMessage(oClient, oMessage)

    oMessage.ClearRecipients 

 End Sub

Code: Select all

test2:
 Sub OnDeliverMessage(oMessage)

oMessage.ClearRecipients
    	
End Sub

obove 2 method...

just you said,if did not add omessage.save,,,the message will not clear the recipients finally...

so ,i should receive the email...
but acutally ,i did not receive the mail after add "oMessage.ClearRecipients" (but no omessage.save)

why?

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

Re: Local Send Only

Post by mattg » 2014-08-20 11:28

happylcg wrote:i don't understand the result :no error infomation ,and method "ExcuteSql" excute many times ,it means "OnDeliverMessage" excute manys times after "oMessage.clearrecipients " ,why ?
happylcg wrote:just you said,if did not add omessage.save,,,the message will not clear the recipients finally...

so ,i should receive the email...
but acutally ,i did not receive the mail after add "oMessage.ClearRecipients" (but no omessage.save)
OK I'm confused

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

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-20 15:55

sorry,my english is bad


i explain my question again


test:

Code: Select all

Sub OnDeliverMessage(oMessage)

    call method1 'method1 is add one record to mysql    (only add one record everytime)
    oMessage.clearrecipients
       
End Sub
after i send an mail,i check mysql data,i find mysql had added many records.i don't understand.
i just send an email..i think it only add two records to mysql (because mirror is enable)

and i did not receive any error infomation..but you said if use "oMessage.clearrecipients",i should be receive some error info like " No remaining recipients "


And then you told me i should add omessage.save

"or the message won't be saved after you clear the recipients, ie the message will still have all recipients"

the question:

because i did not add omessage.save, so ,as you said,the message will still have all recipients....it means all recipients can receive the mail

but actually,none recipient receive the mail..

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

Re: Local Send Only

Post by percepts » 2014-08-20 16:19

if you script generated script error message may not be delivered. Did script generate errors? Check your logs

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-21 03:00

NO,i had checked the logs ,no any error info

and mail did not recevie any wrong msg



if i use the same code on method of "onacceptmssage",logs file have one wrong msg...

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

Re: Local Send Only

Post by mattg » 2014-08-21 03:57

happylcg wrote:and i did not receive any error infomation..but you said if use "oMessage.clearrecipients",i should be receive some error info like " No remaining recipients "
Depends on where that is called.
From OnDeliverMessages, I'd say that you wouldn't get that error, as the message has already been delivered.
happylcg wrote:And then you told me i should add omessage.save

"or the message won't be saved after you clear the recipients, ie the message will still have all recipients"
Again, same answer, depends on where it is being called.

And that was because you were saying that you were getting unending amount of these being triggered.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

happylcg
New user
New user
Posts: 23
Joined: 2014-08-11 14:12

Re: Local Send Only

Post by happylcg » 2014-08-21 07:20

thank you very much!!

I'm not sure exactly what you mean,but i still don't very clear.
and i found that how to stop mysql get endless msg.i need to disable mirror

the code and test result as bellow:

Code: Select all

   Sub OnDeliverMessage(oMessage)

    call excute(sender,recipient,id)  'this method is add oMessage info to  mysql 
    oMessage.clearrecipients
     oMessage.save  'i find add this line or not ,the result is the same  
   End Sub

Test

fromaddress from@yahoo.com
recipient r@sameple.com

TEST1: enable mirror ,mirror address is backupm@sameple.com

mysql data shows the result like this (endless records)

row ...... ... sender..............................recipient ....................................... omessageid
1................. from@yahoo.com..................r@sameple.com...............................10067
2................. from@yahoo.com.................backupm@sameple.com......................10068
........................................................................................................

x................. from@yahoo.com..................backupm@sameple.com......................xxxxx
x................. from@yahoo.com..................backupm@sameple.com......................xxxxx
………………………………bellow all the same………………………………


TEST2.disable mirror

mysql data shows the result like this (just one record)

sender..............................recipient ....................................... omessageid

from@yahoo.com..................r@sameple.com...............................10067



so ,it is very clear ,mysql add endless record ,because i enable the mirror
but why ?

now i can avoid this problem by add one line "if oMessage.Recipients.Item(0).Address<>mirror@sameple.com"

but i still don't understand why if enable mirror it will like that ……

gnoppix
New user
New user
Posts: 21
Joined: 2015-10-26 07:27

Re: Local Send Only

Post by gnoppix » 2016-09-21 13:49

Tezcatlipoca wrote:***UPDATE*** Ah ha! I restarted hMail, made sure the script was there, Enabled and Reloaded then tried again and it looks like it's working fine!

My test user can currently email to @mydomain.com accounts, which is correct, but an email to my external gMail account still isn't as having as having arrived, which was sent about 10 minutes ago! :D

mattg, you are an absolute star, sir!

I did initially want my users to get a message back saying that they couldn't email external accounts when they tried, but I'm guessing I can do this on a user by user basis via rules? Or is it fairly simple to add something to this script that says it?

What if you have 5 domain to check ? Say my.domain1, my.domain2.... etc ?

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

Re: Local Send Only

Post by mattg » 2016-09-22 00:20

As local domains that are OK to send to each other??
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

sathya8561
New user
New user
Posts: 2
Joined: 2019-05-06 11:33

Re: Local Send Only

Post by sathya8561 » 2019-05-06 11:41

edstevens wrote:
2013-01-09 08:33
Hi all

Thanks for the script. It was quite useful for me.

But, there is another thing. When restricted user send an e-mail, all 'CC/BCC' will changed to 'To'

Is there anyway to change the script?

Thanks

i have same problem pls help When restricted user send an e-mail, all 'CC/BCC' will changed to 'To'

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

Re: Local Send Only

Post by mattg » 2019-05-07 02:07

sathya8561 wrote:
2019-05-06 11:41
edstevens wrote:
2013-01-09 08:33
Hi all

Thanks for the script. It was quite useful for me.

But, there is another thing. When restricted user send an e-mail, all 'CC/BCC' will changed to 'To'

Is there anyway to change the script?

Thanks

i have same problem pls help When restricted user send an e-mail, all 'CC/BCC' will changed to 'To'
Same answer applies
mattg wrote:
2013-01-09 10:27
Sure,

You would need to get all recipients as it currently does, and also find out which of these recipients are TO and CC recipients, and when adding them back, add them back as they were previously.
All additional recipients should be BCC recipients.

Feel free to make the changes you need, and please post here if you need help and to post the results of your work so that others can use it.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

sathya8561
New user
New user
Posts: 2
Joined: 2019-05-06 11:33

Re: Local Send Only

Post by sathya8561 » 2019-05-07 08:02

bro sorry i didn't get what you said i humbly request you kindly explain clearly is there any script pls guid me

Post Reply