begin contents of an example email_templates.asp file
---------------------------------------------
<% response.end ' - For security reasons %>
Any content here, outside of brackets, will be ignored.
If you need to send to multiple recipients (to, cc, or bcc),
format it like createobject("cdo.message") would, e.g.:
{assign var=to value='"$to_name_1" <$to_address_1>, "$to_name_2" <$to_address_2>, "$to_name_3" <$to_address_3>'}
{assign var=bcc value='"$bcc_name_1" <$bcc_address_1>, "$bcc_name_2" <$bcc_address_2>, "$bcc_name_3" <$bcc_address_3>'}
Any unknown template variables will resolve to empty strings.
Example template #1: Test the sub.
[TestSendTemplateEmail]
{assign var=subject value='$subject_line'}
{assign var=from value='"$from_firstname $from_lastname" <$from_email>'}
{assign var=replyto value='"$replyto_firstname $replyto_lastname" <$replyto_email>'}
{assign var=to value='"$to_firstname1 $to_lastname1" <$to_email1>, "$to_firstname2 $to_lastname2" <$to_email2>'}
{assign var=cc value='"$cc_firstname1 $cc_lastname1" <$cc_email1>, "$cc_firstname2 $cc_lastname2" <$cc_email2>'}
{assign var=bcc value='"$bcc_firstname1 $bcc_lastname1" <$bcc_email1>, "$bcc_firstname2 $bcc_lastname2" <$bcc_email2>'}
<html>
<style>
body, p { font-size:8pt; font-family: verdana, sans-serif, arial, helvetica; }
hr { height: 1px; color: black; }
</style>
<body>
<h2>Test</h2>
<hr size="1" noshade>
This is a test email. Please disregard.<br>
<hr size="1" noshade><br>
Timestamp: {$m}/{$dd}/{$yyyy} {$h12}:{$mmin}:{$ss} {$ampm} {$timezone_short}
</body>
</html>
[/TestSendTemplateEmail]
Example template #2: New user account.
[AccountCreated]
{assign var=subject value='Organization: Account Created'}
{assign var=from value='"Organization Name" <sales@organization.com>'}
{assign var=replyto value='"Organization Name" <do-not-reply@organization.com>'}
{assign var=to value='"$to_name" <$to_address>'}
{assign var=cc value='"$cc_name" <$cc_address>'}
{assign var=bcc value='"$bcc_name" <$bcc_address>'}
<html>
<style>
body, p { font-size:8pt; font-family: verdana, sans-serif, arial, helvetica; }
hr { height: 1px; color: black; }
</style>
<body>
<h2>Organization Name: Account Created</h2>
<hr size="1" noshade>
Your account was successfully created.<br>
<br>
Name: <b>{$prefix} {$first_name} {$middle_name} {$last_name}</b><br>
Address: <b>{$streetaddress}<br>
{$city} {$state} {$zip}</b><br>
Country code: <b>{$countrycode}</b><br>
Email: <b>{$to_address}</b><br>
<br>
<a href="http://www.organization.com/Login.asp">Log In</a>
<br>
<hr size="1" noshade><BR>
This email was sent from our website.<br>
Technical problems? Email <a href="mailto:support@organizationname.com">support@organizationname.com</a>.<br>
Timestamp: {$m}/{$dd}/{$yyyy} {$h12}:{$mmin}:{$ss} {$ampm} {$timezone_short}
</body>
</html>
[/AccountCreated]
Example template #3: User forgot their password.
[ForgotPassword]
{assign var=subject value='Organization Name: Your Password'}
{assign var=from value='"Organization Name" <sales@organization.com>'}
{assign var=replyto value='"Organization Name" <do-not-reply@organization.com>'}
{assign var=to value='"$to_name" <$to_address>'}
<html>
<style>
body, p { font-size:8pt; font-family: verdana, sans-serif, arial, helvetica; }
hr { height: 1px; color: black; }
</style>
<body>
<h2>Organization Name: Your Password</h2>
<hr size="1" noshade>
Your name: {$to_name}<br>
Your password: <b>{$password}</b><br>
<br>
If you would like to change your password, login with your current password and click "Change Account Information."<br>
<br>
<a href="http://www.organization.com/Login.asp">Log In to the website</a>
<br>
<hr size="1" noshade><BR>
This email was sent from our website.<br>
Technical problems? Email <a href="mailto:support@organizationname.com">support@organizationname.com</a>.<br>
Timestamp: {$m}/{$dd}/{$yyyy} {$h12}:{$mmin}:{$ss} {$ampm} {$timezone_short}
</body>
</html>
[/ForgotPassword]
Example template #4: User uploaded their resume.
[ResumeReceivedNotice]
{assign var=subject value='Organization Name: Resume Received'}
{assign var=from value='"Organization Name" <support@organization.com>'}
{assign var=replyto value='"$uploader_firstname $uploader_lastname" <$uploader_email>'}
{assign var=to value='"$uploader_firstname $uploader_lastname" <$uploader_email>'}
<html>
<style>
body, p { font-size:8pt; font-family: verdana, sans-serif, arial, helvetica; }
hr { height: 1px; color: black; }
</style>
<body>
{$banner_src}<br>
<h2>Organization Name: Resume Received</h2>
<hr size="1" noshade>
Thank you for your resume submission.<br>
<br>
Filename: <strong>{$filename}</strong><br>
Uploaded: <strong>{$filelatestuploadtimestamp}</strong><br>
<br>
<hr size="1" noshade>
Timestamp: {$m}/{$dd}/{$yyyy} {$h12}:{$mmin}:{$ss} {$ampm} {$timezone_short}
</body>
</html>
[/ResumeReceivedNotice]
---------------------------------------------
end of contents of an example email_templates.asp file
begin send_template_email sub
---------------------------------------------
' - send email using a template
' - this function needs to be pretty solid & bulletproof
' - TODO: make sure incoming variables can contain dollars, single quotes, and braces
sub send_template_email(strTemplateFilename, strTemplateName, dicVariables)
dim objFSO, objFileStream
dim intForReading, intForWriting, intForAppending
dim strFile, strTemplateCode, strLeftSide, strRightSide
dim dicBuiltins, strItem, objEmail, dicSettings, strSettingValue
dim intItem, strKey, aryKeys, aryValues, strValue
dim intMaxIterations, objRegexp, aryVarValue, strVar
dim strEscapedDollarReplacement
dim objAssigns, intAssign, strAssign, objElements
dim intH12, intHH12, intIteration
dim strAMPM_lowercase_nodots, strAMPM_lowercase_dots
dim strAMPM_uppercase_nodots, strAMPM_uppercase_dots
dim strTimezone, strTimezoneShort, strTimezoneLong
intForReading=1
intForWriting=2
intForAppending=8
intMaxIterations=100
set objRegexp=new regexp
' - escaped values must be guaranteed unique, cannot contain the replacement character, and use only characters that are regex safe.
' - i recommend using only numbers and letters
strEscapedDollarReplacement="11111backslashdollar11111"
' - prepare the 12 / 24 hour settings and am / pm settings
if hour(now())=0 then
' - midnight
intH12=12
strAMPM_lowercase_nodots="am"
strAMPM_lowercase_dots="a.m."
elseif hour(now())>12 then
' - afternoon / evening
intH12=hour(now())-12
strAMPM_lowercase_nodots="pm"
strAMPM_lowercase_dots="p.m."
else
' - morning
intH12=hour(now())
strAMPM_lowercase_nodots="am"
strAMPM_lowercase_dots="a.m."
end if
intHH12=right("0" & intH12, 2)
strAMPM_uppercase_nodots=ucase(strAMPM_lowercase_nodots)
strAMPM_uppercase_dots=ucase(strAMPM_lowercase_dots)
' - get the timezone.
strTimezoneLong = createobject("wscript.shell").regread("HKLM\System\CurrentControlSet\Control\TimeZoneInformation\StandardName")
if lcase(strTimezoneLong)="hawaii-aleutian standard time" then strTimezoneShort="HST"
if lcase(strTimezoneLong)="alaskan standard time" then strTimezoneShort="AKST"
if lcase(strTimezoneLong)="pacific standard time" then strTimezoneShort="PST"
if lcase(strTimezoneLong)="mountain standard time" then strTimezoneShort="MST"
if lcase(strTimezoneLong)="central standard time" then strTimezoneShort="CST"
if lcase(strTimezoneLong)="eastern standard time" then strTimezoneShort="EST"
if lcase(strTimezoneLong)="atlantic standard time" then strTimezoneShort="AST"
if lcase(strTimezoneLong)="samoa standard time" then strTimezoneShort="SST"
if lcase(strTimezoneLong)="chamorro standard time" then strTimezoneShort="ChST"
' - prepare built-in variables
set dicBuiltins=server.createobject("scripting.dictionary")
dicBuiltins.add "m", month(now())
dicBuiltins.add "mm", right("0" & month(now()), 2)
dicBuiltins.add "d", day(now())
dicBuiltins.add "dd", right("0" & day(now()), 2)
dicBuiltins.add "yy", right(year(now()), 2)
dicBuiltins.add "yyyy", year(now())
dicBuiltins.add "ampm", strAMPM_lowercase_nodots
dicBuiltins.add "a.m.p.m.", strAMPM_lowercase_dots
dicBuiltins.add "uppercase_ampm", strAMPM_uppercase_nodots
dicBuiltins.add "uppercase_a.m.p.m.", strAMPM_uppercase_dots
dicBuiltins.add "h12", intH12
dicBuiltins.add "hh12", intHH12
dicBuiltins.add "h24", hour(now())
dicBuiltins.add "hh24", right("0" & hour(now()), 2)
dicBuiltins.add "mmin", right("0" & minute(now()), 2)
dicBuiltins.add "s", second(now())
dicBuiltins.add "ss", right("0" & second(now()), 2)
dicBuiltins.add "timezone_short", strTimezoneShort
dicBuiltins.add "timezone_long", strTimezoneLong
dicBuiltins.add "query_string", replace(request.servervariables("query_string"), "$", "\$")
dicBuiltins.add "url", replace(request.servervariables("url"), "$", "\$")
' - read the entire template file, or quit if it's missing
set objFSO = server.createobject("scripting.filesystemobject")
if not objFSO.fileexists(strTemplateFilename) then
response.write "Unable to find file " & strTemplateFilename
exit sub
end if
set objFileStream=objFSO.opentextfile(strTemplateFilename, intForReading)
strFile=objFileStream.readall
objFileStream.close
' - complain and quit if there's a syntax error
if instr(lcase(strFile), "[" & lcase(strTemplateName) & "]")=0 then
response.write "Unable to find [" & strTemplateName & "] in " & strTemplateFilename
exit sub
end if
if instr(lcase(strFile), "[/" & lcase(strTemplateName) & "]")=0 then
response.write "Unable to find [/" & strTemplateName & "] in " & strTemplateFilename
exit sub
end if
if instr(lcase(strFile), "[/" & lcase(strTemplateName) & "]") < instr(lcase(strFile), "[" & lcase(strTemplateName) & "]") then
response.write "Syntax error in " & strTemplateFilename
response.write "[/" & strTemplateName & "] was found before " & "[" & strTemplateName & "]"
exit sub
end if
' - make sure dicVariables is a dictionary object
if lcase(typename(dicVariables))<>"dictionary" then
response.write "Error: The third parameter to the send_template_email function must be a dictionary object"
exit sub
end if
' - extract just the template code we need
strTemplateCode=strFile
strTemplateCode=left(strTemplateCode, instr(lcase(strTemplateCode), "[/" & lcase(strTemplateName) & "]")-1)
strTemplateCode=mid(strTemplateCode, instr(lcase(strTemplateCode), "[" & lcase(strTemplateName) & "]")+len("[" & lcase(strTemplateName) & "]")+1)
' - replace {$var}=nulls with empty strings
for each strItem in dicVariables
intIteration=0
while instr(strTemplateCode, "{$" & strItem & "}")>0 and intIteration < intMaxIterations
intIteration=intIteration+1
if isnull(dicVariables(strItem)) then
strTemplateCode=replace(strTemplateCode, "{$" & strItem & "}", "")
end if
wend
next
' - replace $var=nulls with empty strings
for each strItem in dicVariables
intIteration=0
while instr(strTemplateCode, "$" & strItem)>0 and intIteration < intMaxIterations
intIteration=intIteration+1
if isnull(dicVariables(strItem)) then
strTemplateCode=replace(strTemplateCode, "$" & strItem, "")
end if
wend
next
' - replace variables the user passed to this function, first {$vars} then others
for each strItem in dicVariables
intIteration=0
while instr(strTemplateCode, "{$" & strItem & "}")>0 and intIteration < intMaxIterations
intIteration=intIteration+1
strTemplateCode=replace(strTemplateCode, "{$" & strItem & "}", dicVariables(strItem))
wend
next
' - replace $vars with strings
for each strItem in dicVariables
intIteration=0
while instr(strTemplateCode, "$" & strItem)>0 and intIteration < intMaxIterations
intIteration=intIteration+1
strTemplateCode=replace(strTemplateCode, "$" & strItem, dicVariables(strItem))
wend
next
' - replace assigns
objRegexp.global=true
objRegexp.ignorecase=true
objRegexp.pattern="\{assign var=\w+ value='.+'\}"
set objAssigns=objRegexp.execute(strTemplateCode)
strTemplateCode=objRegexp.replace(strTemplateCode, "")
for intAssign=0 to objAssigns.count-1
strAssign=objAssigns.item(intAssign)
objRegexp.pattern=".+var=(\w+).+"
strVar=objRegexp.replace(strAssign, "$1")
objRegexp.pattern=".+value='(.+)'.+"
strValue=objRegexp.replace(strAssign, "$1")
if dicVariables.exists(strVar) then
dicVariables(strVar)=strValue
else
dicVariables.add strVar, strValue
end if
next
' - add any builtin variables to dicVariables that don't already exist
for each strItem in dicBuiltins
if not(dicVariables.exists(strItem)) then
dicVariables.add strItem, dicBuiltins(strItem)
end if
next
' - "delete" unresolved variables
for each strItem in dicVariables
if left(dicVariables(strItem), 1)="$" then
dicVariables(strItem)=""
end if
next
' - replace variables the user passed to this function, again
for each strItem in dicVariables
while instr(strTemplateCode, "{$" & strItem & "}")>0
strTemplateCode=replace(strTemplateCode, "{$" & strItem & "}", dicVariables(strItem))
wend
next
' - restore escaped characters
objRegexp.pattern=strEscapedDollarReplacement
strTemplateCode=objRegexp.replace(strTemplateCode, "\$")
' - delete unknown variables, e.g., {$whatever}
objRegexp.pattern="\{\$.+\}"
strTemplateCode=objRegexp.replace(strTemplateCode, "")
set objEmail=createobject("cdo.message")
objEmail.subject=dicVariables("subject")
objEmail.from=dicVariables("from")
if dicVariables.exists("replyto") then
if len(dicVariables("replyto"))>0 then
objEmail.replyto=dicVariables("replyto")
end if
end if
objEmail.to=dicVariables("to")
if dicVariables.exists("cc") then
if len(dicVariables("cc"))>0 then
objEmail.cc=dicVariables("cc")
end if
end if
if dicVariables.exists("bcc") then
if len(dicVariables("bcc"))>0 then
objEmail.bcc=dicVariables("bcc")
end if
end if
if request.servervariables("LOCAL_ADDR")<>"iis.server.ip.address" then
' - this script is running on an IIS server
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=1
else
' - this script is not running on an IIS server
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
end if
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="iis.server.name"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "D:\smtp\Pickup"
' objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
' objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Update
objEmail.send
if err.number = 0 then
'OK?
response.write ""
else
'Not OK!
Response.Write "Unable to send email.<p>"
Response.Write "Please contact the administrator with the following error description.<p>"
Response.Write "Error: " & objEmail.Response
err.clear
end if
set objFileStream=nothing
set objFSO=nothing
set objRegexp=nothing
set objEmail=nothing
end sub
---------------------------------------------
end of send_template_email sub
begin your code
---------------------------------------------
option explicit
dim dicEmail, strName, strYourFirstname, strYourLastname, strYourEmail
' - set the location of email_templates.asp
' - "c:" refers to the machine the script is running on (the IIS webserver)
const EMAIL_TEMPLATE_FILE="c:\inetpub\wwwroot\configs\email_templates.asp"
const ORGANIZATION_BANNER="<img src=""http://www.organization.com/images/logo.jpg"">"
set dicEmail=server.createobject("scripting.dictionary")
strYourFirstname="Your First Name"
strYourLastname="Your Last Name"
strYourEmail="your_email@organization.com"
dicEmail.add "subject_line", "Subject Line Here"
dicEmail.add "from_firstname", strYourFirstname
dicEmail.add "from_lastname", strYourLastname
dicEmail.add "from_email", strYourEmail
dicEmail.add "replyto_firstname", strYourFirstname
dicEmail.add "replyto_lastname", strYourLastname
dicEmail.add "replyto_email", strYourEmail
dicEmail.add "to_firstname1", "To Firstname 1"
dicEmail.add "to_lastname1", "To Lastname 1"
dicEmail.add "to_email1", "to_email_1@organization.com"
dicEmail.add "to_firstname2", "To Firstname 2"
dicEmail.add "to_lastname2", "To Lastname 2"
dicEmail.add "to_email2", "to_email_2@organization.com"
dicEmail.add "cc_firstname1", "CC Firstname 1"
dicEmail.add "cc_lastname1", "CC Lastname 1"
dicEmail.add "cc_email1", "cc_email_1@organization.com"
dicEmail.add "cc_firstname2", "CC Firstname 2"
dicEmail.add "cc_lastname2", "CC Lastname 2"
dicEmail.add "cc_email2", "cc_email_2@organization.com"
dicEmail.add "bcc_firstname1", "BCC Firstname 1"
dicEmail.add "bcc_lastname1", "BCC Lastname 1"
dicEmail.add "bcc_email1", "bcc_email_1@organization.com"
dicEmail.add "bcc_firstname2", "BCC Firstname 2"
dicEmail.add "bcc_lastname2", "BCC Lastname 2"
dicEmail.add "bcc_email2", "bcc_email_2@organization.com"
call send_template_email(EMAIL_TEMPLATE_FILE, "TestSendTemplateEmail", dicEmail)
set dicEmail=nothing
strName="Mr. Password Forgetter"
set dicEmail=server.createobject("scripting.dictionary")
dicEmail.add "to_name", strName
dicEmail.add "to_address", "password_forgetter@yahoo.com"
dicEmail.add "password", "secret password! shh!"
call send_template_email(EMAIL_TEMPLATE_FILE, "ForgotPassword", dicEmail)
set dicEmail=nothing
set dicEmail=server.createobject("scripting.dictionary")
dicEmail.add "banner_src", ORGANIZATION_BANNER
dicEmail.add "uploader_firstname", "Joe"
dicEmail.add "uploader_lastname", "Cool"
dicEmail.add "uploader_email", "snoopy@peanuts.com"
dicEmail.add "filename", "joe-cool-resume.doc"
dicEmail.add "filelatestuploadtimestamp", now()
call send_template_email(EMAIL_TEMPLATE_FILE, "ResumeReceivedNotice", dicEmail)
set dicEmail=nothing
---------------------------------------------
end of your code