Skip to content

Instantly share code, notes, and snippets.

@evagoras
Created September 28, 2023 14:22
Show Gist options
  • Save evagoras/92ab73d4eb17c8c666c562c6c83e08de to your computer and use it in GitHub Desktop.
Save evagoras/92ab73d4eb17c8c666c562c6c83e08de to your computer and use it in GitHub Desktop.
Formatting complex ASP output using HTML templates
...
Set objEmail = Server.CreateObject("CDONTS.NewMail")
objEmail.BodyFormat = 0 'CdoBodyFormatHTML objEmail.MailFormat = 0 'CdoMailFormatMime
objEmail.From = strEmail
objEmail.To = "someone@somewhere.com"
objEmail.Subject = strSubject
objEmail.Body = ReadFile("template.htm")
objEmail.Send
Set objEmail = Nothing
Function ReadFile(filename)
Dim objFSO
Dim objTStream
Dim strText
Const ForReading = 1
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objTStream = objFSO.OpenTextFile(Server.MapPath(filename), ForReading)
strText = objTStream.ReadAll
Set objTStream = Nothing
Set objFSO = Nothing
strText = Replace(strText, "REPLACE_Name", strName)
strText = Replace(strText, "REPLACE_Email", strEmail)
strText = Replace(strText, "REPLACE_Phone", strPhone)
strText = Replace(strText, "REPLACE_Fax", strFax)
'-- Notice that below I am replacing all line breaks with the HTML <br>.
' This applies to all form textboxes, where the user can enter line breaks
' and it achieves proper display of the contents in HTML.
strText = Replace(strText, "REPLACE_Address", Replace(strAddress, vbcrlf, "<br>"))
strText = Replace(strText, "REPLACE_Profession", strProfession)
strText = Replace(strText, "REPLACE_Subject", strSubject)
strText = Replace(strText, "REPLACE_Message", Replace(strMessage, vbcrlf, "<br>"))
ReadFile = strText
End Function
<html>
<head>
<title>Untitled</title>
</head>
<body>
<em><!--template start; erase all above--></em>
...
<em><!--template end; erase all below--></em>
</body>
</html>
Dim strName
Dim strEmail
Dim strSubject
Dim strMessage
Dim strAddress
Dim strPhone
Dim strFax
Dim strProfession
Dim objEmail
strName = Request.Form("Name")
strEmail = Request.Form("Email")
strSubject = Request.Form("Subject")
strMessage = Request.Form("Message")
strAddress = Request.Form("Address")
strPhone = Request.Form("Phone")
strFax = Request.Form("Fax")
strProfession = Request.Form("Profession")
Set objEmail = Server.CreateObject("CDONTS.NewMail")
objEmail.From = strEmail
objEmail.To = "someone@somewhere.com"
objEmail.Subject = strSubject
objEmail.Body = "Email sent at " & Now() & vbcrlf _
& "by " & strName & vbcrlf _
& "---------------------------------------" & vbcrlf & vbcrlf _
& "Address: " & strAddress & vbcrlf _
& "Phone: " & strPhone & vbcrlf _
& "Fax: " & strFax & vbcrlf _
& "Profession: " & strProfession & vbcrlf _
& "Message: " & strMessage & vbcrlf
objEmail.Send
Set objEmail = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment