Skip to content

Instantly share code, notes, and snippets.

@lankymart
Last active March 29, 2021 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lankymart/efbdcd3e2e1adb92b57c6434b6bf176c to your computer and use it in GitHub Desktop.
Save lankymart/efbdcd3e2e1adb92b57c6434b6bf176c to your computer and use it in GitHub Desktop.
Sending Email encoding using UTF-8 using Classic ASP

Sending Email encoding using UTF-8 using Classic ASP

This Gist is in reply to this question Passing language specific characters from asp to CDO to HTML template not working [duplicate].

It is indeed possible to send Email using Classic ASP that is encoded correctly using UTF-8.

Note: All files are saved as UTF-8

  • form.htm - HTML form used to post to the ASP script.
  • template.htm - HTML template that is loaded using ADODB.Stream.
  • test.asp - The ASP page used to process the form / inject the template and send the email using CDO.Message.
  • result.eml - The resulting EML file downloaded from the local Smtp4dev server used to simulate the email send.
<html>
<head>
<title>Test 33</title>
</head>
<body>
<form action="test.asp" method="post">
<input type="hidden" name="template" value="template.htm" />
<textarea name="message" rows="10" cols="100"></textarea>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Thread-Topic: Test Email
thread-index: AdckzyUf6ZF/uRsTSqG8szy1Ii2tbw==
From: <theserver@example.com>
To: <someone@example.com>
Subject: Test Email
Date: Mon, 29 Mar 2021 20:10:13 +0100
Message-ID: <9DB5C085BE5C40D784838A04215C21B9@FIMDLT1337>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0000_01D724D7.86E45B00"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE
This is a multi-part message in MIME format.
------=_NextPart_000_0000_01D724D7.86E45B00
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: base64
w6Ysw7gsw6UNCg0K
------=_NextPart_000_0000_01D724D7.86E45B00
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: 8bit
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Email</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
a[x-apple-data-detectors] {
color: inherit !important;
}
</style>
</head>
<body style="margin: 0; padding: 0;">
<p>æ,ø,å</p>
<p></p>
</body>
</html>
------=_NextPart_000_0000_01D724D7.86E45B00--
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Email</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
a[x-apple-data-detectors] {
color: inherit !important;
}
</style>
</head>
<body style="margin: 0; padding: 0;">
<p>æ,ø,å</p>
<p>[$message$]</p>
</body>
</html>
<%@Language="VBScript" Codepage = 65001%>
<%
Option Explicit
Response.CodePage = 65001
Response.Charset = "UTF-8"
Dim pde : Set pde = Server.CreateObject("scripting.dictionary")
Function getTextFromFile(path)
Dim adoStream, txt
Set adoStream = Server.CreateObject("ADODB.Stream")
Call adoStream.Open()
adoStream.Charset = "UTF-8"
Call adoStream.LoadFromFile(path)
txt = adoStream.ReadText(-1)
Call adoStream.Close()
Set adoStream = Nothing
getTextFromFile = txt
End Function
Dim redir, mailto, mailfrom, subject, item, body, cc, bcc, message, html, template, usetemplate, testmode
redir = Request.Form("redirect")
mailto = Request.Form("mailto")
If pde.exists(mailto) Then mailto = pde(mailto)
subject = Request.Form("subject")
message = Request.Form("message")
template = Request.Form("template")
If Len(template) > 0 Then template = getTextFromFile(Server.MapPath(template))
usetemplate = (Len(template) > 0)
Dim msg : Set msg = Server.CreateObject("CDO.Message")
Dim smtpServer, yourEmail, yourPassword
msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Call msg.Configuration.Fields.Update()
msg.Subject = "Test Email"
msg.To = "someone@example.com"
msg.From = "theserver@example.com"
If Len(cc) > 0 Then msg.cc = cc
If Len(bcc) > 0 Then msg.bcc = bcc
If Not usetemplate Then
body = body & message & vbCrLf & vbCrLf
Else
body = template
End If
For Each item In Request.Form
Select Case item
Case "redirect", "mailto", "cc", "bcc", "subject", "message", "template", "html", "testmode"
Case Else
If Not usetemplate Then
If item <> "mailfrom" Then body = body & item & ": " & Request.Form(item) & vbCrLf & vbCrLf
Else
body = replace(body, "[$" & item & "$]", Replace(Request.Form(item), vbCrLf, "<br>"))
End If
End Select
Next
If usetemplate Then
Dim rx : Set rx = New RegExp
rx.Pattern = "\[\$.*\$\]"
rx.Global = True
body = rx.Replace(body, "")
End If
msg.BodyPart.Charset = "utf-8"
msg.htmlbody = body
Call msg.Send()
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment