Public Sub thisEmail_SendSMTP(ByVal infile As String, ByVal inuser As String, Optional ByVal bigMsg As Variant = "") Dim stSubject As String ' Dim stName As String ' Dim stSender As String ' Dim prevstMessage As Variant ' Dim stMessage As Variant ' Dim stHelpDesk As String ' Dim stFinished As String ' Dim rs As DAO.Recordset ' Dim stSql As Variant ' Dim stAsset As String ' Dim stPrefix As String ' Dim stSuffix As String ' Dim stSN As String ' Dim stList As String ' Dim objMessage Dim stLeaveDate As String Dim stRequestor As String Dim stOTN As String Dim stCSVHdr As String Dim badMsg As String Dim goodMsg As String Dim noMsg As String goodMsg = "Your " & ftableOfInterest & " updates have been emailed." badMsg = "SMTP " & ftableOfInterest & " email not sent because of error." noMsg = "No updates this session, so no emails sent." stOTN = "" stCSVHdr = "" prevstMessage = "" 'stLeaveDate = InputBox("Please enter the date this employee" & vbCrLf & "is scheduled to leave the agency") 'stRequestor = InputBox("Please enter the name of the person requesting clearance:") ''''stSQL = "Select * from Query1 Where UserID = " & Me.UserID ' ''stSql = "SELECT UserAccess.UserName, UserAccess.OtherTableName, UserAccess.CSVHeader, UserAccess.CSVBodySQL, UserAccess.to, UserAccess.cc, UserAccess.bcc, UserAccess.from, UserAccess.subject, UserAccess.editmessage, UserAccess.templatefile, UserAccess.bodyprefix, UserAccess.bodysuffix, ('" + Chr$(34) + "'+" & ftableOfInterest & "." & ftextField & "+'" + Chr$(34) + "," + Chr$(34) + "'+Format(" & ftableOfInterest & "." & famendDateField & "," + Chr$(34) + "dd/mm/yyyy:hh:nn:ss" + Chr$(34) + ")+" + "'" + Chr$(34) + "') AS QString1 " 'stSql = "SELECT UserAccess.*, ('" + Chr$(34) + "'+" & ftableOfInterest & "." & ftextField & "+'" + Chr$(34) + "," + Chr$(34) + "'+Format(" & ftableOfInterest & "." & famendDateField & "," + Chr$(34) + "dd/mm/yyyy:hh:nn:ss" + Chr$(34) + ")+" + "'" + Chr$(34) + "') AS QString1 " 'stSql = stSql + ShowAll(stSql) 'stSql = stSql + " FROM " & ftableOfInterest & ", UserAccess " 'stSql = stSql + " WHERE " & ftableOfInterest & "." & famendDateField & ">=UserAccess.SessionStartDate And " & ftableOfInterest & "." & ftextField & ">' ' And " & ftableOfInterest & "." & fuserNameField & "=UserAccess.UserName;" stSql = "SELECT [UserAccess].*" If firstgo = False Then stSql = stSql + ", ('" + Chr$(34) + "'+[" + ftempPrefix + tableOfInterest + "].[my" & ftextField & "]+'" + Chr$(34) + "," + Chr$(34) + "'+Format([" + ftempPrefix + tableOfInterest + "].[my" & famendDateField & "]," + Chr$(34) + "dd/mm/yyyy:hh:nn:ss" + Chr$(34) + ")+" + "'" + Chr$(34) + "') AS QString1 " stSql = stSql + ShowAll(stSql) End If stSql = stSql + " FROM UserAccess" If firstgo = False Then stSql = stSql + ",[" + ftableOfInterest + "], [" + ftempPrefix + tableOfInterest + "] " stSql = stSql + canbeslowWhereClause(" WHERE [" & ftempPrefix & tableOfInterest & "].[my" & findxField & "] = [" & ftableOfInterest & "].[" & findxField & "] AND [" + ftempPrefix + tableOfInterest + "].[my" & famendDateField & "]>=UserAccess.SessionStartDate And [" + ftempPrefix + tableOfInterest + "].[my" & ftextField & "]>' ' And [" + ftempPrefix + tableOfInterest + "].[my" & fuserNameField & "]=UserAccess.UserName", prevtableOfInterest) & ";" End If stPrefix = "" stSuffix = "" stSubject = "" ' "Exiting Employee" ' stSender = "rmetcalfe15@gmail.com" ' "robert@conceptpaints.com.au" ' "account@YourDomain.Com" ' stName = "" ' Me.FirstName & " " & Me.LastName & " (" & Me.LOGIN_NAME & ")" ' stMessage = "" ' "Please retrieve the following items from " ' stHelpDesk = "rmetcalfe15@gmail.com" ' "robert@conceptpaints.com.au" ' "Helpdesk@YourDomain.com" ' stFinished = goodMsg ' "The HelpDesk has been notified." ' Dim anError As Integer anError = 0 'Set rs = New ADODB.Recordset ' 'rs.Open stSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic ' On Error GoTo ResumeMuchLater Set objMessage = CreateObject("CDO.Message") On Error GoTo 0 If anError = 0 Then Set rs = DBEngine(0)(0).OpenRecordset(stSql) Dim rsshowall As String Dim ourbodysql As Variant rsshowall = "" If Not rs.EOF Then On Error GoTo NotSoBad rsshowall = rs!ShowAll If rsshowall <> "" Then If rsshowall <> "0" Then fshowall = True End If End If On Error GoTo 0 ourbodysql = rs!CSVBodySQL If UCase(tableOfInterest) = "COLOURDETAILS" Then ourbodysql = rs!CSVBodyColDtlSQL ElseIf UCase(tableOfInterest) = "PRODUCTS" Then ourbodysql = rs!CSVBodyProdSQL ElseIf UCase(tableOfInterest) = "MAKE" Then ourbodysql = rs!CSVBodyMakeSQL End If If rs!firstgo = "1" And ourbodysql > " " Then firstgo = True ourbodysql = ourbodysql + "Full" If bigMsg <> "" Then Set rs = DBEngine(0)(0).OpenRecordset(CurrentDb.QueryDefs(ourbodysql).SQL) End If ourbodysql = "" Else firstgo = False End If If rs!UserName = inuser Then If rs!To <> "" Then stHelpDesk = rs!To End If If rs!cc <> "" Then objMessage.cc = rs!cc End If If rs!bcc <> "" Then objMessage.bcc = rs!bcc End If If rs!Subject <> "" Then If firstgo = True Then stSubject = rs!Subject & " - First Run" Else stSubject = rs!Subject End If End If If rs!FROM <> "" Then stSender = rs!FROM End If If rs!bodyprefix <> "" Then stPrefix = rs!bodyprefix End If If rs!bodysuffix <> "" Then stSuffix = rs!bodysuffix End If If rs!CSVHeader <> "" Then stCSVHdr = Replace(Replace(Replace((rs!CSVHeader & fhnotusual), "#@#", Chr(34)), "!*!", Chr(34)), "|", ",") + Chr(13) + Chr(10) End If 'stAsset = rs!AssetCategory ' 'stSN = rs!SerialNumber ' 'stList = stList & vbCrLf & stAsset & " (SN:" & stSN & ")" ' End If If ourbodysql <> "" Then Dim tableSql As String Dim tableSqlV As Variant tableSqlV = ourbodysql If InStr(tableSqlV, "SELECT ") <= 0 And tableSqlV > "." Then tableSqlV = CurrentDb.QueryDefs(ourbodysql).SQL ourbodysql = ReplaceV(ReplaceV(ReplaceV(tableSqlV, Chr(13) & Chr(10), " "), Chr(10), " "), Chr(13), " ") End If tableSqlV = ReplaceV(ReplaceV(ReplaceV(ourbodysql, "#@#", Chr(34)), "!*!", Chr(34)), "|", ",") rs.Close Set rs = Nothing ourbodysql = ShowAll(tableSqlV) Set rs = DBEngine(0)(0).OpenRecordset(ourbodysql) End If End If If bigMsg = "" Then Do Until rs.EOF stMessage = stMessage + rs!QString1 On Error GoTo NotSoBad stMessage = stMessage + rs!QString2 stMessage = stMessage + rs!QString3 stMessage = stMessage + rs!QString4 stMessage = stMessage + rs!QString5 stMessage = stMessage + rs!QString6 stMessage = stMessage + rs!QString7 stMessage = stMessage + rs!QString8 stMessage = stMessage + rs!QString9 On Error GoTo 0 If InStr(stMessage, (Chr(7) & "-32765,")) <= 0 And InStr(stMessage, (",-32765,")) <= 0 Then stMessage = ReplaceV(stMessage, Chr(7), "") + Chr(13) + Chr(10) Else stMessage = prevstMessage End If If rs!UserName = inuser Then If rs!To <> "" Then stHelpDesk = rs!To End If If rs!cc <> "" Then objMessage.cc = rs!cc End If If rs!bcc <> "" Then objMessage.bcc = rs!bcc End If If rs!Subject <> "" Then If firstgo = True Then stSubject = rs!Subject & " - First Run" Else stSubject = rs!Subject End If End If If rs!FROM <> "" Then stSender = rs!FROM End If If rs!bodyprefix <> "" Then stPrefix = rs!bodyprefix End If If rs!bodysuffix <> "" Then stSuffix = rs!bodysuffix End If If rs!CSVHeader <> "" Then stCSVHdr = Replace(Replace(Replace((rs!CSVHeader & fhnotusual), "#@#", Chr(34)), "!*!", Chr(34)), "|", ",") + Chr(13) + Chr(10) End If 'stAsset = rs!AssetCategory ' 'stSN = rs!SerialNumber ' 'stList = stList & vbCrLf & stAsset & " (SN:" & stSN & ")" ' End If prevstMessage = stMessage rs.MoveNext Loop Else stMessage = bigMsg End If If UCase(tableOfInterest) = "COLOURDETAILS" Then stSubject = stSubject & " - Colour Details - " & Replace(infile, Replace(Replace(Environ("TEMP") + "\*", "\\*", "\"), "\*", "\"), "") ElseIf UCase(tableOfInterest) = "PRODUCTS" Then stSubject = stSubject & " - Products - " & Replace(infile, Replace(Replace(Environ("TEMP") + "\*", "\\*", "\"), "\*", "\"), "") ElseIf UCase(tableOfInterest) = "MAKE" Then stSubject = stSubject & " - Make - " & Replace(infile, Replace(Replace(Environ("TEMP") + "\*", "\\*", "\"), "\*", "\"), "") ElseIf UCase(tableOfInterest) = "JOBS" Then stSubject = stSubject & " - Jobs - " & Replace(infile, Replace(Replace(Environ("TEMP") + "\*", "\\*", "\"), "\*", "\"), "") End If Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). Const cdoAnonymous = 0 'Do not authenticate Const cdoBasic = 1 'basic (clear-text) authentication Const cdoNTLM = 2 'NTLM objMessage.Subject = stSubject ' & ": " & stName objMessage.Sender = stSender objMessage.To = stHelpDesk Dim stBig As Variant stBig = stMessage stBig = stPrefix & stCSVHdr & stMessage & stSuffix objMessage.TextBody = stBig ' & stName & " leaving " & stLeaveDate & ":" & _ ' vbCrLf & "Requesting Clearance: " & stRequestor & vbCrLf & stList On Error GoTo NotSoBad objMessage.AddAttachment infile On Error GoTo 0 objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" 'Type of authentication, NONE, Basic (Base64 encoded), NTLM objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic 'Your UserID on the SMTP server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "rmetcalfe15" 'Your password on the SMTP server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = smtppass 'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 ' 25 'Use SSL for the connection (False or True) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True ' False 'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 objMessage.Configuration.Fields.Update '==End remote SMTP server configuration section== If stMessage <> "" Then On Error GoTo NotSoGood objMessage.Send If stFinished <> badMsg Then Dim fso Set fso = CreateObject("Scripting.FileSystemObject") On Error GoTo 0 If fso.FileExists(infile) Then If ftpmode <= 0 Then If InStr(UCase(infile), UCase(Environ("TEMP"))) > 0 Then fso.DeleteFile (infile) End If End If End If Set fso = Nothing End If Else stFinished = noMsg End If On Error GoTo 0 If showmbox = True Then MsgBox stFinished End If End If Exit Sub NotSoBad: Resume Next ResumeMuchLater: anError = 1 Resume Next NotSoGood: stFinished = badMsg Resume Next End Sub