Advertisement
vibetribe

hMailServer - Email Signature Placement Script

Mar 7th, 2016
1,824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub OnAcceptMessage(oClient, oMessage)
  2.  
  3.     Dim debug : debug = FALSE
  4.  
  5.     If oClient.username <> "" Then ' Only modify outgoing emails
  6.  
  7.         EventLog.Write(oClient.Username & " - INFO: Sending mail ...")
  8.  
  9.         If (Len(oMessage.HTMLBody) < 1 And Len(oMessage.body) > 0) Then ' Message is text only?
  10.             If debug Then
  11.                 EventLog.Write(oClient.Username & " - DEBUG: Full Message (Text) = " & oMessage.body)
  12.             End If
  13.             Dim textSegments : textSegments = Split(oMessage.body, "<!-- TXTsig -->", 2, 1)
  14.             Dim textMessage : textMessage = textSegments(0)
  15.             Dim textSignature : textSignature = textSegments(1)
  16.             textMessage = Replace(textMessage, "<", "&lt;")
  17.             textMessage = Replace(textMessage, ">", "&gt;")
  18.             textMessage = Replace(textMessage, vbNewLine, "<br />")
  19.             oMessage.body = ""
  20.             oMessage.HTMLBody = "<html><body>" & textMessage & "</body></html><br/>" & textSignature ' Convert plain text into HTML ...
  21.             ' You have to add HTML signature into the TXT signature, but the signature has to start with <!-- TXTsig -->
  22.             ' ALERT: Do this for domain and user signatures!
  23.         End If
  24.  
  25.         If (Len(oMessage.HTMLBody) > 0) Then ' Message is not empty (for instance appointments are empty)
  26.  
  27.             Set regExp = New RegExp ' Regular expression to determine the end tags of the message
  28.             With regExp
  29.                 .Pattern    = "</body>[\r\n]*</html>[\r\n]*<br/>"
  30.                 .IgnoreCase = True
  31.                 .Global     = False
  32.             End With
  33.  
  34.             If debug Then
  35.                 EventLog.Write(oClient.Username & " - DEBUG: Full Message = " & oMessage.HTMLBody)
  36.             End If
  37.  
  38.             Set splitMarkMatches = regExp.Execute(oMessage.HTMLBody) ' Gets a single match of the end tags of the message without signature
  39.  
  40.             If splitMarkMatches.Count > 0 Then ' Is there an end mark, so this is a HTML email?
  41.        
  42.                 Dim splitMark : splitMark = splitMarkMatches.Item(0).Value
  43.  
  44.                 If debug Then
  45.                     EventLog.Write(oClient.Username & " - DEBUG: Split mark = " & splitMark)
  46.                 End If
  47.  
  48.                 Dim parts : parts = Split(oMessage.HTMLBody, splitMark) ' Split message and signature, remove break
  49.                 Dim length : length = UBound(parts)
  50.  
  51.                 If debug Then
  52.                     EventLog.Write(oClient.Username & " - DEBUG: Parts length = " & length + 1)
  53.                 End If
  54.            
  55.                 If length > 0 Then
  56.  
  57.                     parts(0) = parts(0) & "</body></html>"
  58.                     ' parts(0) <- Message
  59.                     If debug Then
  60.                         EventLog.Write(oClient.Username & " - DEBUG: Message = " & parts(0))
  61.                     End If
  62.                     ' parts(1) <- Signature
  63.                     If debug Then
  64.                         EventLog.Write(oClient.Username & " - DEBUG: Signature = " & parts(1))
  65.                     End If
  66.  
  67.                     Dim sigSet : sigSet = FALSE
  68.  
  69.                     If Not sigSet Then
  70.                         ' The following list contains HTML snippets that indicate the start of the first quote of the message BEHIND A BREAK to insert the signature right before it without a leading break. Snippets that start with an @ are regular expressions.
  71.                         ' Here is the description of each element in the same order:
  72.            ' - Quote begin in HTML mails sent with Microsoft Outlook 2010 / 2016
  73.                         ' - Quote begin in HTML mails sent with Microsoft Outlook Web App
  74.                         ' - Quote begin in HTML mails sent with Microsoft Mail for Windows 10
  75.                         ' - Quote begin in HTML mails sent with Mozilla Thunderbird
  76.            ' - Quote begin in HTML mails sent with AquaMail  
  77.                         sigSet = PlaceSignature(debug, oClient, oMessage, parts, Array("<div><div style='border:none;border-top:solid", "<hr tabindex=""-1"">", "&nbsp;</o:p></span></p><div style='mso-element:para-border-div;border:none;border-top:solid", "@<div[^>]*>\s*<p[^<]*>On(.|\r|\n)+? \d\d:\d\d:\d\d (.|\r|\n)+? wrote:</p>"), FALSE)
  78.                     End If
  79.  
  80.            
  81.                     If Not sigSet Then
  82.                         ' The following list contains HTML snippets that indicate the start of the first quote of the message WITHOUT A BREAK ahead to insert the signature with a leading break right before it. Snippets that start with an @ are regular expressions.
  83.                         ' Here is the description of each element in the same order:
  84.                         ' - Quote begin in HTML mails sent with Microsoft Outlook 2003
  85.                         ' - Quote begin in HTML mails sent with Microsoft Outlook 2010
  86.                         ' - Quote begin in TXT mails sent with Microsoft Outlook
  87.                         ' - Quote begin in TXT mails sent with Microsoft Outlook (German)
  88.                         ' - Quote begin in TXT mails sent with Mozilla Thunderbird
  89.                         ' - Quote begin in HTML mails sent with Messages & Mail for iPhone (German) / Quote begin in TXT mails sent with Mozilla Thunderbird (German)
  90.                         ' - Quote begin in HTML mails sent with RainLoop
  91.                         sigSet = PlaceSignature(debug, oClient, oMessage, parts, Array("@<hr size=""?2""? width=""?100%""? align=""?center""? tabindex=""?-1""?>", "<a name=""_MailEndCompose"">", "<br /><br />-----Original Message-----<br />From: ", "<br /><br />-----Ursprüngliche Nachricht-----<br />Von: ", "@<br /><br />On \d\d(\d\d)?[-/.]\d\d[-/.]\d\d(\d\d)? \d\d:\d\d, [^<]+? wrote:<br />>", "@<br>\s*<div class=""moz-cite-prefix"">", "@<div><br>Am \d\d[-/.]\d\d[-/.]\d\d\d\d um \d\d:\d\d schrieb ([^<]+ &lt;)?<a href=""mailto:", "@<br>[^,<]+, (""[^""]*"" &lt;)?<a target=""_blank"" tabindex=""-1"" muse_scanned=""true"" href=""mailto:"), TRUE)
  92.                     End If
  93.            
  94.                     If Not sigSet Then
  95.                         ' The following list contains HTML snippets that indicate the end of the message WITHOUT A BREAK ahead to insert the signature with a leading break right before it. Snippets that start with an @ are regular expressions.
  96.                         ' Here is the description of each element in the same order:
  97.                         ' - End in HTML mails sent with Microsoft Outlook (variant)
  98.                         ' - End with leading break (variant)
  99.                         ' - End with leading break (variant)
  100.                         ' - End with leading break (variant)
  101.                         ' - General end
  102.                         sigSet = PlaceSignature(debug, oClient, oMessage, parts, Array("<p class=MsoNormal><o:p>&nbsp;</o:p></p></body>", "<br></body>", "<br/></body>", "<br /></body>", "</body>"), TRUE)
  103.                     End If
  104.            
  105.                     If Not sigSet Then
  106.                         EventLog.Write(oClient.Username & " - ERROR: Signature could not be set, because no snippet was found!")
  107.                     End If
  108.  
  109.                     If debug Then
  110.                         EventLog.Write(oClient.Username & " - DEBUG: Final message = " & oMessage.HTMLBody)
  111.                     End If
  112.                
  113.                 Else
  114.                     EventLog.Write(oClient.Username & " - ERROR: Signature could not be splited off, because there is only one splited part!")
  115.                 End If
  116.  
  117.             Else
  118.                 EventLog.Write(oClient.Username & " - ERROR: Signature could not be splited off, because the split mark was not found!")
  119.             End If
  120.  
  121.         End If
  122.         EventLog.Write(oClient.Username & " - INFO: Mail sent.")
  123.  
  124.     End If
  125.  
  126.     oMessage.Save
  127.  
  128. End Sub
  129.  
  130. Function PlaceSignature(debug, oClient, oMessage, parts, possibleSnippetsToAddSignatureBefore, addLeadingBreak)
  131.  
  132.     Dim sigWrapBefore : sigWrapBefore = "<div class=""WordSection1""><p class=""MsoNormal"">"
  133.     Dim sigWrapAfter : sigWrapAfter = "</p></div>"
  134.  
  135.     For Each snippet In possibleSnippetsToAddSignatureBefore
  136.  
  137.         Dim snippetFound : snippetFound = FALSE
  138.  
  139.         If(StrComp(Left(snippet, 1), "@") = 0) Then ' Input snippet is RegExp?
  140.             Set regExp = New RegExp ' Regular expression to determine snippet
  141.             With regExp
  142.                 .Pattern    = Right(snippet, Len(snippet) - 1)
  143.                 .IgnoreCase = True
  144.                 .Global     = False
  145.             End With
  146.             Set regExpMatches = regExp.Execute(oMessage.HTMLBody) ' Gets a single match of the snippet
  147.             If regExpMatches.Count > 0 Then
  148.                 snippet = regExpMatches.Item(0).Value ' Convert regex snippet to normal snippet
  149.                 snippetFound = TRUE ' Message contains snippet
  150.             End If
  151.         Else
  152.             If (InStr(oMessage.HTMLBody, snippet) > 0) Then
  153.                 snippetFound = TRUE ' Message contains snippet
  154.             End If
  155.         End If
  156.  
  157.         If (snippetFound) Then ' Message contains snippet?
  158.             If debug Then EventLog.Write(oClient.Username & " - DEBUG: Found snippet = " & snippet)
  159.             ' Insert signature and add some tags and classes to display the signature in the same font as all clients do:
  160.             If addLeadingBreak Then
  161.                 oMessage.HTMLBody = Replace(parts(0), snippet, "<br />" & sigWrapBefore & parts(1) & sigWrapAfter & snippet, 1, 1, 1)
  162.             Else
  163.                 oMessage.HTMLBody = Replace(parts(0), snippet, sigWrapBefore & parts(1) & sigWrapAfter & snippet, 1, 1, 1)
  164.             End If
  165.             PlaceSignature = TRUE
  166.             Exit For
  167.         End If
  168.  
  169.     Next
  170.  
  171. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement