We help IT Professionals succeed at work.

Email function for ASP classic

I have an ASP classic application that sends emails, I currently use an old extension for Dreaweaver to generate the code that sends emails. Its like a wizard, you fill out the necessary information and the extension creates the code.
Unfortunately the latest versions of Dreamweaver do not support this extension anymore.

I am looking for some sort of template/code/plugin I can use to replace the old code, something more modern I guess.

The current extension allows me to specify the following:

1.  SMTP server details (CDOSYS)
2. Subject
3. Recipients (to/cc/bcc, this can be a list that comes from a result of a recordset)
4. Attached documents (Also from the results of a recordset, could be more than one attachment)
5. HTML for the body of the email

Anything to replace this extension must include the above at least.

Any suggestions are greatly appreciated.
Comment
Watch Question

Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
There are good examples here https://www.w3schools.com/asp/asp_send_email.asp but I have always used this site http://www.paulsadowski.com/wsh/cdo.htm

Using this example

<%
Set myMail = CreateObject("CDO.Message")
myMail.Subject = "Sending email with CDO"
myMail.From = "mymail@mydomain.com"
myMail.To = "someone@somedomain.com"
myMail.Bcc = "someoneelse@somedomain.com"
myMail.Cc = "someoneelse2@somedomain.com"
myMail.TextBody = "This is a message."
myMail.Send
set myMail = nothing
%>

Open in new window


Replace  the text with variables.

<%
Set myMail = CreateObject("CDO.Message")
myMail.Subject = emailSubject
myMail.From = "mymail@mydomain.com"
myMail.To = sendTo
myMail.TextBody = emailBody
myMail.Send
set myMail = nothing
%>

Open in new window


I prefer to wrap it in a function.  Then place the function in it's own page and include it in any page that I want to email.
<%

function sendMail(sendTo,emailSubject, emailBody)
Set myMail = CreateObject("CDO.Message")
myMail.Subject = emailSubject
myMail.From = "mymail@mydomain.com"
myMail.To = sendTo
myMail.TextBody = emailBody
myMail.Send
set myMail = nothing
end function
%>

Open in new window


This way you are not starting over each time.  When I use larger emails, it also allows me to build up my html.

html="
html=html&"<table>"
html=html&"<tr>"
html=html&"<td>My Content</td>"
html=html&"</tr>"
html=html&"</table>"

emailBody = html

Open in new window

Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
If the base code does not work you may have to use settings such as authenticate. Check the Paul Sadowski site for examples.

Author

Commented:
I have seen the code in w3 schools. I have not seen how to include attachments that come from a recordset (multiple attachments) and also select one or multiple recipients.
Perhaps I would need to build a 'template' which then later I can reuse and simply exchange the values of each variable.

Author

Commented:
I also don't see there how to specify the server, the credentials, the timeout, etc.

Author

Commented:
Tomorrow morning I will post all the items that I need to include, perhaps we can build a function that includes all those parameters, this way I can reuse it and simply pass the values so it sends the email.
Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
One thing a time. There is an option for attachments. I suggest instead of attachments, just include a link that points to the document for people to download.  

For sending from a database, this is where the function makes it easy.

Assume a record set named rsContacts
<%

function sendMail(sendTo,emailSubject, emailBody)
Set myMail = CreateObject("CDO.Message")
myMail.Subject = emailSubject
myMail.From = "mymail@mydomain.com"
myMail.To = sendTo
myMail.TextBody = emailBody
myMail.Send
set myMail = nothing
end function
%>

<%
' fyi, I would used getrows and place the data in an array instead.  This way may be easier to understand.
' also, if you are sending out more than a couple hundred at a time, you will want to throttle.  Do not use he webserver as a timer. Use sql server instead. I have code for that here on EE

' this assumes same subject and body
do until rsContacts.eof
     sendMail rsContacts("emailAddress"),emailSubject, emailBody
rsContacts.movenext
loop

%>

Open in new window



variable subject and body
<%

function sendMail(sendTo,emailSubject, emailBody)
Set myMail = CreateObject("CDO.Message")
myMail.Subject = emailSubject
myMail.From = "mymail@mydomain.com"
myMail.To = sendTo
myMail.TextBody = emailBody
myMail.Send
set myMail = nothing
end function
%>

<%
' fyi, I would used getrows and place the data in an array instead.  This way may be easier to understand.
' also, if you are sending out more than a couple hundred at a time, you will want to throttle.  Do not use he webserver as a timer. Use sql server instead. I have code for that here on EE

' this assumes same subject and body
do until rsContacts.eof

     emailSubject = rsContacts("name")&" Thank you for your purchase"
     emailBody =  rsContacts("name") & "Please verify your address <br>"&rsContacts("addresss")

     sendMail rsContacts("emailAddress"),emailSubject, emailBody
rsContacts.movenext
loop

%>

Open in new window

Author

Commented:
That is looking better. Let me get all the details so I can post all the things that the script would need to include if that is OK. Ill try to post it ASAP.
Developer & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013
Commented:
Here is an old example on how to use a template https://www.experts-exchange.com/questions/28549720/Sending-HTML-Emails.html#a40420042

do until rs.eof
' replace data with recordset
theBody = template(firstName,lastName, variableData1,varialbeData2)

sendMail theFirstName, theLastName, theAddress, theSubject,  theBody

rs.movenext
loop

Sub SendMail(theFirstName, theLastName, theAddress, theSubject,  theBody)

' Set stuff up here
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
'SMTP userinfo
smtpUser="username"
smtpPass="password"

Rcpt = Chr(34) & theFirstName &" "&theLastName & Chr(34) & "<" & TheAddress & ">" 

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = theSubject 
objMessage.From = "first last <address@mydomain.com>" 
objMessage.To = ""&theFirstName&" "&theLastName&" <"&TheAddress&">" 'Rcpt
objMessage.HTMLBody =  theHTML(theSubject,theBody) ' runs sub EmailBody

'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") =  "smtp.mandrillapp.com" 
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = smtpUser
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = smtpPass
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update


'==End remote SMTP server configuration section==


objMessage.Send

End Sub

function template(firstName,lastName, variableData1,varialbeData2)
    mailHTML =""
    mailHTML=mailHTML&"<table>"
    mailHTML=mailHTML&"<tr><td>Hello "&firstName&" "&lastName&"</td></tr>"
    mailHTML=mailHTML&"<tr><td>"&variableData1&"</td></tr>"
    mailHTML=mailHTML&"<tr><td>"&variableData2&"</td></tr>"
    mailHTML=mailHTML&"<table>"
template=mailHTML
end function.

Open in new window

Author

Commented:
Thanks, ill check it out.
Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
My last post shows credentials.  The second link I originally gave you has good examples for more detailed settings  http://www.paulsadowski.com/wsh/cdo.htm

Author

Commented:
Thanks for the help!  For the most part I was able to replace a couple of old emails, will continue to work on it and if I need help Ill repost.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.