Updated: 01/11/2000 - I have had quite a few amendments sent to me on this article (it's good to see that people aren't simply copying it word for word). Now the code should run more smoothly and has been further tested with the CDONTS method. Thanks to Chris Knipe and Todd Taylor amongst others. Last of all, I will soon be creating a new script that sends the e-mails separately so recipients cannot see eachothers e-mail addresses. If anyone has any ideas please feel free to e-mail me, your name will be displayed as a contributor. This article will show you how to place variables into an array and from that array, add each recipient to an e-mail. If you do not have much experience in using the CDONTS mail component or the AspMail component please learn the basics before you read this article. Please change the text in the red writing. If you do have any questions about any other ASP or WAP/WML related problems please feel free to e-mail me at michael_wright@lineone.com and if you are a Manager, Supervisor or Director please feel free the offer me a job. So here’s the code: '----------------------------------------- 'Multiple-newsletter e-mailer using database 'Language: VBscript 'Date: 21/08/2000 'Author: Michael Wright '----------------------------------------- 'THIS SCRIPT WILL FIRST STORE THE E-MAIL NAMES INTO 'AN ARRAY FROM AN MS ACCESS DATABASE. 'INITIALISE VARIABLES AND SET OBJECTS dim conn dim rs dim strsql dim strconn strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("LOCATION OF DATABASE") set conn = server.createobject("adodb.connection") conn.open strconn set rs = server.createobject("adodb.recordset") 'SET COUNTER VARIABLE TO 0 intCount = 0 'CHANGE SQL TO YOUR OWN DATABASE TABLES Query = "SELECT EmailAddressField FROM Database1 SORT BY ID" 'OPEN AND CONNECT TO DATABASE, QUERY DATABASE rs.open Query, conn 'debug: Response.Write(Query) if not rs.eof Then rs.movefirst Do While NOT rs.EOF 'INCREMENT COUNTER intCount = intCount + 1 'PLACE RECORDSET INTO ARRAY Emails = rs.getrows() rs.movenext Loop else response.write("No records found.") end if 'CLOSE RECORDS AND SET OBJECTS TO NULL rs.close set rs= nothing set conn = nothing set strconn = nothing Now it’s time to create the e-mail, add the addresses from the array and then send it. If you have the AspMail component active on your system use the script below but if you would prefer to use the CDONTS component then use the script after. Set Mailer = CreateObject("SMTPsvg.Mailer") 'ADD YOUR SMTP MAILSERVER LOCATION Mailer.RemoteHost = "MAILSERVER LOCATION" 'ADD WHO YOU WANT THE SENDER TO BE E.G. 'website@aarogya.com Mailer.FromAddress = "FROM ADDRESS" 'THIS ADDS ALL THE RECIPENTS TO THE EMAIL If intCount <> 0 Then Do While i <> intCount 'CHANGE THE FIRST DIMENSION OF THE ARRAYS TO SUIT YOUR 'DATABASE BUT IT SHOULD BE A FIELD NUMBER E.G. 0 THE FIRST FIELD Mailer.AddRecipient Emails(FIELD OF RECIPENTS NAME,i), Emails(FIELD OF RECIPENTS EMAIL ADDRESS,i) i = i + 1 Loop Else Mailer.AddRecipient "YOUR OWN NAME", "YOUR OWN ADDRESS" End If Mailer.Subject = "YOUR E-MAILS SUBJECT" 'PLACE YOUR OWN EMAIL MESSAGE HERE Mailer.BodyText = "YOUR E-MAILS BODY TEXT" 'SEND EMAIL If Mailer.SendMail Then Response.Write "Mail has been sent..." else Response.Write "Mail send failure. Error was " & Mailer.Response end if 'SET MAILER TO NOTHING Set Mailer = nothing 'END OF SCRIPT Here is the code for the CDONTS method of the above. 'Declare local variables to hold the data from the Input form page that is used above. Dim strTo Dim strSubject, Dim strBody 'Strings for recipient, subject, boby Dim objCDOMail 'The CDO object ' Create an instance of the NewMail object. Set objCDOMail = Server.CreateObject("CDONTS.NewMail") ' Set the properties of the object objCDOMail.From = "YOUR FROM ADDRESS" objCDOMail.To = "YOUR TEST ADDRESS TO CHECK E-MAIL HAS BEEN SENT" 'THIS ADDS ALL THE RECIPENTS TO THE EMAIL strEmailStart = "" IF intCount <> 0 THEN Do While i <> intCount 'CHANGE THE FIRST DIMENSION OF THE ARRAYS TO SUIT YOUR DATABASE BUT IT SHOULD BE A FIELD NUMBER E.G. 0 THE FIRST FIELD strEmailStart = strEmailStart & strToEmail(FIELD OF RECIPENTS EMAIL ADDRESS,i) & ";" i = i + 1 Loop ELSE objCDOMail.Cc = "myemail@email.com" END IF 'debug: Response.Write strEmailStart objCDOMail.Cc = strEmailStart
objCDOMail.Subject = "YOUR E-MAILS SUBJECT" objCDOMail.Body = "YOUR E-MAILS BODY TEXT" ' There are lots of other properties you can use. ' You can send HTML e-mail, attachments, etc... ' You can also modify most aspects of the message ' like importance, custom headers, ... ' Check the help files for a full list as well ' and the correct syntax. ' Some of the more useful ones I've included samples of here: 'objCDOMail.Cc = "mailto:sschofield@aspfree.com;steve@aspfree.com" Notice this sending to more than one person! 'objCDOMail.Bcc = "sschofield@aspfree.com;steve@aspfree.com" 'objCDOMail.Importance = 1 '(0=Low, 1=Normal, 2=High)im a 'objCDOMail.AttachFile "c:\path\filename.txt", "filename.txt" ' Send the message! objCDOMail.Send 'Set the object to nothing because it immediately becomes 'invalid after calling the Send method + it clears it out of the Server's Memory. Set objCDOMail = Nothing The array code and one of the e-mail pieces of code should be place into an .asp file and then when opened within a web browser the newsletter you created will be on it's way to the specified recipients providing you have changed the BODY TEXT fields to display your newsletters message. I am sorry about the code not being incremented, but when I used MS Word to convert it to HTML they disappeared. I hope this article has been of some use to you. |