Forum Moderators: open
I have a mailing list script which we send out to our subscribers every quarter. (its not spam. We are very strict on how we get our users to request more information.....but that is another conversation).
As it stands just now, i send these letters out at around 300-400 every 5 minutes. Then send the next 300-400 and so on.
I don't like the idea of instantly clogging the server with several thousand outgoing emails. My ideal situation would be to be able to run this script so that it sent 1 email every 2-3 seconds, allowing the server time to send and not clogg up.
Is this possible with ASP and SQL? Below is the script i currently use:
=========================================
Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.RecordSet")
cn.ConnectionString = connstr
cn.Open
rs.ActiveConnection = cn
rs.Open "SELECT * from my_table where id BETWEEN 100 and 500 and emails_active = 1 ORDER by id "
%>
<%
While Not rs.EOF
Set mail = Server.CreateObject("CDONTS.NewMail")
mail.From = "something@ourwebadress.com"
mail.To = rs("email")
mail.Subject = "News from our team"
mail.Body = message & rs("email")
mail.Send
Set mail = Nothing%>
<%
'This part shows a count on screen of all the email that have been sent.
no = no + 1%>
<%Response.write(no)%>
<%Response.write " - "%>
<%=rs("id")%>
<%response.write "<br>"%>
<%Response.Flush%>
<%
rs.MoveNext
wend%>
======================================
Basically just now, all i do is send the above. Then update the SQL line to pull the next 300-400 or so, re-upload the page and send again.
It does work, but as you can imagine, its a long process.....especially if there are several 1000 users registered.
Any ideas on howi can make this more efficient......and automatic i.e. hit run once and they all send at around 1 email every few seconds?
Regards
Webboy
Why not use the BCC feature and just send 1 email.
If this isnt possible - I would send them all at once in the script (there only actually going as far as Exchange) , and then look at scheduling/managing the bulk sending from Exchange.
2. (I will get to your question eventually =) It is also not necessary to use both a connection object and a recordset in script - use recordset.open only, passing the connection string as the second parameter (I assume you're happy with a read-only, forward only cursor - more about that in point 4). The reason for this is ADO will create the connection object for you, and it will do it more efficiently than the VBScript - I won't bore you with the details.
3. My understanding is there is no "Sleep" method in VBScript (there isn't one in VB6). I assume this is because the designers never considered that a developer would WANT to slow down an executing web script. If you have determined that the other suggestions in this thread are not suitable for your circumstances, you can build a trivial COM component that will expose the Win32 Sleep API call to your script. You can do this in VC++ directly. If you have VB you need to supply the unit with a "declare" statement giving it access to the API call. This is not hard and I can give you the source to do it if you're interested. Then what you do is create your "sleeper" object outside the loop, and after each mail.Send, call it with a parameter of 1 or 2 or 3 seconds, whatever. You'll have to set your Script.Timeout to a very large number (number of emails to send * number of seconds to wait between them * some padding for luck) or the script will crash.
4. Finally, (assuming you're going with point 3) you need to consider the impact on your database. A read-only/forward-only cursor is the fastest way to access data, but the cursor is _server-side_, meaning you'll be hogging a database connection for the entire time your script runs. This could get ugly, especially if there is lots of other database activity on your site. I would suggest you use a "static" cursor, which frees up the database connection once the recordset has been returned to the client. You need to set your connection parameters to adUseClient and adOpenStatic, and don't specify a lock type.
Please note that if, as aspdaddy assumes, you are connected to an Exchange server over which you, or one of your colleagues has administrative rights (i.e. not using the local SMTP server with the "Smart Host" pointing at a mail server out of your control), this stuff is not necessary.