bogorman
asked on
Sending multiple emails by iterating through recordset
Am trying to write a vbscript to send multiple emails but cannot get it to work. Here it is:
Dim Mail
Dim objCDOSYSCon , objCDOSYSMail, BodyString, ResponseString, myRS, db, strSQL, strSQL2
Dim mySQLWhere, MonthString, Connection, FullLoginName, Prompt, UserID, myCmd, myCmd2, RecordID
Set Connection = New clsDBConnection1
Connection.Open
UserID = Connection.ToSQL(CCGetUser ID(), ccsInteger)
BodyString = "This is a test email sent via LIFEnet"
'This prevents the form from being Cached.
Response.Expires = 0
Response.Expiresabsolute = Now-1
'Create the MailSender Object
Set Mail = Server.CreateObject("Persi ts.MailSen der")
Set myCmd = Server.CreateObject("ADODB .Command")
Set myCmd2 = Server.CreateObject("ADODB .Command")
Set myRS = Server.CreateObject("ADODB .Recordset ")
'Specify your SMTP Mail Server
Mail.Host = "mailA28.webcontrolcenter. com"
'Specify the From Address and Name
Mail.From = "matthewo'gorman@lifechari ty.org.uk"
'Mail.FromName = FullLoginName
Mail.FromName = "Matthew O'Gorman, LIFE London"
strSQL = "SELECT tblselectedschools.fldCont act, tblselectedschools.fldemai l, tblselectedschools.fldReco rdID FROM tblschools RIGHT JOIN tblselectedschools ON tblschools.fldSCHOOL_ID = tblselectedschools.fldSCHO OL_ID WHERE tblselectedschools.fldUser ID = " & UserID & " AND tblselectedschools.fldSele cted = 1"
With myCmd
'.ActiveConnection = Connection
.CommandText = strSQL ' This should be your SQL select statement
.CommandType = 1 'adCmdText
End With
strSQL2 = "UPDATE tblselectedschools SET fldmessagesent=0 WHERE fldID = " & RecordID
With myCmd2
'.ActiveConnection = Connection
.CommandText = strSQL ' This is the SQL statement for UPDATE query to change the fldmessagesent field
.CommandType = 1 'adCmdText
End With
myRS.Open myCmd
firstRecord=1
Do While Not myRS.EOF
RecordID = myRS.Fields("fldRecordID")
if Mail.Send then
' Message sent sucessfully
If firstRecord = 1 Then
firstRecord = 0
Mail.AddRecipient myRS.Fields("fldContact"), myRS.Fields("fldemail")
Else
Mail.AddBCC myRS.Fields("fldContact"), myRS.Fields("fldemail")
End If
Else
' Message send failure
'WRITE 0 TO fldmessagesent in tblselectedschools
fldCurrentID = myRS.Fields("fldID")
myRS.Open myCmd2
myRS.Close myCmd2
Connection.Close
Set Connection = Nothing
On Error Goto 0
end if
myRS.MoveNext()
Loop
MyFile.Close()
myConn.Close()
When opening the page, I get:
The connection cannot be used to perform this operation. It is either closed or invalid in this context
on the firstRecord=1 line.
Possibly the problem is due to trying to use the mail connection to open the recordset, and I should define two connections - I am far from being an expert here.
The coding should iterate through the recordset and, where emails can't be sent write to the fldmessagesent field in tblselectedschools. I am not sure whether this is valid. If an email cannot be sent, would this be detected immediately?
Anyway, would appreciate help on this.
Dim Mail
Dim objCDOSYSCon , objCDOSYSMail, BodyString, ResponseString, myRS, db, strSQL, strSQL2
Dim mySQLWhere, MonthString, Connection, FullLoginName, Prompt, UserID, myCmd, myCmd2, RecordID
Set Connection = New clsDBConnection1
Connection.Open
UserID = Connection.ToSQL(CCGetUser
BodyString = "This is a test email sent via LIFEnet"
'This prevents the form from being Cached.
Response.Expires = 0
Response.Expiresabsolute = Now-1
'Create the MailSender Object
Set Mail = Server.CreateObject("Persi
Set myCmd = Server.CreateObject("ADODB
Set myCmd2 = Server.CreateObject("ADODB
Set myRS = Server.CreateObject("ADODB
'Specify your SMTP Mail Server
Mail.Host = "mailA28.webcontrolcenter.
'Specify the From Address and Name
Mail.From = "matthewo'gorman@lifechari
'Mail.FromName = FullLoginName
Mail.FromName = "Matthew O'Gorman, LIFE London"
strSQL = "SELECT tblselectedschools.fldCont
With myCmd
'.ActiveConnection = Connection
.CommandText = strSQL ' This should be your SQL select statement
.CommandType = 1 'adCmdText
End With
strSQL2 = "UPDATE tblselectedschools SET fldmessagesent=0 WHERE fldID = " & RecordID
With myCmd2
'.ActiveConnection = Connection
.CommandText = strSQL ' This is the SQL statement for UPDATE query to change the fldmessagesent field
.CommandType = 1 'adCmdText
End With
myRS.Open myCmd
firstRecord=1
Do While Not myRS.EOF
RecordID = myRS.Fields("fldRecordID")
if Mail.Send then
' Message sent sucessfully
If firstRecord = 1 Then
firstRecord = 0
Mail.AddRecipient myRS.Fields("fldContact"),
Else
Mail.AddBCC myRS.Fields("fldContact"),
End If
Else
' Message send failure
'WRITE 0 TO fldmessagesent in tblselectedschools
fldCurrentID = myRS.Fields("fldID")
myRS.Open myCmd2
myRS.Close myCmd2
Connection.Close
Set Connection = Nothing
On Error Goto 0
end if
myRS.MoveNext()
Loop
MyFile.Close()
myConn.Close()
When opening the page, I get:
The connection cannot be used to perform this operation. It is either closed or invalid in this context
on the firstRecord=1 line.
Possibly the problem is due to trying to use the mail connection to open the recordset, and I should define two connections - I am far from being an expert here.
The coding should iterate through the recordset and, where emails can't be sent write to the fldmessagesent field in tblselectedschools. I am not sure whether this is valid. If an email cannot be sent, would this be detected immediately?
Anyway, would appreciate help on this.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Sorry for the delay in responding.
You were quite right. Have altered the coding and it works.
Have assigned the points.
Thanks
Brian