KySoto

MailWrapper class version5

Aug 10th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Compare Database
  2. 'MailWrapperVersion=5
  3. Option Explicit
  4. 'Do Not Move the above line
  5. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  6. ' Version 1 - 13/Mar/2017 - Changed the mail module to a class module         '
  7. ' Version 2 - 09/Feb/2018 - Added a parameter for the attachment in the       '
  8. '                           CreateMessage function.                           '
  9. ' Version 3 - 15/May/2018 - Added CSS constants to make it easier to create   '
  10. '                           HTML based emails. Also added Properties for those'
  11. '                           constants.                                        '
  12. ' Version 4 - 16/May/2018 - Added a tab to use without needing to code it out '
  13. '                           every time style inline,added font-size: 100% to  '
  14. '                           pCSS_PRE                                          '
  15. ' Version 5 - 17/May/2018 - fixed HtmlTab, pre was not properly using the     '
  16. '                           display: inline; property                         '
  17. '                                                                             '
  18. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  19. ' Requirements                                                                '
  20. ' -iwshruntimelibrary                                                         '
  21. ' -vbsendmail.dll installed                                                   '
  22. ' dbo_Email_Error_Log table                                                   '
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. 'Uncomment below line after update
  25. 'Private WithEvents objSendMail As vbSendMail.clsSendMail
  26. 'Enumerations''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  27. Public Enum EmailListType
  28.     StringArray = 0
  29.     TableNameString = 1
  30.     QueryNameString = 2
  31. End Enum
  32.  
  33. 'CSS Constants'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  34. Const pCSS_Table As String = "table{border-collapse: collapse;}table, th, td{border: 1px solid black;}th, td{padding: 5px;}"
  35. Const pCSS_Pre As String = "pre{display: inline;font-family: calabri;font-size: 100%;}"
  36.  
  37. Const pCSS_ALL As String = pCSS_Table & pCSS_Pre
  38.  
  39. 'Other text constants''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  40. Const pHtmlTab As String = "<span style=" & """" & "white-space: pre;" & """" & ">" & vbTab & "</span>"
  41.  
  42.  
  43.  
  44. 'Custom properties'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  45. Public Property Get CSS_ALL_WithStyleTags()
  46.     CSS_ALL_WithStyleTags = "<style>" & pCSS_ALL & "</style>"
  47. End Property
  48.  
  49. Public Property Get CSS_ALL_NoStyleTags()
  50.     CSS_ALL_NoStyleTags = pCSS_ALL
  51. End Property
  52.  
  53. Public Property Get CSS_TABLE()
  54.     CSS_TABLE = pCSS_Table
  55. End Property
  56.  
  57. Public Property Get CSS_PRE()
  58.     CSS_PRE = pCSS_Pre
  59. End Property
  60.  
  61. Public Property Get HtmlTab()
  62.     HtmlTab = pHtmlTab
  63. End Property
  64.  
  65. 'Events''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  66. Private Sub objSendMail_SendSuccessful()
  67.     Debug.Print "Email Success"
  68. End Sub
  69.  
  70. Private Sub objSendMail_SendFailed(Explanation As String)
  71.     Debug.Print "Email Failed"
  72.     Debug.Print Explanation
  73.     LogEmailError AppSpecific.AppID, Environ("COMPUTERNAME"), 4, , Explanation
  74. End Sub
  75.  
  76. Private Sub objSendMail_Status(Status As String)
  77.     'Debug.Print Status
  78. End Sub
  79.  
  80. Private Sub objSendMail_Progress(PercentComplete As Long)
  81.     'Debug.Print PercentComplete
  82. End Sub
  83.  
  84. Private Sub Class_Initialize()
  85.     Set objSendMail = New vbSendMail.clsSendMail
  86.     Debug.Print "Initilizing"
  87. End Sub
  88.  
  89. Private Sub Class_Terminate()
  90.     Set objSendMail = Nothing
  91.     Debug.Print "Terminating"
  92. End Sub
  93.  
  94. 'Functions'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  95. Public Function CreateMessage(ByVal argSubject As String, argMessage As String, argReplyto As String, argDisplay As String, argFrom As String, argEmailListSource As Variant, Optional argEmailListType As EmailListType = StringArray, Optional argAsHTML As Boolean = False, Optional argAttach As String) As Long
  96.     'sets all nessecery fields for a proper e-mail then passes the e-mail list to the function that compiles them into the format needed on a mail server.
  97.    'this function uses a string array as the e-mail source
  98.    'Outputs
  99.    '0 - function closed normally without any errors
  100.    '1 - vb send mail is not installed
  101.    '2 - somehow the destination emails is empty
  102.    '3 - Mail server unreachable
  103.    '4 - Send error with error sent via send failed event
  104.    '5 - unknown error
  105.    Dim ErrorCode As Long: ErrorCode = 0
  106.     If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
  107.     Dim strMailTo As String
  108.     strMailTo = FormatList(argEmailListSource, argEmailListType)
  109.     If strMailTo & "" = "" Then
  110.         Err.Raise vbObjectError + 1, "Email", "Email List Source is invalid"
  111.     End If
  112.  
  113.     If Not objSendMail.Ping("192.168.10.4") Then
  114.         Err.Raise vbObjectError + 2, "Email", "Unable to contact e-mail server"
  115.     End If
  116.     objSendMail.SMTPHost = "192.168.10.4"
  117.     objSendMail.UseAuthentication = False
  118.     objSendMail.RecipientDisplayName = strMailTo
  119.     objSendMail.ReplyToAddress = argReplyto
  120.     objSendMail.from = argFrom
  121.     objSendMail.FromDisplayName = argDisplay
  122.     objSendMail.Recipient = strMailTo
  123.     objSendMail.Subject = argSubject
  124.     objSendMail.Message = argMessage
  125.     objSendMail.AsHTML = argAsHTML
  126.     If Not IsNull(argAttach) Then
  127.         objSendMail.Attachment = argAttach
  128.     End If
  129.     objSendMail.Send
  130. Error_Handler_Exit:
  131.     CreateMessage = ErrorCode
  132.     Exit Function
  133. Error_Handler:
  134.     If Err.Number = vbObjectError + 1 Then
  135.         ErrorCode = 2
  136.     ElseIf Err.Number = vbObjectError + 2 Then
  137.         ErrorCode = 3
  138.     Else
  139.         ErrorCode = 5
  140.     End If
  141.     'nofail = False
  142.    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
  143.            "Error Number: " & Err.Number & vbCrLf & _
  144.            "Error Source: MailWrapper.CreateMessage" & vbCrLf & _
  145.            "Error Description: " & Err.Description _
  146.            , vbOKOnly + vbCritical, "An Error has Occured!"
  147.     Resume Error_Handler_Exit
  148. End Function
  149.  
  150. Public Function FormatList(argListSource As Variant, argListType As EmailListType) As String
  151.     'this compiles a list of e-mails from a string array
  152.    Dim strMailTo As String: strMailTo = ""
  153.     Dim rs As DAO.Recordset
  154.     Select Case argListType
  155.     Case StringArray
  156.         Dim v As Variant
  157.         For Each v In argListSource
  158.             strMailTo = strMailTo & ";" & v
  159.         Next
  160.     Case TableNameString
  161.         Set rs = dbLocal.TableDefs(argListSource).OpenRecordset(dbOpenDynaset, dbSeeChanges)
  162.         With rs
  163.             If .RecordCount > 0 Then
  164.                 .MoveFirst
  165.                 While Not .EOF
  166.                     strMailTo = strMailTo & ";" & .Fields(0).Value
  167.                     .MoveNext
  168.                 Wend
  169.             End If
  170.         End With
  171.     Case QueryNameString
  172.         Set rs = dbLocal.QueryDefs(argListSource).OpenRecordset(dbOpenDynaset, dbSeeChanges)
  173.         With rs
  174.             If .RecordCount > 0 Then
  175.                 .MoveFirst
  176.                 While Not .EOF
  177.                     strMailTo = strMailTo & ";" & .Fields(0).Value
  178.                     .MoveNext
  179.                 Wend
  180.             End If
  181.         End With
  182.     End Select
  183.     If strMailTo & "" <> "" Then
  184.         strMailTo = Mid(strMailTo, 2)
  185.     End If
  186.     FormatList = strMailTo
  187. End Function
  188.  
  189. Public Function LogEmailError(argAppID As Long, argHostName As String, argErrorCode As Long, Optional argUserID As String = "", Optional argExplination As String = "") As Long
  190.     Dim rs As DAO.Recordset
  191.     If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
  192.     Set rs = dbLocal.TableDefs("dbo_Email_Error_Log").OpenRecordset(dbOpenDynaset, dbSeeChanges)
  193.     With rs
  194.         .AddNew
  195.         .Fields("ApplicationID").Value = argAppID
  196.         .Fields("HostName").Value = argHostName
  197.         .Fields("ErrorCode").Value = argErrorCode
  198.         .Fields("DateTime").Value = Now()
  199.         .Fields("UserID").Value = argUserID
  200.         .Fields("Explination").Value = argExplination
  201.         .Update
  202.     End With
  203.     LogEmailError = 0
  204. Error_Handler_Exit:
  205.     Set rs = Nothing
  206.     Exit Function
  207. Error_Handler:
  208.     LogEmailError = -1
  209.     'nofail = False
  210.    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
  211.            "Error Number: " & Err.Number & vbCrLf & _
  212.            "Error Source: MailWrapper.LogEmailError" & vbCrLf & _
  213.            "Error Description: " & Err.Description _
  214.            , vbOKOnly + vbCritical, "An Error has Occured!"
  215.     Resume Error_Handler_Exit
  216.  
  217. End Function
Advertisement
Add Comment
Please, Sign In to add comment