Forward SMS-compatible message

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
redrummy
Senior user
Senior user
Posts: 370
Joined: 2007-06-21 06:52
Location: Alaska

Forward SMS-compatible message

Post by redrummy » 2007-12-20 21:02

Edit: 2007-12-26 - OBSOLETE - See "MobileProxy: 2-Way Mobile Device Proxy" (http://www.hmailserver.com/forum/viewtopic.php?t=11209)

Information:
I have a rule that forwards messages to my phone's e-mail address. My wireless carrier used to send whatever fit into a single SMS message to my phone and dropped the rest. A waste of bandwidth on my part, I know, but it got me what I wanted (sender, subject, 1st sentence or so... basically just a new message notification to my phone). My wireless carrier recently upgraded their system so that they now split and deliver up to 4 SMS messages per e-mail (1/4, 2/4, 3/4, 4/4).

Question:
Does anyone have the time & inclination to suggest a script that will forward text-only messages up to the SMS limit (160 chars) and truncate the rest? I can probably tweak the script myself once I get an idea of how it should be written, but the only scripting language I know is BAT/CMD. =)

Thanks 10^6,
Ryan
Last edited by redrummy on 2007-12-27 07:04, edited 1 time in total.

redrummy
Senior user
Senior user
Posts: 370
Joined: 2007-06-21 06:52
Location: Alaska

Post by redrummy » 2007-12-23 05:03

Okay, I see I'm being instructed with the silent Zen methodology. Fair enough. ;) I'm posting the product of my lesson in case it's of use to anyone else. The cmurphy54 code in http://www.hmailserver.com/forum/viewtopic.php?t=2048 was the closest thing I found to what I was trying to accomplish. I muddled through several modifications:
  • -Change from OnDeliverMessage sub to MailboxNotify function
    (call from rules selectively instead of notifying on every message)
    -Add support for masquerading the original sender in the outbound notification
    (if your carrier's SMS server allows this, which luckily mine does)
    -Change outbound message MIME from to SMTP from
    (compatibility w/ my carrier's SMS service)
    -Remove debugging code
    (ironically it had a bug and the hMailServer log records everything I want anyway)
    -Minor visual cleanup
    (what can I say, I'm a neat freak)
It meets 90% of my goals now. The only thing I haven't figured out yet is how to extract the 1st 50-100 characters of the original message body to include in the outbound notification. Then it will be purrr-fect. I can probably figure that out eventually but like I said, I write batch files, not VB. So... any takers?

Ryan

Code: Select all

' MailboxNotify for hMailserver v2007122201 (redrummy)
' for discussion see http://www.hmailserver.com/forum/viewtopic.php?t=11183
' based on cmurphy54 code in http://www.hmailserver.com/forum/viewtopic.php?t=2048
' 
' Purpose: Send short e-mail to SMS or other address to notify of new mail
' 
' Instructions:
' Mandatory - Edit oNotificationAddresses as indicated under InitializeVariables
' Optional - Edit Const strings as desired or necessary to suit your needs
' Usage - Enter MailboxNotify in Rule / Action / Run function / Script function

'*************************************************************** 
' User defined strings 
'*************************************************************** 

' Auto-replaced strings are: [mailbox], [sender], [subject], [n] (newline/CRLF)
Const conNotificationFrom = "[sender]" 
Const conNotificationSubject = "[subject]" 
Const conNotificationBody = "Received in mailbox: [mailbox][n]" 
' If notifications fail with settings above (probably due to forged sender)
' try the settings below instead (or figure out what works for you)
' Const conNotificationFrom = "[mailbox]" 
' Const conNotificationSubject = "[subject]" 
' Const conNotificationBody = "Received from: [sender][n]" 

'*************************************************************** 
' Initialize variables, define mailbox/notify-address pairings
'*************************************************************** 

Dim oNotificationAddresses 

Sub InitializeVariables 
  ' Create dictionary object for our notification lookups 
  Set oNotificationAddresses = CreateObject("Scripting.Dictionary") 

  ' Add pairings in the format: oNotificationAddresses.Add "original.recipient", "notify.address" 
  ' oNotificationAddresses.Add "user@hmailserver.domain", "1234567890@sms.email.domain" 
  oNotificationAddresses.Add "user@hmailserver.domain", "1234567890@sms.email.domain" 
  
End Sub 

'*************************************************************** 
' Mailbox notification functionality 
'*************************************************************** 

Sub DoMailboxNotification(oMessage) 
  Dim strRecipientAddress 
  Dim strMessageBody 
  Dim strMessageSubject 

  ' Loop through all the mail recipients 
  For i = 0 To oMessage.Recipients.Count - 1 
    strRecipientAddress = oMessage.Recipients(i).Address 
    If oNotificationAddresses.Exists(strRecipientAddress) Then 
      ' Auto-replace supported strings 
      strMessageFrom = conNotificationFrom 
      strMessageFrom = Replace(strMessageFrom, "[mailbox]", strRecipientAddress) 
      strMessageFrom = Replace(strMessageFrom, "[sender]", oMessage.FromAddress) 
      strMessageSubject = conNotificationSubject 
      strMessageSubject = Replace(strMessageSubject, "[mailbox]", strRecipientAddress) 
      strMessageSubject = Replace(strMessageSubject, "[sender]", oMessage.FromAddress) 
      strMessageSubject = Replace(strMessageSubject, "[subject]", oMessage.Subject) 
      strMessageBody = conNotificationBody 
      strMessageBody = Replace(strMessageBody, "[mailbox]", strRecipientAddress) 
      strMessageBody = Replace(strMessageBody, "[sender]", oMessage.FromAddress) 
      strMessageBody = Replace(strMessageBody, "[subject]", oMessage.Subject) 
      strMessageBody = Replace(strMessageBody, "[n]", vbCRLF) 
      ' Send message 
      SendMessage strMessageFrom, Array(oNotificationAddresses.Item(strRecipientAddress)), strMessageSubject, strMessageBody 
    End If 
  Next 

End Sub 

'*************************************************************** 
' Send message functionality 
'*************************************************************** 

Function SendMessage(strFrom, arrRecipients, strSubject, strBody) 
  Dim oMessage 
  Set oMessage = CreateObject("hMailServer.Message") 
  oMessage.FromAddress = strFrom 
  oMessage.Subject = strSubject 
  Dim arrRecipientParts 

  For Each recipient in arrRecipients 
    arrRecipientParts = Split(recipient, ",") 
    If(UBound(arrRecipientParts) > 1) Then 
      oMessage.AddRecipient arrRecipientParts(0), arrRecipientParts(1) 
    Else 
      oMessage.AddRecipient "", arrRecipientParts(0) 
    End If 
  Next 

  oMessage.Body = strBody 
  oMessage.Save 
  Set oMessage = Nothing 
End Function 

'*************************************************************** 
' Clean up any variables we might be using 
'*************************************************************** 

Sub DisposeVariables 
  Set oNotificationAddresses = Nothing 
End Sub 

'*************************************************************** 
' Define "MailboxNotify" function for calling from mailbox rules
'*************************************************************** 

Function MailboxNotify(oMessage) 
  InitializeVariables 
  DoMailboxNotification(oMessage) 
  DisposeVariables    
End Function 
Last edited by redrummy on 2007-12-27 07:00, edited 1 time in total.

redrummy
Senior user
Senior user
Posts: 370
Joined: 2007-06-21 06:52
Location: Alaska

Post by redrummy » 2007-12-26 06:01

Ha! Got it. I'm sure the code isn't the greatest, but when I consider myself proficient in VBS I *might* come back and clean it up. Anyway, it works as desired. In case it's of use to anyone else: Merry Christmas. =)

Code: Select all

' MailboxNotify for hMailserver v2007122501 (hMS forum user redrummy)
' for discussion see http://www.hmailserver.com/forum/viewtopic.php?t=11183
' based on cmurphy54 code in http://www.hmailserver.com/forum/viewtopic.php?t=2048
' 
' Purpose: Send short e-mail to SMS user or other address to notify of new mail
' 
' Instructions:
' Mandatory - Edit oNotifyAddresses as indicated under InitializeVariables
' Optional - Edit Const strings as desired or necessary to suit your needs
' Usage - in Rule / Action / Run function / Script function: MailboxNotify

'*************************************************************** 
' User defined strings 
'*************************************************************** 

' Auto-replaced strings are: [mailbox], [sender], [subject], [body], [n] (newline/CRLF) 
' xxxChars is number of characters to include from original message subject and body (text)
' Total message will be SubjChars + BodyChars + additional literal or [replaced] strings
Const conNotifyFrom = "[sender]" 
Const conNotifySubject = "[subject]" 
Const conNotifySubjChars = 20 
Const conNotifyBody = "(for [mailbox])[n][body]" 
Const conNotifyBodyChars = 80 

'*************************************************************** 
' Initialize variables, define mailbox/notify-address pairings 
'*************************************************************** 

Dim oNotifyAddresses 

Sub InitializeVariables 
  ' Create dictionary object for our notification lookups 
  Set oNotifyAddresses = CreateObject("Scripting.Dictionary") 

  ' Add pairings in the format: oNotifyAddresses.Add "original.recipient", "notify.address" 
  ' oNotifyAddresses.Add "user@hmailserver.domain", "1234567890@sms.message.domain" 
  oNotifyAddresses.Add "user@hmailserver.domain", "1234567890@sms.message.domain" 

End Sub 

'*************************************************************** 
' Mailbox notification functionality 
'*************************************************************** 

Sub DoMailboxNotify(oMessage) 
  Dim strNotifyAddress 
  Dim strNotifySubject 
  Dim strNotifySubjShort
  Dim strNotifyBody 
  Dim strNotifyBodyShort

  ' Loop through all the mail recipients 
  For i = 0 To oMessage.Recipients.Count - 1 
    strNotifyAddress = oMessage.Recipients(i).Address 
    If oNotifyAddresses.Exists(strNotifyAddress) Then 
	  ' Trim subject and message body text to specified lengths, tag if trimmed, remove coded returns & spaces
	  strNotifySubjShort = Left(oMessage.Subject, conNotifySubjChars)
	  If not strNotifySubjShort = oMessage.Subject Then
	    strNotifySubjShort = strNotifySubjShort & "(...)"
	  End If
	  strNotifyBodyShort = Left(oMessage.Body, conNotifyBodyChars)
	  If not strNotifyBodyShort = oMessage.Body Then
	    strNotifyBodyShort = strNotifyBodyShort & "(...)"
	  End If
	  strNotifyBodyShort = Replace(strNotifyBodyShort, " =" & vbCRLF, " ")
	  strNotifyBodyShort = Replace(strNotifyBodyShort, "=0D", " ")
      strNotifyBodyShort = Replace(strNotifyBodyShort, "=20", " ")
	  ' Auto-replace supported strings 
      strMessageFrom = conNotifyFrom 
      strMessageFrom = Replace(strMessageFrom, "[mailbox]", strNotifyAddress) 
      strMessageFrom = Replace(strMessageFrom, "[sender]", oMessage.FromAddress) 
      strNotifySubject = conNotifySubject 
      strNotifySubject = Replace(strNotifySubject, "[mailbox]", strNotifyAddress) 
      strNotifySubject = Replace(strNotifySubject, "[sender]", oMessage.FromAddress) 
      strNotifySubject = Replace(strNotifySubject, "[subject]", strNotifySubjShort) 
      strNotifyBody = conNotifyBody 
      strNotifyBody = Replace(strNotifyBody, "[mailbox]", strNotifyAddress) 
      strNotifyBody = Replace(strNotifyBody, "[sender]", oMessage.FromAddress) 
      strNotifyBody = Replace(strNotifyBody, "[subject]", strNotifySubjShort) 
      strNotifyBody = Replace(strNotifyBody, "[n]", vbCRLF) 
      strNotifyBody = Replace(strNotifyBody, "[body]", strNotifyBodyShort) 
      ' Send message 
      SendMessage strMessageFrom, Array(oNotifyAddresses.Item(strNotifyAddress)), strNotifySubject, strNotifyBody 
    End If 
  Next 

End Sub 

'*************************************************************** 
' Send message functionality 
' arrRecipients = recipient array in the form of "Address" or "Address,Display Name" 
'   ex. Array("user@domain.com", "dduck@domain.com,Daffy Duck") 
'*************************************************************** 

Function SendMessage(strFrom, arrRecipients, strSubject, strBody) 
  Dim oMessage 
  Set oMessage = CreateObject("hMailServer.Message") 
  oMessage.FromAddress = strFrom 
  oMessage.Subject = strSubject 
  Dim arrRecipientParts 

  For Each recipient in arrRecipients 
    arrRecipientParts = Split(recipient, ",") 
    If(UBound(arrRecipientParts) > 1) Then 
      oMessage.AddRecipient arrRecipientParts(0), arrRecipientParts(1) 
    Else 
      oMessage.AddRecipient "", arrRecipientParts(0) 
    End If 
  Next 

  oMessage.Body = strBody 
  oMessage.Save 
  Set oMessage = Nothing 
End Function 

'*************************************************************** 
' Clean up any variables we might be using 
'*************************************************************** 

Sub DisposeVariables 
  Set oNotifyAddresses = Nothing 
End Sub 

'*************************************************************** 
' Define "MailboxNotify" function for calling from mailbox rules
'*************************************************************** 

Function MailboxNotify(oMessage) 
  InitializeVariables 
  DoMailboxNotify(oMessage) 
  DisposeVariables    
End Function 

redrummy
Senior user
Senior user
Posts: 370
Joined: 2007-06-21 06:52
Location: Alaska

Post by redrummy » 2007-12-27 07:05

OBSOLETE - See "MobileProxy: 2-Way Mobile Device Proxy" (http://www.hmailserver.com/forum/viewtopic.php?t=11209)

Ryan

Post Reply