Rules create copy and send, sending slow

Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.
michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Rules create copy and send, sending slow

Post by michalhana99 » 2021-03-22 23:36

Hi all,
I have created a rule to create copy forwarding based on the number of retransmissions.
After each delivery, the subject of the email changes and then the e-mail is received by the SMS gateway, which sends the SMS to the set number in the subject of the email.
There are about 30 such rules.

The problem is that delivery of the created copy always takes a minute and is not delivered immediately

This is a big problem for me, because it is an SMS for firefighters with specific information about the intervention.

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-03-23 01:25

michalhana99 wrote:
2021-03-22 23:36
Hi all,
I have created a rule to create copy forwarding based on the number of retransmissions.
After each delivery, the subject of the email changes and then the e-mail is received by the SMS gateway, which sends the SMS to the set number in the subject of the email.
There are about 30 such rules.

The problem is that delivery of the created copy always takes a minute and is not delivered immediately

This is a big problem for me, because it is an SMS for firefighters with specific information about the intervention.
You do know that SMS was never intended to be real-time.
Actually my old NEC TR5E1000 - 7A phone from 1986 could do DMS ... or NMT-text as it also was called. That was really cool but it could sometimes take up to 10 minutes to come through. Messages were short spaced so we had to move on from 1337 to "Txt-Spk" for mere mortals.

Anyways, back to your problem...
7h3 0n3 m1nu73 d314y y0u 3xp3213nc3 15 7h3 d3f4u17 qu3u3 71m3 1n hm411532v32.
SørenR.

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

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-03-23 01:45

This has been covered extensively on other threads. The delay is there by design. One poster did have success scripting a function to create a new "clone" message and submit it, which does get sent immediately:

viewtopic.php?f=7&t=36017&p=226758&hili ... ay#p226758

IMO, @SorenR is correct, and you are likely to encounter delays in the system outside your control.

If you need SLO/SLA based delivery, and not best-effort/as-available service, you need to look into a carrier-specific SMS service for "enterprise paging" (AT&T actually calls it such: http://www.attdircontracts.com/Mobility ... Paging.pdf) and submit your pages using either SNPP, WCTP, or TAP, which are protocols specifically for the type of application you are describing.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-03-24 14:01

michalhana99 wrote:
2021-03-22 23:36
Hi all,
I have created a rule to create copy forwarding based on the number of retransmissions.
After each delivery, the subject of the email changes and then the e-mail is received by the SMS gateway, which sends the SMS to the set number in the subject of the email.
There are about 30 such rules.

The problem is that delivery of the created copy always takes a minute and is not delivered immediately

This is a big problem for me, because it is an SMS for firefighters with specific information about the intervention.
Why don't you just extract what you want from the incoming message and send it directly to the SMS gateway? Then there's no delay at all.

Do you manage the SMS gateway?

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-03-24 14:09

SorenR wrote:
2021-03-23 01:25

You do know that SMS was never intended to be real-time.
Actually my old NEC TR5E1000 - 7A phone from 1986 could do DMS ... or NMT-text as it also was called. That was really cool but it could sometimes take up to 10 minutes to come through. Messages were short spaced so we had to move on from 1337 to "Txt-Spk" for mere mortals.
In current year we use multipart messages so you can send long SMS in actual [insert language].

Anyways, back to your problem...
7h3 0n3 m1nu73 d314y y0u 3xp3213nc3 15 7h3 d3f4u17 qu3u3 71m3 1n hm411532v32.
:lol:

I imagine a couple of creative telegraph operators came up with that long before we did. :mrgreen:

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-03-24 16:12

palinka wrote:
2021-03-24 14:09
Anyways, back to your problem...
7h3 0n3 m1nu73 d314y y0u 3xp3213nc3 15 7h3 d3f4u17 qu3u3 71m3 1n hm411532v32.
:lol:

I imagine a couple of creative telegraph operators came up with that long before we did. :mrgreen:
Nope ... It was born on the Interweb for the elite ... 31337 ... 8)

People using leet-speak was mostly multilingual (fluent in more than one programming language) and quite skilled at logic ... AKA Hackers, crackers or Programmers. 8)
SørenR.

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

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 09:59

mikedibella wrote:
2021-03-23 01:45
This has been covered extensively on other threads. The delay is there by design. One poster did have success scripting a function to create a new "clone" message and submit it, which does get sent immediately:

viewtopic.php?f=7&t=36017&p=226758&hili ... ay#p226758

IMO, @SorenR is correct, and you are likely to encounter delays in the system outside your control.

If you need SLO/SLA based delivery, and not best-effort/as-available service, you need to look into a carrier-specific SMS service for "enterprise paging" (AT&T actually calls it such: http://www.attdircontracts.com/Mobility ... Paging.pdf) and submit your pages using either SNPP, WCTP, or TAP, which are protocols specifically for the type of application you are describing.
I'm sorry, but I only got to it now
when I use the code

Code: Select all

Function CloneMail(oMessage, strRecipient, strAddress, strHeader)
    '
    '   CloneMail(oMessage, "Wile E. Coyote", "wile.e.coyote@acme.inc", "X-hMailServer-Cloned")
    '
    Dim strFilename
    If oMessage.HeaderValue(strHeader) = "YES" Then Exit Function
    With CreateObject("hMailServer.Message")
        strFilename = .Filename
        With CreateObject("Scripting.FileSystemObject")
            .CopyFile oMessage.Filename, strFilename, True
        End With
        .RefreshContent
        .AddRecipient strRecipient, strAddress
        .HeaderValue(strHeader) = "YES"
        .Save
    End With
End Function
I will get back the error message Script Error: Source: Microsoft VBScript - error p"DEBUG" 11852 "2021-04-30 09:44:54.229" "Event completed"

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-04-30 10:18

michalhana99 wrote:
2021-04-30 09:59
mikedibella wrote:
2021-03-23 01:45
This has been covered extensively on other threads. The delay is there by design. One poster did have success scripting a function to create a new "clone" message and submit it, which does get sent immediately:

viewtopic.php?f=7&t=36017&p=226758&hili ... ay#p226758

IMO, @SorenR is correct, and you are likely to encounter delays in the system outside your control.

If you need SLO/SLA based delivery, and not best-effort/as-available service, you need to look into a carrier-specific SMS service for "enterprise paging" (AT&T actually calls it such: http://www.attdircontracts.com/Mobility ... Paging.pdf) and submit your pages using either SNPP, WCTP, or TAP, which are protocols specifically for the type of application you are describing.
I'm sorry, but I only got to it now
when I use the code

Code: Select all

Function CloneMail(oMessage, strRecipient, strAddress, strHeader)
    '
    '   CloneMail(oMessage, "Wile E. Coyote", "wile.e.coyote@acme.inc", "X-hMailServer-Cloned")
    '
    Dim strFilename
    If oMessage.HeaderValue(strHeader) = "YES" Then Exit Function
    With CreateObject("hMailServer.Message")
        strFilename = .Filename
        With CreateObject("Scripting.FileSystemObject")
            .CopyFile oMessage.Filename, strFilename, True
        End With
        .RefreshContent
        .AddRecipient strRecipient, strAddress
        .HeaderValue(strHeader) = "YES"
        .Save
    End With
End Function
I will get back the error message Script Error: Source: Microsoft VBScript - error p"DEBUG" 11852 "2021-04-30 09:44:54.229" "Event completed"
How do you call this function?
SørenR.

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

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 10:27

SorenR wrote:
2021-04-30 10:18
michalhana99 wrote:
2021-04-30 09:59
mikedibella wrote:
2021-03-23 01:45
This has been covered extensively on other threads. The delay is there by design. One poster did have success scripting a function to create a new "clone" message and submit it, which does get sent immediately:

viewtopic.php?f=7&t=36017&p=226758&hili ... ay#p226758

IMO, @SorenR is correct, and you are likely to encounter delays in the system outside your control.

If you need SLO/SLA based delivery, and not best-effort/as-available service, you need to look into a carrier-specific SMS service for "enterprise paging" (AT&T actually calls it such: http://www.attdircontracts.com/Mobility ... Paging.pdf) and submit your pages using either SNPP, WCTP, or TAP, which are protocols specifically for the type of application you are describing.
I'm sorry, but I only got to it now
when I use the code

Code: Select all

Function CloneMail(oMessage, strRecipient, strAddress, strHeader)
    '
    '   CloneMail(oMessage, "Wile E. Coyote", "wile.e.coyote@acme.inc", "X-hMailServer-Cloned")
    '
    Dim strFilename
    If oMessage.HeaderValue(strHeader) = "YES" Then Exit Function
    With CreateObject("hMailServer.Message")
        strFilename = .Filename
        With CreateObject("Scripting.FileSystemObject")
            .CopyFile oMessage.Filename, strFilename, True
        End With
        .RefreshContent
        .AddRecipient strRecipient, strAddress
        .HeaderValue(strHeader) = "YES"
        .Save
    End With
End Function
I will get back the error message Script Error: Source: Microsoft VBScript - error p"DEBUG" 11852 "2021-04-30 09:44:54.229" "Event completed"
How do you call this function?
I am a complete beginner in this
Rules->RUN fUNCTION->CloneMail

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-04-30 10:43

michalhana99 wrote:
2021-04-30 10:27
SorenR wrote:
2021-04-30 10:18

How do you call this function?
I am a complete beginner in this
Rules->RUN fUNCTION->CloneMail
This function does not support that. This function require 4 parameters and running a function from a rule only passes 1 parameter; the message object.

What exactly are you trying to accomplish?
SørenR.

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

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 11:28

SorenR wrote:
2021-04-30 10:43
michalhana99 wrote:
2021-04-30 10:27
SorenR wrote:
2021-04-30 10:18

How do you call this function?
I am a complete beginner in this
Rules->RUN fUNCTION->CloneMail
This function does not support that. This function require 4 parameters and running a function from a rule only passes 1 parameter; the message object.

What exactly are you trying to accomplish?
I have created a rule to create copy forwarding based on the number of retransmissions.
The problem is that delivery of the created copy always takes a minute and is not delivered immediately

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-04-30 13:04

michalhana99 wrote:
2021-04-30 11:28
SorenR wrote:
2021-04-30 10:43
michalhana99 wrote:
2021-04-30 10:27


I am a complete beginner in this
Rules->RUN fUNCTION->CloneMail
This function does not support that. This function require 4 parameters and running a function from a rule only passes 1 parameter; the message object.

What exactly are you trying to accomplish?
I have created a rule to create copy forwarding based on the number of retransmissions.
The problem is that delivery of the created copy always takes a minute and is not delivered immediately
Try this. Call with the same rule.

Code: Select all

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress, SMSSubject

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSSubject          = "1234567890"                     '<-- OP says SMS number comes from subject line
	
	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = SMSSubject
		.Body = oMessage.Body
		.Save
	End With
End Sub

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 13:40

palinka wrote:
2021-04-30 13:04
michalhana99 wrote:
2021-04-30 11:28
SorenR wrote:
2021-04-30 10:43


This function does not support that. This function require 4 parameters and running a function from a rule only passes 1 parameter; the message object.

What exactly are you trying to accomplish?
I have created a rule to create copy forwarding based on the number of retransmissions.
The problem is that delivery of the created copy always takes a minute and is not delivered immediately
Try this. Call with the same rule.

Code: Select all

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress, SMSSubject

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSSubject          = "1234567890"                     '<-- OP says SMS number comes from subject line
	
	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = SMSSubject
		.Body = oMessage.Body
		.Save
	End With
End Sub
Great! this works :-)

But I still need the value in the email header X-hMailServer-LoopCount

Thansk :-)

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-04-30 14:14

michalhana99 wrote:
2021-04-30 13:40
But I still need the value in the email header X-hMailServer-LoopCount
That should be part of your rule.

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 14:23

palinka wrote:
2021-04-30 14:14
michalhana99 wrote:
2021-04-30 13:40
But I still need the value in the email header X-hMailServer-LoopCount
That should be part of your rule.
I suppose, that
.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
overwrites the header
it would be best if the entire original header was retained

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-04-30 15:12

michalhana99 wrote:
2021-04-30 14:23
I suppose, that
.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
overwrites the header
it would be best if the entire original header was retained
The function creates a completely new message. Nothing from the original message is retained except the message body. This is the workaround to avoid the 60 second forwarding delay.

What information do you want from the headers?

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 15:55

palinka wrote:
2021-04-30 15:12
michalhana99 wrote:
2021-04-30 14:23
I suppose, that
.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
overwrites the header
it would be best if the entire original header was retained
The function creates a completely new message. Nothing from the original message is retained except the message body. This is the workaround to avoid the 60 second forwarding delay.

What information do you want from the headers?
I already have predefined rules directly on the hmail server. The header is important for identifying recipients.
Below is a picture with the settings
Image

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-04-30 19:32

michalhana99 wrote:
2021-04-30 15:55
I already have predefined rules directly on the hmail server. The header is important for identifying recipients.
Below is a picture with the settings
If the subject already contains the SMS number, then just use it. You can get it from the oMessage object.

Code: Select all

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	
	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = oMessage.Body
		.Save
	End With
End Sub
Also, your rule should be x-hmailserver-loopcount < 1 and you no longer need action "create and send copy" because that's what the function does except without the 60 second delay. :D

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-04-30 21:14

palinka wrote:
2021-04-30 19:32
michalhana99 wrote:
2021-04-30 15:55
I already have predefined rules directly on the hmail server. The header is important for identifying recipients.
Below is a picture with the settings
If the subject already contains the SMS number, then just use it. You can get it from the oMessage object.

Code: Select all

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	
	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = oMessage.Body
		.Save
	End With
End Sub
Also, your rule should be x-hmailserver-loopcount < 1 and you no longer need action "create and send copy" because that's what the function does except without the 60 second delay. :D
Thanks But in the email header byvh needed to keep at least its own parameter "Unit = X" added in using hmail server

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-04-30 23:38

Can you please spell out exactly how your system works? Sequence of events. I don't understand what your headers are/do/why you need them.

What is header "byvh"? What does "Unit = x" represent?

You can access header values with oMessage.HeaderValue("your-header-name")

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-01 13:34

palinka wrote:
2021-04-30 23:38
Can you please spell out exactly how your system works? Sequence of events. I don't understand what your headers are/do/why you need them.

What is header "byvh"? What does "Unit = x" represent?

You can access header values with oMessage.HeaderValue("your-header-name")
Sorry, bad translation :-D
Finally, I used the first variant with creating a script for each number separately

I still want to ask if to improve in oMessage.Body delete or replace part of a certain text eg with the command:
response.write(Replace(txt,"Red","Blue"))

Thanks :-)

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-01 14:23

.Body = Replace(oMessage.Body, "find", "replace")

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-01 17:33

palinka wrote:
2021-05-01 14:23
.Body = Replace(oMessage.Body, "find", "replace")
Thanks.
one more question, I'm worried for an hour and I can't figure it out

I need in oMessage.Body change text (your code its ok :-) ) and delete blank lines

Thanks :-)

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-01 19:42

michalhana99 wrote:
2021-05-01 17:33
palinka wrote:
2021-05-01 14:23
.Body = Replace(oMessage.Body, "find", "replace")
Thanks.
one more question, I'm worried for an hour and I can't figure it out

I need in oMessage.Body change text (your code its ok :-) ) and delete blank lines

Thanks :-)
I know exactly what you want. I do the same thing with my email2sms. I run a gammu SMS gateway. Email =/= SMS. Line breaks, blank spaces and html are an issue to deal with.

You can only find/replace new lines using regex, so you need a function for that.

Code: Select all

REM - oLookup function courtesy SorenR
Function oLookup(strRegEx, strMatch, bGlobal)
	If strRegEx = "" Then strRegEx = StrReverse(strMatch)
	With CreateObject("VBScript.RegExp")
		.Pattern = strRegEx
		.Global = bGlobal
		.MultiLine = True
		.IgnoreCase = True
		Set oLookup = .Execute(strMatch)
	End With
End Function

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress, SMSMessage, strRegEx, Match, Matches

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSMessage = oMessage.Body
	
	REM - replace line breaks with spaces
	strRegEx = "(\r\n|\r|\n)"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next
	
	REM - replace double/multiple spaces
	strRegEx = "([\s]{2,})"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next

	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = SMSMessage
		.Save
	End With
End Sub
Because I get messages containing anything, I also remove html elements. But its a LOT cleaner if your email messages are plain text to start with.

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-10 11:22

palinka wrote:
2021-05-01 19:42
michalhana99 wrote:
2021-05-01 17:33
palinka wrote:
2021-05-01 14:23
.Body = Replace(oMessage.Body, "find", "replace")
Thanks.
one more question, I'm worried for an hour and I can't figure it out

I need in oMessage.Body change text (your code its ok :-) ) and delete blank lines

Thanks :-)
I know exactly what you want. I do the same thing with my email2sms. I run a gammu SMS gateway. Email =/= SMS. Line breaks, blank spaces and html are an issue to deal with.

You can only find/replace new lines using regex, so you need a function for that.

Code: Select all

REM - oLookup function courtesy SorenR
Function oLookup(strRegEx, strMatch, bGlobal)
	If strRegEx = "" Then strRegEx = StrReverse(strMatch)
	With CreateObject("VBScript.RegExp")
		.Pattern = strRegEx
		.Global = bGlobal
		.MultiLine = True
		.IgnoreCase = True
		Set oLookup = .Execute(strMatch)
	End With
End Function

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress, SMSMessage, strRegEx, Match, Matches

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSMessage = oMessage.Body
	
	REM - replace line breaks with spaces
	strRegEx = "(\r\n|\r|\n)"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next
	
	REM - replace double/multiple spaces
	strRegEx = "([\s]{2,})"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next

	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = SMSMessage
		.Save
	End With
End Sub
Because I get messages containing anything, I also remove html elements. But its a LOT cleaner if your email messages are plain text to start with.
Thank you, I still have one problem. This is how the original email arrives, and the email forwarding feature becomes blank
Attachments
Screenshot_2021-05-10-11-17-31-835_me.bluemail.mail.jpg

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-10 19:08

You are setting the clone message's plaintext body (.Body) to the plaintext body of the source message. A better approach is to write your code to detect if there is a plaintext part, an HTML part(.HTMLBody), or both, and set one or both of the close properties with the contents of the source message parts.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-11 12:41

michalhana99 wrote:
2021-05-10 11:22
palinka wrote:
2021-05-01 19:42

Code: Select all

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'
Thank you, I still have one problem. This is how the original email arrives, and the email forwarding feature becomes blank
Screenshot 2021-05-11 062251.png
Can you post the entire eml message? Delete sensitive content if you want.

oMessage can access .Body and .HTMLBody but removing html tags is not a simple task. It can be if you know in advance what to expect.

In general, its not a great idea to try to forward html messages to SMS. If you don't get rid of the html tags properly you'll get a mess of an unreadable, long SMS message. I only forward messages to SMS that I know are short and in plain text, such as event reminders.

It would be a lot simpler and easier if you set up the originating software to send plain text messages, or at least include a plain text part to the message.

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-11 16:33

palinka wrote:
2021-05-11 12:41
michalhana99 wrote:
2021-05-10 11:22
palinka wrote:
2021-05-01 19:42

Code: Select all

Sub CloneMail(oMessage)
	'
	' WARNING - incoming message must be plain text
	'
Thank you, I still have one problem. This is how the original email arrives, and the email forwarding feature becomes blank
Screenshot 2021-05-11 062251.png

Can you post the entire eml message? Delete sensitive content if you want.

oMessage can access .Body and .HTMLBody but removing html tags is not a simple task. It can be if you know in advance what to expect.

In general, its not a great idea to try to forward html messages to SMS. If you don't get rid of the html tags properly you'll get a mess of an unreadable, long SMS message. I only forward messages to SMS that I know are short and in plain text, such as event reminders.

It would be a lot simpler and easier if you set up the originating software to send plain text messages, or at least include a plain text part to the message.
I enclose. I can't change the initial message, I don't have access to the sw
Attachments
HNB.rar
(1.74 KiB) Downloaded 5 times

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-11 17:29

michalhana99 wrote:
2021-05-11 16:33
I enclose. I can't change the initial message, I don't have access to the sw
Of this, what do you want to extract as the sms message?

Code: Select all

<html>
<head>
  <title>Událost č.224769</title>
</head>
  <body>
  <div style="font-family: Times New Roman; width: 650px;">
    <strong>Nová Bystřice</strong>
    <b><big id="typ">JINÉ, ZATÍM NEURČENO - TECHNOLOGICKÝ TEST</big></b><br>
    Co se stalo: <b>test</b><br/>
    <hr>
    <i>Adresa události:</i><br>
    <table padding="10px">
<tr><td>GPS:</td><td><b>N 49.021091  E 15.104053 </b></td></tr>
      <tr><td>OKRES:</td><td><b>Jindřichův Hradec</b></td></tr>
      <tr><td>OBEC:</td><td><b>Nová Bystřice</b></td></tr>
      <tr><td>ULICE:</td><td><b>Švermova 278</b></td></tr>
    </table>
    OZNÁMIL: <b>&nbsp; &nbsp; Nová bystřice</b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Telefon: &nbsp; &nbsp; <b>undefined</b><br>
    <hr>
  </div>
  </body>
</html>

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-11 18:21

Can you forward the message to a MMS gateway instead of an SMS gateway? Perhaps consider attaching the the HTML body of the message as an attachment with an HTML MIME type and .html extension, and create an alert to be delivered to the mobile device over MMS. MMS uses the control channel (SMS) to deliver the envelope information, and the data plane (cellular data) for the message content, so you'd still get the message on the device in the short message client.

Once the content has been downloaded, the attachment could be opened on the mobile device using the browser.

Unless the messages you are trying to forward will always look the same, I don't think it will be practical to try to convert the HTML body to plaintext.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-11 20:20

mikedibella wrote:
2021-05-11 18:21
Unless the messages you are trying to forward will always look the same, I don't think it will be practical to try to convert the HTML body to plaintext.
I think they do all look the same. OP mentions something about getting forwarded to firefighters. The message is some kind of notification about location and type of emergency. I guess firemen don't have smartphones.

Code: Select all

Function RemoveHTML(strHTML)
	With CreateObject("VBScript.RegExp")
		.Pattern = "<[^>]+>|&nbsp;|&lt;|&gt;|&quot;|&amp;"
		.Global = True
		.MultiLine = True
		.IgnoreCase = True
		RemoveHTML = .Replace(strHTML, "")
	End With
End Function
Now a really neat trick would be converting the geo coordinates into a google maps link. :mrgreen:

Edit: http://maps.google.com/maps?q=49.021091,15.104053

Seems to work just fine. Even better turn that into a short link. I use yourls for that and I even have a function in eventhandlers.vbs for it.

Code: Select all

Function ShortURL(xSearchString)
	Dim ReturnCode, Json, oGeoip, oXML, strBase, oShortUrl
	Include("C:\hMailServer\Events\VbsJson.vbs")
	Set Json = New VbsJson
	' On Error Resume Next
	Set oXML = CreateObject ("Msxml2.XMLHTTP.3.0")
	oXML.Open "GET", "https://url.mydomain.net/yourls-api.php?signature=API_KEY_GOES_HERE&action=shorturl&format=json&url=https%3A%2F%2Flog.mydomain.net%2Findex.php%3Fsearch%3D" & xSearchString, False
	oXML.Send
	EventLog.Write( "Function ShortURL status        : " & oXML.status )
	Set oShortUrl = Json.Decode(oXML.responseText)
	ReturnCode = oXML.status
	' On Error Goto 0

	If (ReturnCode <> 200 ) Then
		EventLog.Write( "Function ShortURL - ERROR       : Could not create short URL https://log.mydomain.net/index.php?search=" & xSearchString )
		EventLog.Write( "Function ShortURL - RETURN CODE : " & oXML.status )
		EventLog.Write( "Function ShortURL - ERROR MSG   : " & oShortUrl("message") )
	Else
		ShortURL = Mid(oShortUrl("shorturl"),9)
	End If
End Function
This is tailored for my custom log, but changing it to just create a shortlink from geo coordinates is trivial. I actually use this function for SMS notifications to keep the messages short. :mrgreen:

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-11 20:45

palinka wrote:
2021-05-11 20:20
OP mentions something about getting forwarded to firefighters. The message is some kind of notification about location and type of emergency. I guess firemen don't have smartphones.
Well, then this is a frightening solution in it's fragility. Let me reiterate:
mikedibella wrote:
2021-03-23 01:45
If you need SLO/SLA based delivery, and not best-effort/as-available service, you need to look into a carrier-specific SMS service for "enterprise paging" (AT&T actually calls it such: http://www.attdircontracts.com/Mobility ... Paging.pdf) and submit your pages using either SNPP, WCTP, or TAP, which are protocols specifically for the type of application you are describing.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-11 22:46

mikedibella wrote:
2021-05-11 20:45
palinka wrote:
2021-05-11 20:20
OP mentions something about getting forwarded to firefighters. The message is some kind of notification about location and type of emergency. I guess firemen don't have smartphones.
Well, then this is a frightening solution in it's fragility. Let me reiterate:
mikedibella wrote:
2021-03-23 01:45
If you need SLO/SLA based delivery, and not best-effort/as-available service, you need to look into a carrier-specific SMS service for "enterprise paging" (AT&T actually calls it such: http://www.attdircontracts.com/Mobility ... Paging.pdf) and submit your pages using either SNPP, WCTP, or TAP, which are protocols specifically for the type of application you are describing.
¯\_(ツ)_/¯ That's enterprise level stuff. Totally unnecessary for the OPs application. Functionally, there's no difference when only one or 2 messages at a time are being forwarded. If thousands of recipients really needed timely messaging, then you'd have to go with that enterprise stuff. But not for one or 2 message throughput at a time.

My gammu setup has throughput of about 1 message per 3 seconds. Its faster when only sending because outgoing messages get processed together. Its not instantaneous, but my volume - and the OP's - doesn't require any more.

Also, I agree with the fragility thing. But he said he doesn't have access to the source messaging, so he has to play the hand he's dealt. If he wants SMS and its a feature not offered by whoever is providing the source messaging, then what we have started is not really a bad way to do it. After it gets fine tuned, it should work fine. And email (also fragile/not-always-timely) is the backup solution. I'm pretty sure they have radios too. :mrgreen:

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-11 23:00

IMO, he should at least explore a direct-to-SMS API instead of using a carrier SMTP-to-SMS gateway. These are rarely managed to an SLO or SLA and are generally offer "as available, when available". I would not want to be the owner of a burning house relying on mustering at-large firefighters via SMTP-to-SMS gateway.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-11 23:40

To be honest, I'm surprised it's even allowed. I Googled "fire company dispatch" and one of the top results was a manual for Lackawanna County, PA.
Personal Phone Calls
7.7.1 The ECC will not make personal telephone calls for fire units/personnel in the field. This will include hook flashing calls for an individual who calls in on Cellular Phone via 911 lines for purposes of bypassing the Cellular air time charge.
7.8 Personal Paging
7.8.1 The ECC will not page individual fire fighters, except in extreme emergencies and at the discretion of the ECC Supervisor.
This seems more appropriate. Anyway, dispatches go to the fire house, not to individual people except maybe the chief. And radios reach places where cell service is unavailable.

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-12 00:05

In rural America, there are all-volunteer fire departments staffed with trained, but unpaid, responders who are on-call (at-large). There may or may not be staff at the firehouse, but when dispatch is necessary, these at-large responders many not muster at the firehouse. Sometimes only the driver will respond to the firehouse and get the truck, with remaining responders going directly to the scene.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 03:09

mikedibella wrote:
2021-05-12 00:05
In rural America, there are all-volunteer fire departments staffed with trained, but unpaid, responders who are on-call (at-large). There may or may not be staff at the firehouse, but when dispatch is necessary, these at-large responders many not muster at the firehouse. Sometimes only the driver will respond to the firehouse and get the truck, with remaining responders going directly to the scene.
So its 8 or 10 messages instead of 1 or 2?

It could be a couple dozen if one of those hay barns goes up in flames. :lol:

I grew up on a farm and with all the stupidity of my teenage self (fireworks, smoking, guns), I'm actually shocked looking back that I didn't burn the hay barn down myself. By all rights it should have been a smoldering heap in the early 80s and its literally a miracle that it survived. :mrgreen:

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-12 03:17

Well, I'm using an analogy from my youth growing up in then-rural New Jersey to speculate on the circumstances surrounding the OP's use case. The OP's example email has .cz as the TLD, which is geographically far from North America, but that doesn't necessarily invalidate the speculation.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 04:17

mikedibella wrote:
2021-05-12 03:17
Well, I'm using an analogy from my youth growing up in then-rural New Jersey to speculate on the circumstances surrounding the OP's use case. The OP's example email has .cz as the TLD, which is geographically far from North America, but that doesn't necessarily invalidate the speculation.
Sure, but no matter what there's one thing that apparently can't be overcome: no access to / influence over dispatch messaging programming (so no direct SMS at the source).

Also, SMS also appears to be a brand new feature since a) there doesn't seem to be any critical, time-sensitive requirement to roll it out (its still not done) and b) whatever system was in place before is still in place. And that was/is email. We don't even know if the emails are pushed or not, but I'd guess not because the OP is considering SMS as a supplement/improvement.

I believe these dispatch messages are all formatted exactly the same, so removing html and/or extracting important/useful information from them should be a snap.

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-12 05:33

Here's my donation to the cause. I use this function in my HTML-scraping routines:

Code: Select all

function Between(sString, sFrom, sTo)
	if InStr(sString, sFrom) > 0 then
		sTemp = Mid(sString, InStr(sString, sFrom) + Len(sFrom))
		if InStr(sTemp, sTo) > 0 then
			Between = Mid(sTemp, 1, InStr(sTemp, sTo) - 1)
		end if
	end if
end function
i.e.:

Code: Select all

sOKRES = Between(oMessage.HTMLBody, "<tr><td>OKRES:</td><td><b>", "</b></td></tr>")
sOBEC = Between(oMessage.HTMLBody, "<tr><td>OBEC:</td><td><b>", "</b></td></tr>")
sULICE = Between(oMessage.HTMLBody, "<tr><td>ULICE:</td><td><b>", "</b></td></tr>")

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-12 05:39

Google says that translates to
DISTRICT
MUNICIPALITY
STREET

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-12 13:27

palinka wrote:
2021-05-11 17:29
michalhana99 wrote:
2021-05-11 16:33
I enclose. I can't change the initial message, I don't have access to the sw
Of this, what do you want to extract as the sms message?

Code: Select all

<html>
<head>
  <title>Událost č.224769</title>
</head>
  <body>
  <div style="font-family: Times New Roman; width: 650px;">
    <strong>Nová Bystřice</strong>
    <b><big id="typ">JINÉ, ZATÍM NEURČENO - TECHNOLOGICKÝ TEST</big></b><br>
    Co se stalo: <b>test</b><br/>
    <hr>
    <i>Adresa události:</i><br>
    <table padding="10px">
<tr><td>GPS:</td><td><b>N 49.021091  E 15.104053 </b></td></tr>
      <tr><td>OKRES:</td><td><b>Jindřichův Hradec</b></td></tr>
      <tr><td>OBEC:</td><td><b>Nová Bystřice</b></td></tr>
      <tr><td>ULICE:</td><td><b>Švermova 278</b></td></tr>
    </table>
    OZNÁMIL: <b>&nbsp; &nbsp; Nová bystřice</b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Telefon: &nbsp; &nbsp; <b>undefined</b><br>
    <hr>
  </div>
  </body>
</html>
I see that a nice discussion started here. It is really a matter of improving services so that volunteer firefighters have more information about the intervention they are going to take.
The originally attached file was generated incorrectly. I am attaching a file that the hmail server will receive in the original.
I need the file to be transmitted in plain text to the sms gateway.
If I used the create function and send a copy, the problem was that sending each message took over a minute. These are about 35 telephone numbers, each of which must be entered in a separate email, which is then forwarded to the SMS gateway.
The script worked without problems if I tested it manually, but by the time the e-mail came from the control center in the format in which it is attached, the script only sent an empty message
Attachments
alarm.rar
(1.74 KiB) Downloaded 4 times

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-05-12 14:05

I'm playing around with this a bit. Converting from HTML to Plain Text is usually a pita ... Still need to work on tabs and newlines though.

Code: Select all

Function HtmlToText(sHTML)
    Dim oDoc : Set oDoc = CreateObject("htmlfile")
    oDoc.open : oDoc.close
    oDoc.body.innerHTML = sHTML
    HtmlToText = oDoc.body.innerText
End Function
SørenR.

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

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 15:13

michalhana99 wrote:
2021-05-12 13:27
I see that a nice discussion started here. It is really a matter of improving services so that volunteer firefighters have more information about the intervention they are going to take.
The originally attached file was generated incorrectly. I am attaching a file that the hmail server will receive in the original.
I need the file to be transmitted in plain text to the sms gateway.
If I used the create function and send a copy, the problem was that sending each message took over a minute. These are about 35 telephone numbers, each of which must be entered in a separate email, which is then forwarded to the SMS gateway.
The script worked without problems if I tested it manually, but by the time the e-mail came from the control center in the format in which it is attached, the script only sent an empty message
Try this:

Code: Select all

REM - oLookup function courtesy SorenR
Function oLookup(strRegEx, strMatch, bGlobal)
	If strRegEx = "" Then strRegEx = StrReverse(strMatch)
	With CreateObject("VBScript.RegExp")
		.Pattern = strRegEx
		.Global = bGlobal
		.MultiLine = True
		.IgnoreCase = True
		Set oLookup = .Execute(strMatch)
	End With
End Function

Sub CloneMail(oMessage)
	Dim SMSRecipientAddress, SMSFromAddress, SMSMessage, strRegEx, Match, Matches

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSMessage = oMessage.HTMLBody

	REM - remove HTML tags
	strRegEx = "<[^>]+>|&nbsp;|&lt;|&gt;|&quot;|&amp;"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, "")
	Next
	
	REM - replace line breaks with spaces
	strRegEx = "(\r\n|\r|\n)"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next
	
	REM - replace double/multiple spaces
	strRegEx = "([\s]{2,})"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next

	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = SMSMessage
		.Save
	End With
End Sub
Takes HTML body and strips HTML tags, then replaces linebreaks with spaces, then removes all double/multiple spaces, then sends message.

I tested it though my Email2SMS function and the SMS message was received like this:

Code: Select all

Událost c.224769 Nová Bystrice JINÉ, ZATÍM NEURCENO - TECHNOLOGICKÝ TEST Co se stalo: test Adresa události: GPS: N 49.021091 E 15.104053 OKRES: Jindrichuv Hradec OBEC: Nová Bystrice ULICE: Švermova 278 OZNÁMIL: Nová bystrice Telefon: undefined
Funny thing is, to get your message to work I had to completely rework my powershell email function due to the unicode characters. That was a project in itself.

Powershell:

Code: Select all

$FromAddress      = 'notify@mydomain.tld'
$Recipient        = 'admin@mydomain.tld'
$Subject          = 'CZTEST'
$EmailBody        = 'C:\scripts\GeoLite2SQL\Script-Created-Files\EmailBody.txt'
$FileAttachment   = 'C:\scripts\nosuchfile'
$HTML             = $True
$SMTPServer       = 'mydomain.tld'
$SMTPAuthUser     = 'notify@mydomain.tld'
$SMTPAuthPass     = 'supersecretpassword'
$SMTPPort         = 587
$SSL              = $True

Function Email {
	$BSFS = "
		PGh0bWw+CjxoZWFkPgogIDx0aXRsZT5VZMOhbG9zdCDEjS4yMjQ3Njk8L3RpdGxlPgo8L2hlYWQ+
		CiAgPGJvZHk+CiAgPGRpdiBzdHlsZT0iZm9udC1mYW1pbHk6IFRpbWVzIE5ldyBSb21hbjsgd2lk
		dGg6IDY1MHB4OyI+CiAgICA8c3Ryb25nPk5vdsOhIEJ5c3TFmWljZTwvc3Ryb25nPgogICAgPGI+
		PGJpZyBpZD0idHlwIj5KSU7DiSwgWkFUw41NIE5FVVLEjEVOTyAtIFRFQ0hOT0xPR0lDS8OdIFRF
		U1Q8L2JpZz48L2I+PGJyPgogICAgQ28gc2Ugc3RhbG86IDxiPnRlc3Q8L2I+PGJyLz4KICAgIDxo
		cj4KICAgIDxpPkFkcmVzYSB1ZMOhbG9zdGk6PC9pPjxicj4KICAgIDx0YWJsZSBwYWRkaW5nPSIx
		MHB4Ij4KPHRyPjx0ZD5HUFM6PC90ZD48dGQ+PGI+TiA0OS4wMjEwOTEgIEUgMTUuMTA0MDUzIDwv
		Yj48L3RkPjwvdHI+DQogICAgICA8dHI+PHRkPk9LUkVTOjwvdGQ+PHRkPjxiPkppbmTFmWljaMWv
		diBIcmFkZWM8L2I+PC90ZD48L3RyPg0KICAgICAgPHRyPjx0ZD5PQkVDOjwvdGQ+PHRkPjxiPk5v
		dsOhIEJ5c3TFmWljZTwvYj48L3RkPjwvdHI+DQogICAgICA8dHI+PHRkPlVMSUNFOjwvdGQ+PHRk
		PjxiPsWgdmVybW92YSAyNzg8L2I+PC90ZD48L3RyPg0KICAgIDwvdGFibGU+CiAgICBPWk7DgU1J
		TDogPGI+Jm5ic3A7ICZuYnNwOyBOb3bDoSBieXN0xZlpY2U8L2I+Jm5ic3A7ICZuYnNwOyAmbmJz
		cDsgJm5ic3A7ICZuYnNwOyAmbmJzcDsgVGVsZWZvbjogJm5ic3A7ICZuYnNwOyA8Yj51bmRlZmlu
		ZWQ8L2I+PGJyPgogICAgPGhyPgogIDwvZGl2PgogIDwvYm9keT4KPC9odG1sPgo=
	"
	$Body = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($BSFS))
	# $Body = (Get-Content -Path $EmailBody | Out-String )
	If (Test-Path $FileAttachment){$Attachment = New-Object System.Net.Mail.Attachment $FileAttachment}
	$Message = New-Object System.Net.Mail.Mailmessage $FromAddress, $Recipient, $Subject, $Body
	$Message.BodyEncoding = [System.Text.Encoding]::UTF8
	$Message.IsBodyHTML = [System.Convert]::ToBoolean($HTML)
	If (Test-Path $FileAttachment){$Message.Attachments.Add($FileAttachment)}
	$SMTP = New-Object System.Net.Mail.SMTPClient $SMTPServer,$SMTPPort
	$SMTP.EnableSsl = [System.Convert]::ToBoolean($SSL)
	$SMTP.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUser, $SMTPAuthPass); 
	$SMTP.Send($Message)
}

Email
Now the message, with the correct encoding, displays properly. :D
Screenshot 2021-05-12 091600.png

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 15:20

mikedibella wrote:
2021-05-12 05:33
Here's my donation to the cause. I use this function in my HTML-scraping routines:

Code: Select all

function Between(sString, sFrom, sTo)
	if InStr(sString, sFrom) > 0 then
		sTemp = Mid(sString, InStr(sString, sFrom) + Len(sFrom))
		if InStr(sTemp, sTo) > 0 then
			Between = Mid(sTemp, 1, InStr(sTemp, sTo) - 1)
		end if
	end if
end function
i.e.:

Code: Select all

sOKRES = Between(oMessage.HTMLBody, "<tr><td>OKRES:</td><td><b>", "</b></td></tr>")
sOBEC = Between(oMessage.HTMLBody, "<tr><td>OBEC:</td><td><b>", "</b></td></tr>")
sULICE = Between(oMessage.HTMLBody, "<tr><td>ULICE:</td><td><b>", "</b></td></tr>")
This is a nice function. I tried to extract these things using regex but it was picking up the entire line between the first < and very last >. This is a good way to do this.

Also, you forgot the GPS coordinates. :D

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-12 16:16

palinka wrote:
2021-05-12 15:13
michalhana99 wrote:
2021-05-12 13:27
I see that a nice discussion started here. It is really a matter of improving services so that volunteer firefighters have more information about the intervention they are going to take.
The originally attached file was generated incorrectly. I am attaching a file that the hmail server will receive in the original.
I need the file to be transmitted in plain text to the sms gateway.
If I used the create function and send a copy, the problem was that sending each message took over a minute. These are about 35 telephone numbers, each of which must be entered in a separate email, which is then forwarded to the SMS gateway.
The script worked without problems if I tested it manually, but by the time the e-mail came from the control center in the format in which it is attached, the script only sent an empty message
Try this:

Code: Select all

REM - oLookup function courtesy SorenR
Function oLookup(strRegEx, strMatch, bGlobal)
	If strRegEx = "" Then strRegEx = StrReverse(strMatch)
	With CreateObject("VBScript.RegExp")
		.Pattern = strRegEx
		.Global = bGlobal
		.MultiLine = True
		.IgnoreCase = True
		Set oLookup = .Execute(strMatch)
	End With
End Function

Sub CloneMail(oMessage)
	Dim SMSRecipientAddress, SMSFromAddress, SMSMessage, strRegEx, Match, Matches

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSMessage = oMessage.HTMLBody

	REM - remove HTML tags
	strRegEx = "<[^>]+>|&nbsp;|&lt;|&gt;|&quot;|&amp;"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, "")
	Next
	
	REM - replace line breaks with spaces
	strRegEx = "(\r\n|\r|\n)"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next
	
	REM - replace double/multiple spaces
	strRegEx = "([\s]{2,})"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next

	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = SMSMessage
		.Save
	End With
End Sub
Takes HTML body and strips HTML tags, then replaces linebreaks with spaces, then removes all double/multiple spaces, then sends message.

I tested it though my Email2SMS function and the SMS message was received like this:

Code: Select all

Událost c.224769 Nová Bystrice JINÉ, ZATÍM NEURCENO - TECHNOLOGICKÝ TEST Co se stalo: test Adresa události: GPS: N 49.021091 E 15.104053 OKRES: Jindrichuv Hradec OBEC: Nová Bystrice ULICE: Švermova 278 OZNÁMIL: Nová bystrice Telefon: undefined
Funny thing is, to get your message to work I had to completely rework my powershell email function due to the unicode characters. That was a project in itself.

Powershell:

Code: Select all

$FromAddress      = 'notify@mydomain.tld'
$Recipient        = 'admin@mydomain.tld'
$Subject          = 'CZTEST'
$EmailBody        = 'C:\scripts\GeoLite2SQL\Script-Created-Files\EmailBody.txt'
$FileAttachment   = 'C:\scripts\nosuchfile'
$HTML             = $True
$SMTPServer       = 'mydomain.tld'
$SMTPAuthUser     = 'notify@mydomain.tld'
$SMTPAuthPass     = 'supersecretpassword'
$SMTPPort         = 587
$SSL              = $True

Function Email {
	$BSFS = "
		PGh0bWw+CjxoZWFkPgogIDx0aXRsZT5VZMOhbG9zdCDEjS4yMjQ3Njk8L3RpdGxlPgo8L2hlYWQ+
		CiAgPGJvZHk+CiAgPGRpdiBzdHlsZT0iZm9udC1mYW1pbHk6IFRpbWVzIE5ldyBSb21hbjsgd2lk
		dGg6IDY1MHB4OyI+CiAgICA8c3Ryb25nPk5vdsOhIEJ5c3TFmWljZTwvc3Ryb25nPgogICAgPGI+
		PGJpZyBpZD0idHlwIj5KSU7DiSwgWkFUw41NIE5FVVLEjEVOTyAtIFRFQ0hOT0xPR0lDS8OdIFRF
		U1Q8L2JpZz48L2I+PGJyPgogICAgQ28gc2Ugc3RhbG86IDxiPnRlc3Q8L2I+PGJyLz4KICAgIDxo
		cj4KICAgIDxpPkFkcmVzYSB1ZMOhbG9zdGk6PC9pPjxicj4KICAgIDx0YWJsZSBwYWRkaW5nPSIx
		MHB4Ij4KPHRyPjx0ZD5HUFM6PC90ZD48dGQ+PGI+TiA0OS4wMjEwOTEgIEUgMTUuMTA0MDUzIDwv
		Yj48L3RkPjwvdHI+DQogICAgICA8dHI+PHRkPk9LUkVTOjwvdGQ+PHRkPjxiPkppbmTFmWljaMWv
		diBIcmFkZWM8L2I+PC90ZD48L3RyPg0KICAgICAgPHRyPjx0ZD5PQkVDOjwvdGQ+PHRkPjxiPk5v
		dsOhIEJ5c3TFmWljZTwvYj48L3RkPjwvdHI+DQogICAgICA8dHI+PHRkPlVMSUNFOjwvdGQ+PHRk
		PjxiPsWgdmVybW92YSAyNzg8L2I+PC90ZD48L3RyPg0KICAgIDwvdGFibGU+CiAgICBPWk7DgU1J
		TDogPGI+Jm5ic3A7ICZuYnNwOyBOb3bDoSBieXN0xZlpY2U8L2I+Jm5ic3A7ICZuYnNwOyAmbmJz
		cDsgJm5ic3A7ICZuYnNwOyAmbmJzcDsgVGVsZWZvbjogJm5ic3A7ICZuYnNwOyA8Yj51bmRlZmlu
		ZWQ8L2I+PGJyPgogICAgPGhyPgogIDwvZGl2PgogIDwvYm9keT4KPC9odG1sPgo=
	"
	$Body = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($BSFS))
	# $Body = (Get-Content -Path $EmailBody | Out-String )
	If (Test-Path $FileAttachment){$Attachment = New-Object System.Net.Mail.Attachment $FileAttachment}
	$Message = New-Object System.Net.Mail.Mailmessage $FromAddress, $Recipient, $Subject, $Body
	$Message.BodyEncoding = [System.Text.Encoding]::UTF8
	$Message.IsBodyHTML = [System.Convert]::ToBoolean($HTML)
	If (Test-Path $FileAttachment){$Message.Attachments.Add($FileAttachment)}
	$SMTP = New-Object System.Net.Mail.SMTPClient $SMTPServer,$SMTPPort
	$SMTP.EnableSsl = [System.Convert]::ToBoolean($SSL)
	$SMTP.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUser, $SMTPAuthPass); 
	$SMTP.Send($Message)
}

Email
Now the message, with the correct encoding, displays properly. :D

Screenshot 2021-05-12 091600.png
Thanks :-)

I'm a complete amateur. Do I need to run code in PowerShell before receiving email?

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

Re: Rules create copy and send, sending slow

Post by mikedibella » 2021-05-12 17:45

IMO, we need to select an approach. @plinka has been working on stripping the tags from the HTML body and just pass the plaintext without markup.

I'm suggesting you parse the HTML body and extract relevant fields, then compose a new plaintext message using value subsitiution.

Let's try to align to one of these approaches.

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-05-12 17:54

mikedibella wrote:
2021-05-12 17:45
IMO, we need to select an approach. @plinka has been working on stripping the tags from the HTML body and just pass the plaintext without markup.

I'm suggesting you parse the HTML body and extract relevant fields, then compose a new plaintext message using value subsitiution.

Let's try to align to one of these approaches.
The 6 lines of code I posted earlier today strip away ALL HTML/Table/Document formatting. You are left with Plain Text and a lot of 0D0A's.
SørenR.

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

User avatar
RvdH
Senior user
Senior user
Posts: 1461
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Rules create copy and send, sending slow

Post by RvdH » 2021-05-12 18:32

SorenR wrote:
2021-05-12 17:54
The 6 lines of code I posted earlier today strip away ALL HTML/Table/Document formatting. You are left with Plain Text and a lot of 0D0A's.

In my classic asp days i used something like

Code: Select all

Function stripHTML(strHTML)
'Strips the HTML tags from strHTML

  Dim objRegExp, strOutput
  Set objRegExp = New Regexp

  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  objRegExp.Pattern = "<(.|\n)+?>"

  'Replace all HTML tag matches with the empty string
  strOutput = objRegExp.Replace(strHTML, "")
  
  'Replace all < and > with &lt; and &gt;
  strOutput = Replace(strOutput, "<", "&lt;")
  strOutput = Replace(strOutput, ">", "&gt;")
  
  stripHTML = strOutput    'Return the value of strOutput

  Set objRegExp = Nothing
End Function
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-05-12 18:39

This:

Code: Select all

Function HtmlToText(sHTML)
    Dim oDoc : Set oDoc = CreateObject("htmlfile")
    oDoc.open : oDoc.close
    oDoc.body.innerHTML = sHTML
    HtmlToText = oDoc.body.innerText
End Function
is comparable with this:

Code: Select all

Function HTML2PlainText(strSource)
    Dim i, strResult, strBreaks, strTabs
    Const IgnoreCase = True
    Const MatchCase = False

    '   Remove HTML Development formatting
    '   Replace line breaks with space because browsers inserts space (CR)
    strResult = Replace(strSource, Chr(13), " ", 1, -1, 0)

    '   Replace line breaks with space because browsers inserts space (LF)
    strResult = Replace(strResult, Chr(10), " ", 1, -1, 0)

    '   Remove step-formatting
    strResult = Replace(strResult, Chr(9), "", 1, -1, 0)

    '   Remove repeating spaces because browsers ignore them
    strResult = RegExReplace(strResult, "( )+", " ", IgnoreCase)

    '   Remove the header (prepare first by clearing attributes)
    strResult = RegExReplace(strResult, "<( )*head([^>])*>", "<head>", IgnoreCase)
    strResult = RegExReplace(strResult, "(<( )*(/)( )*head( )*>)", "</head>", IgnoreCase)
    strResult = RegExReplace(strResult, "(<head>).*(</head>)", "", IgnoreCase)

    '   Remove all scripts (prepare first by clearing attributes)
    strResult = RegExReplace(strResult, "<( )*script([^>])*>", "<script>", IgnoreCase)
    strResult = RegExReplace(strResult, "(<( )*(/)( )*script( )*>)", "</script>", IgnoreCase)

    'strResult = RegExReplace(strResult, "(<script>)([^(<script>\.</script>)])*(</script>)", "", IgnoreCase)
    strResult = RegExReplace(strResult, "(<script>).*(</script>)", "", IgnoreCase)

    '   Remove all styles (prepare first by clearing attributes)
    strResult = RegExReplace(strResult, "<( )*style([^>])*>", "<style>", IgnoreCase)
    strResult = RegExReplace(strResult, "(<( )*(/)( )*style( )*>)", "</style>", IgnoreCase)
    strResult = RegExReplace(strResult, "(<style>).*(</style>)", "", IgnoreCase)

    '   Insert tabs in spaces of <td> tags
    strResult = RegExReplace(strResult, "<( )*td([^>])*>", Chr(9), IgnoreCase)

    '   Insert line breaks in places of <BR> and <LI> tags
    strResult = RegExReplace(strResult, "<( )*br( )*>", Chr(13), IgnoreCase)
    strResult = RegExReplace(strResult, "<( )*li( )*>", Chr(13), IgnoreCase)

    '   Insert line paragraphs (double line breaks) in place
    '   if <P>, <DIV> and <TR> tags
    strResult = RegExReplace(strResult, "<( )*div([^>])*>", Chr(13) & Chr(13), IgnoreCase)
    strResult = RegExReplace(strResult, "<( )*tr([^>])*>", Chr(13) & Chr(13), IgnoreCase)
    strResult = RegExReplace(strResult, "<( )*p([^>])*>", Chr(13) & Chr(13), IgnoreCase)

    '   Remove remaining tags like <a>, links, images, comments etc - anything that's enclosed inside < >
    strResult = RegExReplace(strResult, "<[^>]*>", "", IgnoreCase)

    '   Replace special characters:
    strResult = RegExReplace(strResult, " ", " ", IgnoreCase)

    strResult = RegExReplace(strResult, "&bull", " * ", IgnoreCase)
    strResult = RegExReplace(strResult, "&lsaquo", "<", IgnoreCase)
    strResult = RegExReplace(strResult, "&rsaquo", ">", IgnoreCase)
    strResult = RegExReplace(strResult, "&trade", "(tm)", IgnoreCase)
    strResult = RegExReplace(strResult, "&frasl", "/", IgnoreCase)
    strResult = RegExReplace(strResult, "&lt", "<", IgnoreCase)
    strResult = RegExReplace(strResult, "&gt", ">", IgnoreCase)
    strResult = RegExReplace(strResult, "&copy", "(c)", IgnoreCase)
    strResult = RegExReplace(strResult, "&reg", "(r)", IgnoreCase)

    '   Danish
    strResult = RegExReplace(strResult, "&AElig;", "Æ", MatchCase)
    strResult = RegExReplace(strResult, "&aelig;", "æ", MatchCase)
    strResult = RegExReplace(strResult, "&Oslash;", "Ø", MatchCase)
    strResult = RegExReplace(strResult, "&oslash;", "ø", MatchCase)
    strResult = RegExReplace(strResult, "&Aring;", "Å", MatchCase)
    strResult = RegExReplace(strResult, "&aring;", "å", MatchCase)

    '   Remove all others. More can be added, see
    '   http:'hotwired.lycos.com/webmonkey/reference/special_characters/
    strResult = RegExReplace(strResult, "&(.{2,6})", "", IgnoreCase)

    '   for testing
    'RegExReplace(strResult, this.txtRegex.Text, "", IgnoreCase)

    '   Make line breaking consistent
    strResult = Replace(strResult, Chr(10), Chr(13), 1, -1, 0)

    '   Remove extra line breaks and tabs: 
    '   replace over 2 breaks with 2 and over 4 tabs with 4.
    '   Prepare first to remove any whitespaces in between the escaped 
    '   characters and remove redundant tabs in between line breaks
    strResult = RegExReplace(strResult, "(\r)( )+(\r)", Chr(13) & Chr(13), IgnoreCase)
    strResult = RegExReplace(strResult, "(\t)( )+(\t)", Chr(9) & Chr(9), IgnoreCase)
    strResult = RegExReplace(strResult, "(\t)( )+(\r)", Chr(9) & Chr(13), IgnoreCase)
    strResult = RegExReplace(strResult, "(\r)( )+(\t)", Chr(13) & Chr(9), IgnoreCase)

    ' Remove redundant tabs
    strResult = RegExReplace(strResult, "(\r)(\t)+(\r)", Chr(13) & Chr(13), IgnoreCase)

    ' Remove multiple tabs following a line break with just one tab
    strResult = RegExReplace(strResult, "(\r)(\t)+", Chr(13) & Chr(9), IgnoreCase)

    ' Initial replacement target string for line breaks
    strBreaks = Chr(13) & Chr(13) & Chr(13)

    ' Initial replacement target string for tabs
    strTabs = Chr(9) & Chr(9) & Chr(9) & Chr(9) & Chr(9)

    For i=0 To Len(strResult)
        strResult = Replace(strResult, strBreaks, Chr(13) & Chr(13), 1, -1, 0)
        strResult = Replace(strResult, strTabs, Chr(9) & Chr(9) & Chr(9) & Chr(9), 1, -1, 0)
        strBreaks = strBreaks + Chr(13)
        strTabs = strTabs + Chr(9)
    Next

    ' That's it.
    HTML2PlainText = strResult
End Function


Function RegExReplace(strSource, strRegEx, strReplace, bCase)
    With CreateObject("VBScript.RegExp")
        .Pattern = strRegEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = bCase
        RegExReplace = .Replace(strSource, strReplace)
    End With
End Function
SørenR.

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

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 18:44

SorenR wrote:
2021-05-12 18:39
This:
....

is comparable with this:
No its not. Its WAY shorter. :mrgreen:

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 18:47

michalhana99 wrote:
2021-05-12 16:16
I'm a complete amateur. Do I need to run code in PowerShell before receiving email?
NO! That was just my script to get your language unicode characters to display properly. :D

It was the only way I could replicate your message for testing the CloneMail function.

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 18:56

mikedibella wrote:
2021-05-12 17:45
IMO, we need to select an approach. @plinka has been working on stripping the tags from the HTML body and just pass the plaintext without markup.

I'm suggesting you parse the HTML body and extract relevant fields, then compose a new plaintext message using value subsitiution.

Let's try to align to one of these approaches.
I agree, but I took the simple route. See my shortlink function and google maps link idea above. :D

Also, we're assuming the OP doesn't want *all* of the message.

By the way, the SMS test message was 245 char after stripping out html. Another good reason to go in your direction. Not every phone is capable of recombining multi-part SMS. Better to try to keep it under 160 char.

SorenR wrote:
2021-05-12 17:54
The 6 lines of code I posted earlier today strip away ALL HTML/Table/Document formatting. You are left with Plain Text and a lot of 0D0A's.
Mike is talking about constructing a completely new message and leaving out superfluous items from the original message.

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-12 20:30

palinka wrote:
2021-05-12 18:47
michalhana99 wrote:
2021-05-12 16:16
I'm a complete amateur. Do I need to run code in PowerShell before receiving email?
NO! That was just my script to get your language unicode characters to display properly. :D

It was the only way I could replicate your message for testing the CloneMail function.
I'm sorry, I didn't realize that at all.

After testing, the Clonemail function returns me:

<head> <title>Událost č.224769</title> </head> <body> <div style="font-family: Times New Roman; width: 650px;"> <strong>Nová Bystřice</strong> <b><big id="typ">JINÉ, ZATÍM NEURČENO - TECHNOLOGICKÝ TEST</big></b><br> Co se stalo: <b>test</b><br/> <hr> <i>Adresa události:</i><br> <table padding="10px"> <tr><td>GPS:</td><td><b>N 49.021091 E 15.104053 </b></td></tr> .....

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 20:35

michalhana99 wrote:
2021-05-12 20:30
palinka wrote:
2021-05-12 18:47
michalhana99 wrote:
2021-05-12 16:16
I'm a complete amateur. Do I need to run code in PowerShell before receiving email?
NO! That was just my script to get your language unicode characters to display properly. :D

It was the only way I could replicate your message for testing the CloneMail function.
I'm sorry, I didn't realize that at all.

After testing, the Clonemail function returns me:

<head> <title>Událost č.224769</title> </head> <body> <div style="font-family: Times New Roman; width: 650px;"> <strong>Nová Bystřice</strong> <b><big id="typ">JINÉ, ZATÍM NEURČENO - TECHNOLOGICKÝ TEST</big></b><br> Co se stalo: <b>test</b><br/> <hr> <i>Adresa události:</i><br> <table padding="10px"> <tr><td>GPS:</td><td><b>N 49.021091 E 15.104053 </b></td></tr> .....
* Did you include the oLookup function in your eventhandlers.vbs?
* Did you reload scripts at settings > advanced > scripts?
* Is there an error log?

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-12 20:43

palinka wrote:
2021-05-12 20:35
michalhana99 wrote:
2021-05-12 20:30
palinka wrote:
2021-05-12 18:47


NO! That was just my script to get your language unicode characters to display properly. :D

It was the only way I could replicate your message for testing the CloneMail function.
I'm sorry, I didn't realize that at all.

After testing, the Clonemail function returns me:

<head> <title>Událost č.224769</title> </head> <body> <div style="font-family: Times New Roman; width: 650px;"> <strong>Nová Bystřice</strong> <b><big id="typ">JINÉ, ZATÍM NEURČENO - TECHNOLOGICKÝ TEST</big></b><br> Co se stalo: <b>test</b><br/> <hr> <i>Adresa události:</i><br> <table padding="10px"> <tr><td>GPS:</td><td><b>N 49.021091 E 15.104053 </b></td></tr> .....
* Did you include the oLookup function in your eventhandlers.vbs?
* Did you reload scripts at settings > advanced > scripts?
* Is there an error log?
* function oLookup is paste in eventhandlers.vbs
* scripts reload
* There is nothing in the error log

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

Re: Rules create copy and send, sending slow

Post by palinka » 2021-05-12 23:16

michalhana99 wrote:
2021-05-12 20:43
* function oLookup is paste in eventhandlers.vbs
* scripts reload
* There is nothing in the error log
Very strange. I'm not sure why it didn't work, but it doesn't, obviously. I thought I could save a couple lines of code. Anyway, I tested with this function RemoveHTML that I use. This absolutely does work on your test message.

Code: Select all

REM - oLookup function courtesy SorenR
Function oLookup(strRegEx, strMatch, bGlobal)
	If strRegEx = "" Then strRegEx = StrReverse(strMatch)
	With CreateObject("VBScript.RegExp")
		.Pattern = strRegEx
		.Global = bGlobal
		.MultiLine = True
		.IgnoreCase = True
		Set oLookup = .Execute(strMatch)
	End With
End Function

Function RemoveHTML(strHTML)
	With CreateObject("VBScript.RegExp")
		.Pattern = "<[^>]+>|&nbsp;|&quot;|&amp;"
		.Global = True
		.MultiLine = True
		.IgnoreCase = True
		RemoveHTML = .Replace(strHTML, "")
	End With
End Function

Sub CloneMail(oMessage)
	Dim SMSRecipientAddress, SMSFromAddress, SMSMessage, strRegEx, Match, Matches

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSMessage = oMessage.HTMLBody

	REM - Remove HTML tags
	SMSMessage = RemoveHTML(SMSMessage)

	REM - replace line breaks with spaces
	strRegEx = "(\r\n|\r|\n)"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next
	
	REM - replace double/multiple spaces
	strRegEx = "([\s]{2,})"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next

	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = SMSMessage
		.Save
	End With
End Sub
With that being said, I would test Soren's function because it does a lot more than my simple one.

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-05-12 23:57

palinka wrote:
2021-05-12 23:16

With that being said, I would test Soren's function because it does a lot more than my simple one.
I'm running all messages to a small group of accounts thru this to fine tune removal of unwanted newlines...

Code: Select all

Function HtmlToText(sHTML)

    If IsNull(sHTML) Then
        HtmlToText = ""
        Exit Function
    End If

    Dim i, strTemp, oDoc : Set oDoc = CreateObject("htmlfile")
    oDoc.open : oDoc.close
    oDoc.body.innerHTML = sHTML
    strTemp = Replace(oDoc.body.innerText, Chr(13) & Chr(10), Chr(13), 1, -1, 0)

    i = Len(strTemp) + 1
    Do Until i = Len(strTemp)
        i = Len(strTemp)
        strTemp = Replace(strTemp, Chr(13) & Chr(13) & Chr(13), Chr(13) & Chr(13), 1, -1, 0)
        strTemp = Replace(strTemp, Chr(32) & Chr(13), Chr(13), 1, -1, 0)
    Loop

    HtmlToText = strTemp

End Function
SørenR.

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

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

Re: Rules create copy and send, sending slow

Post by SorenR » 2021-05-13 00:07

palinka wrote:
2021-05-12 20:35
michalhana99 wrote:
2021-05-12 20:30
palinka wrote:
2021-05-12 18:47


NO! That was just my script to get your language unicode characters to display properly. :D

It was the only way I could replicate your message for testing the CloneMail function.
I'm sorry, I didn't realize that at all.

After testing, the Clonemail function returns me:

<head> <title>Událost č.224769</title> </head> <body> <div style="font-family: Times New Roman; width: 650px;"> <strong>Nová Bystřice</strong> <b><big id="typ">JINÉ, ZATÍM NEURČENO - TECHNOLOGICKÝ TEST</big></b><br> Co se stalo: <b>test</b><br/> <hr> <i>Adresa události:</i><br> <table padding="10px"> <tr><td>GPS:</td><td><b>N 49.021091 E 15.104053 </b></td></tr> .....
* Did you include the oLookup function in your eventhandlers.vbs?
* Did you reload scripts at settings > advanced > scripts?
* Is there an error log?
Scraping HTML tables ???

Have a look here ==> https://superuser.com/questions/1009341 ... -worksheet
SørenR.

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

michalhana99
New user
New user
Posts: 22
Joined: 2017-10-21 12:39

Re: Rules create copy and send, sending slow

Post by michalhana99 » 2021-05-13 08:50

palinka wrote:
2021-05-12 23:16
michalhana99 wrote:
2021-05-12 20:43
* function oLookup is paste in eventhandlers.vbs
* scripts reload
* There is nothing in the error log
Very strange. I'm not sure why it didn't work, but it doesn't, obviously. I thought I could save a couple lines of code. Anyway, I tested with this function RemoveHTML that I use. This absolutely does work on your test message.

Code: Select all

REM - oLookup function courtesy SorenR
Function oLookup(strRegEx, strMatch, bGlobal)
	If strRegEx = "" Then strRegEx = StrReverse(strMatch)
	With CreateObject("VBScript.RegExp")
		.Pattern = strRegEx
		.Global = bGlobal
		.MultiLine = True
		.IgnoreCase = True
		Set oLookup = .Execute(strMatch)
	End With
End Function

Function RemoveHTML(strHTML)
	With CreateObject("VBScript.RegExp")
		.Pattern = "<[^>]+>|&nbsp;|&quot;|&amp;"
		.Global = True
		.MultiLine = True
		.IgnoreCase = True
		RemoveHTML = .Replace(strHTML, "")
	End With
End Function

Sub CloneMail(oMessage)
	Dim SMSRecipientAddress, SMSFromAddress, SMSMessage, strRegEx, Match, Matches

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSMessage = oMessage.HTMLBody

	REM - Remove HTML tags
	SMSMessage = RemoveHTML(SMSMessage)

	REM - replace line breaks with spaces
	strRegEx = "(\r\n|\r|\n)"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next
	
	REM - replace double/multiple spaces
	strRegEx = "([\s]{2,})"
	Set Matches = oLookup(strRegEx, SMSMessage, False)
	For Each Match In Matches
	   SMSMessage = Replace(SMSMessage, Match.Value, " ")
	Next

	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = oMessage.Subject
		.Body = SMSMessage
		.Save
	End With
End Sub
With that being said, I would test Soren's function because it does a lot more than my simple one.
The above code is already running smoothly.
But unfortunately, when I try to run another rule for an edit message, for example:

Code: Select all

Sub SMS1(oMessage)
	'
	' WARNING - incoming message must be plain text
	'

	Dim SMSRecipientAddress, SMSFromAddress, SMSSubject

	SMSRecipientAddress = "someoutsideemail@domain.tld"    '<-- account to which message gets forwarded
	SMSFromAddress      = "localuser@yourdomain.tld"       '<-- account from which message is sent
	SMSSubject          = "1234567890"                     '<-- OP says SMS number comes from subject line
	
	With CreateObject("hMailServer.Message")
		.From = Chr(34) & SMSFromAddress & Chr(34) & " <" & SMSFromAddress & ">"
		.FromAddress = SMSFromAddress
		.AddRecipient SMSRecipientAddress, SMSRecipientAddress
		.HeaderValue("To") = Chr(34) & SMSRecipientAddress & Chr(34) & " <" & SMSRecipientAddress & ">"
		.Subject = SMSSubject
		.Body = oMessage.Body
		.Save
	End With
End Sub
so the rule does not break. I have the conditions set correctly

Post Reply