Click to See Complete Forum and Search --> : Possible to put in a loop?
sanjuT
02-08-2005, 10:23 AM
i use something like this for sending e-mails:
if request.Form ("checkboxA") = 1 then
Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
objCDOMail.To = "a@b.com" 'the destination
objCDOMail.From = "c@d.com" 'the sender
objCDOMail.Subject = "testSubject"
objCDOMail.Body = "testBody"
objCDOMail.BodyFormat = 1
objCDOMail.Importance = 2
objCDOMail.Send 'fire off the email
set objCDOMail = nothing
end if
Now i have a form where there are about 50 checkboxes for selecting the e-mail destination (don't ask, it wasn't my idea!).
Can i put this code in a loop for checking which boxes are checked?
I have tried, but someone here said that CDONTS can't do that, that i have to repeat this loop for each e-mail possibility.
Any insight would help, thanks!
lmf232s
02-08-2005, 11:41 AM
no you could do a loop.
You might need to rename your checkboxes to get this to work.
The deal with changing the textboxes is that you will have
checkbox_1 - checkbox_50
By nameing them like this you can reference each checkbox by a
value of 1 or i. Anyway the folling loop would work assuming that
you have your checkboxs named in a way that you can loop them
like this.
For i = 1 to 50
If request.form("checkbox_"<%=i%>) = 1 then
Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
objCDOMail.To = "a@b.com" 'the destination
objCDOMail.From = "c@d.com" 'the sender
objCDOMail.Subject = "testSubject"
objCDOMail.Body = "testBody"
objCDOMail.BodyFormat = 1
objCDOMail.Importance = 2
objCDOMail.Send 'fire off the email
set objCDOMail = nothing
end if
Next
sanjuT
02-08-2005, 01:41 PM
how would the loop know which e-mail adress to send to?
Eah of the 50 have different addresses.
maybe it's easier to just repeat the loop for each address. will this affect performance much?
lmf232s
02-08-2005, 03:07 PM
Copy this code and execute it, and you will see how i did it.
<%option explicit%>
<%
Dim hidsubmit
Dim i
Dim objCDOMail
hidsubmit = Request.Form("hidsubmit")
Select Case hidsubmit
Case "Send"
For i = 1 to 5
'Response.Write Request.Form("Checkbox_" & i) & "<BR>"
if Request.Form("Checkbox_" & i) <> "" then
Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
objCDOMail.To = "larryf@idinet.com"
objCDOMail.From = Request.Form("Checkbox_" & i)
objCDOMail.Subject = "This is the Subject"
objCDOMail.Body = "My Body is so fresh and so clean clean."
objCDOMail.BodyFormat = 1
objCDOMail.Importance = 2
'objCDOMail.Send
set objCDOMail = nothing
Response.Write " Email Sent to " & Request.Form("Checkbox_" & i) & "<BR>"
end if
next
Case else
'Pay no attention to the hidsubmit and select Case. This is just how i submit pages
'and decide what action to take. The 1 thing i wanted you to see is that you can
'do this. It comes down to naming your textboxes like i did, so that you can easily
'loop through them. This time i set the value of the checkbox to the email address.
'So if you were going to type this for 50 checkboxs, i would use a loop to create all 50 textboxs.
'So your body would be. This also assumes that you are getting your email address from
'a database.
'For i = 1 to 50
' Response.Write "<input type=checkbox name=""checkbox_" & i & """ value="" & objRS("EmailAddress") & "">" & objRS("EmailAddress") & "<br>"
'Next
End Select
%>
<form name=form method=post>
<input type=hidden name=hidsubmit value="">
<input type=checkbox name=Checkbox_1 value="lmf232s@yahoo.com">lmf232s@yahoo.com<br>
<input type=checkbox name=Checkbox_2 value="Joe@yahoo.com">Joe@yahoo.com<br>
<input type=checkbox name=Checkbox_3 value="Chris@yahoo.com">Chris@yahoo.com<br>
<input type=checkbox name=Checkbox_4 value="Mary@yahoo.com">Mary@yahoo.com<br>
<input type=checkbox name=Checkbox_5 value="Yahoo@yahoo.com">Yahoo@yahoo.com<br>
<input type=button name=cmdSend value="Send" onclick="document.form.hidsubmit.value='Send';document.form.submit();">
</form>