Hi all.
I have CDO working well to send a test message to the recipients of a table (This will change later to an on demand query)
What I'd like to do is use a textfile template for the message which will be populated from the recordset.
What do I need to do with the code to make this work and is there anything special I need to do with the textfile in terms of placing the requisite data from the recordset?
I have CDO working well to send a test message to the recipients of a table (This will change later to an on demand query)
What I'd like to do is use a textfile template for the message which will be populated from the recordset.
What do I need to do with the code to make this work and is there anything special I need to do with the textfile in terms of placing the requisite data from the recordset?
Code:
Option Compare Database
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM
Public Sub SendEmail()
Dim rs As DAO.Recordset
Dim imsg As Object
Dim iconf As Object
Dim flds As Object
Dim schema As String
Set imsg = CreateObject("CDO.Message")
Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields
' send one copy with SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = cdoSendUsingPort
flds.Item(schema & "smtpserver") = "serverdetails.co.uk"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = cdoBasic
flds.Item(schema & "sendusername") = "me@home.com"
flds.Item(schema & "sendpassword") = "12345780"
flds.Item(schema & "smtpusessl") = False
flds.Update
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Customers") 'This is where the records come from, most likely a query run on demand
If Not (rs.EOF And rs.BOF) Then
Do Until rs.EOF = True
'On Error Resume Next
With imsg
.To = rs.Fields("Email")
.From = """SENDER"" <XXX@somewhere.com>"
.Subject = rs.Fields("Event")
.TextBody = "Test Message"
Set .Configuration = iconf
.Send
End With
rs.MoveNext
Loop
Set iconf = Nothing
Set imsg = Nothing
Set flds = Nothing
End If
MsgBox "Finished looping through records, all emails have been sent."
rs.Close 'Close the recordset
End Sub