Link to home
Create AccountLog in
Avatar of bogorman
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(CCGetUserID(), 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("Persits.MailSender")
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@lifecharity.org.uk"
'Mail.FromName = FullLoginName
Mail.FromName = "Matthew O'Gorman, LIFE London"


strSQL = "SELECT tblselectedschools.fldContact, tblselectedschools.fldemail, tblselectedschools.fldRecordID FROM tblschools RIGHT JOIN tblselectedschools ON tblschools.fldSCHOOL_ID = tblselectedschools.fldSCHOOL_ID WHERE tblselectedschools.fldUserID = " & UserID & " AND tblselectedschools.fldSelected = 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.
ASKER CERTIFIED SOLUTION
Avatar of golfDoctor
golfDoctor

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of bogorman
bogorman

ASKER

Hi,
Sorry for the delay in responding.
You were quite right. Have altered the coding and it works.
Have assigned the points.
Thanks
Brian