Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub OnAcceptMessage(oClient, oMessage)
- Dim debug : debug = FALSE
- If oClient.username <> "" Then ' Only modify outgoing emails
- EventLog.Write(oClient.Username & " - INFO: Sending mail ...")
- If (Len(oMessage.HTMLBody) < 1 And Len(oMessage.body) > 0) Then ' Message is text only?
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Full Message (Text) = " & oMessage.body)
- End If
- Dim textSegments : textSegments = Split(oMessage.body, "<!-- TXTsig -->", 2, 1)
- Dim textMessage : textMessage = textSegments(0)
- Dim textSignature : textSignature = textSegments(1)
- textMessage = Replace(textMessage, "<", "<")
- textMessage = Replace(textMessage, ">", ">")
- textMessage = Replace(textMessage, vbNewLine, "<br />")
- oMessage.body = ""
- oMessage.HTMLBody = "<html><body>" & textMessage & "</body></html><br/>" & textSignature ' Convert plain text into HTML ...
- ' You have to add HTML signature into the TXT signature, but the signature has to start with <!-- TXTsig -->
- ' ALERT: Do this for domain and user signatures!
- End If
- If (Len(oMessage.HTMLBody) > 0) Then ' Message is not empty (for instance appointments are empty)
- Set regExp = New RegExp ' Regular expression to determine the end tags of the message
- With regExp
- .Pattern = "</body>[\r\n]*</html>[\r\n]*<br/>"
- .IgnoreCase = True
- .Global = False
- End With
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Full Message = " & oMessage.HTMLBody)
- End If
- Set splitMarkMatches = regExp.Execute(oMessage.HTMLBody) ' Gets a single match of the end tags of the message without signature
- If splitMarkMatches.Count > 0 Then ' Is there an end mark, so this is a HTML email?
- Dim splitMark : splitMark = splitMarkMatches.Item(0).Value
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Split mark = " & splitMark)
- End If
- Dim parts : parts = Split(oMessage.HTMLBody, splitMark) ' Split message and signature, remove break
- Dim length : length = UBound(parts)
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Parts length = " & length + 1)
- End If
- If length > 0 Then
- parts(0) = parts(0) & "</body></html>"
- ' parts(0) <- Message
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Message = " & parts(0))
- End If
- ' parts(1) <- Signature
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Signature = " & parts(1))
- End If
- Dim sigSet : sigSet = FALSE
- If Not sigSet Then
- ' 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.
- ' Here is the description of each element in the same order:
- ' - Quote begin in HTML mails sent with Microsoft Outlook 2010 / 2016
- ' - Quote begin in HTML mails sent with Microsoft Outlook Web App
- ' - Quote begin in HTML mails sent with Microsoft Mail for Windows 10
- ' - Quote begin in HTML mails sent with Mozilla Thunderbird
- ' - Quote begin in HTML mails sent with AquaMail
- sigSet = PlaceSignature(debug, oClient, oMessage, parts, Array("<div><div style='border:none;border-top:solid", "<hr tabindex=""-1"">", " </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)
- End If
- If Not sigSet Then
- ' 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.
- ' Here is the description of each element in the same order:
- ' - Quote begin in HTML mails sent with Microsoft Outlook 2003
- ' - Quote begin in HTML mails sent with Microsoft Outlook 2010
- ' - Quote begin in TXT mails sent with Microsoft Outlook
- ' - Quote begin in TXT mails sent with Microsoft Outlook (German)
- ' - Quote begin in TXT mails sent with Mozilla Thunderbird
- ' - Quote begin in HTML mails sent with Messages & Mail for iPhone (German) / Quote begin in TXT mails sent with Mozilla Thunderbird (German)
- ' - Quote begin in HTML mails sent with RainLoop
- 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 ([^<]+ <)?<a href=""mailto:", "@<br>[^,<]+, (""[^""]*"" <)?<a target=""_blank"" tabindex=""-1"" muse_scanned=""true"" href=""mailto:"), TRUE)
- End If
- If Not sigSet Then
- ' 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.
- ' Here is the description of each element in the same order:
- ' - End in HTML mails sent with Microsoft Outlook (variant)
- ' - End with leading break (variant)
- ' - End with leading break (variant)
- ' - End with leading break (variant)
- ' - General end
- sigSet = PlaceSignature(debug, oClient, oMessage, parts, Array("<p class=MsoNormal><o:p> </o:p></p></body>", "<br></body>", "<br/></body>", "<br /></body>", "</body>"), TRUE)
- End If
- If Not sigSet Then
- EventLog.Write(oClient.Username & " - ERROR: Signature could not be set, because no snippet was found!")
- End If
- If debug Then
- EventLog.Write(oClient.Username & " - DEBUG: Final message = " & oMessage.HTMLBody)
- End If
- Else
- EventLog.Write(oClient.Username & " - ERROR: Signature could not be splited off, because there is only one splited part!")
- End If
- Else
- EventLog.Write(oClient.Username & " - ERROR: Signature could not be splited off, because the split mark was not found!")
- End If
- End If
- EventLog.Write(oClient.Username & " - INFO: Mail sent.")
- End If
- oMessage.Save
- End Sub
- Function PlaceSignature(debug, oClient, oMessage, parts, possibleSnippetsToAddSignatureBefore, addLeadingBreak)
- Dim sigWrapBefore : sigWrapBefore = "<div class=""WordSection1""><p class=""MsoNormal"">"
- Dim sigWrapAfter : sigWrapAfter = "</p></div>"
- For Each snippet In possibleSnippetsToAddSignatureBefore
- Dim snippetFound : snippetFound = FALSE
- If(StrComp(Left(snippet, 1), "@") = 0) Then ' Input snippet is RegExp?
- Set regExp = New RegExp ' Regular expression to determine snippet
- With regExp
- .Pattern = Right(snippet, Len(snippet) - 1)
- .IgnoreCase = True
- .Global = False
- End With
- Set regExpMatches = regExp.Execute(oMessage.HTMLBody) ' Gets a single match of the snippet
- If regExpMatches.Count > 0 Then
- snippet = regExpMatches.Item(0).Value ' Convert regex snippet to normal snippet
- snippetFound = TRUE ' Message contains snippet
- End If
- Else
- If (InStr(oMessage.HTMLBody, snippet) > 0) Then
- snippetFound = TRUE ' Message contains snippet
- End If
- End If
- If (snippetFound) Then ' Message contains snippet?
- If debug Then EventLog.Write(oClient.Username & " - DEBUG: Found snippet = " & snippet)
- ' Insert signature and add some tags and classes to display the signature in the same font as all clients do:
- If addLeadingBreak Then
- oMessage.HTMLBody = Replace(parts(0), snippet, "<br />" & sigWrapBefore & parts(1) & sigWrapAfter & snippet, 1, 1, 1)
- Else
- oMessage.HTMLBody = Replace(parts(0), snippet, sigWrapBefore & parts(1) & sigWrapAfter & snippet, 1, 1, 1)
- End If
- PlaceSignature = TRUE
- Exit For
- End If
- Next
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement