繁体   English   中英

将语言特定字符从 asp 传递到 CDO 到 HTML 模板不起作用

[英]Passing language specific characters from asp to CDO to HTML template not working

注意! 对于那些将这篇文章与以下内容相关联的人: https://stackoverflow.com/questions/31524404/classic-asp-and-utf-8它不能解决问题,并且其中的所有内容都已经过测试。

我有一个 ASP Classic 表单,它使用 HTML 模板通过 CDO 发送电子邮件。将特殊语言字符(在本例中为丹麦字符æ,ø,å )从表单传递到包含 CDO 代码的 ASP 页面是工作正常并在该页面上进行Response.Write时正确显示字符。 但是,当电子邮件发送时,传递的字符是“混乱的”。 但是,如果我将字符直接写入 HTML 模板,它们会在电子邮件中正确显示。 所有三个文档的字符集都设置为 UTF-8 并且 ASP 页面设置了 CODEPAGE 65001。 谁能看到导致这种行为的原因?

这是电子邮件中发生的情况:

这就是电子邮件中发生的情况

电子邮件模板(仅定义):

<!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;">

 

    
</body>

</html>

发送邮件.asp:

<%@Codepage = 65001%>
<%
Option explicit
Response.CodePage = 65001
Response.CharSet = "UTF-8"


    Dim SettingsConn
    Set SettingsConn = Server.CreateObject("ADODB.Connection")
    SettingsConn.ConnectionString="Provider=SQLOLEDB; DATA SOURCE=<SERVER>;UID=<USERNAME>;PWD=<PASSWORD>;DATABASE=<DB>"
    SettingsConn.Open

    Dim SettingsSQL, objSettings

    SettingsSQL = "SELECT SMTPServer,SMTPPort,MailFromName,MailFromEmail,MailCC,MailBCC,EFPVersion FROM EFP_Settings WHERE ID = 1;"

    Set objSettings = SettingsConn.Execute(SettingsSQL)


    dim pde : set pde = createobject("scripting.dictionary")

    function getTextFromFile(path)
        dim fso, f, txt
        set fso = createobject("Scripting.FileSystemObject")
        if not fso.fileexists(path) then
            getTextFromFile = ""
            exit function
        end if
        set f = fso.opentextfile(path,1)
        if f.atendofstream then txt = "" else txt = f.readall
        f.close
        set f = nothing
        set fso = 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)
    cc = objSettings("MailCC")
    bcc = objSettings("MailBCC")
    subject = request.form("subject")
    message = request.form("message")
    template = request.form("template")

    if len(template) > 0 then template = getTextFromFile(server.mappath(template))
    if len(template) > 0 then usetemplate = true else usetemplate = false
    dim msg : set msg = server.createobject("CDO.Message")
    dim smtpServer, yourEmail, yourPassword
    msg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    msg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "" & objSettings("SMTPServer") & ""
    msg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = objSettings("SMTPPort")
    msg.Configuration.Fields.Update
    msg.subject = subject
    msg.to = mailto
    msg.from = """" & objSettings("MailFromName") & """ <" & objSettings("MailFromEmail") & ">"
    msg.Sender = """" & objSettings("MailFromName") & """ <" & objSettings("MailFromEmail") & ">"

    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.htmlbody = body

        msg.send

        response.redirect redir

        set msg = nothing
    %>

更新:

根据以下评论;

我试图将 FSO 转换为 ADODB,所以它看起来如下所示,但是是一个错误:

代码:

function getTextFromFile(path)
    Dim adoStream, txt
    Set adoStream = CreateObject("Adodb.Stream")
    if not adoStream.FileSystemObject(path) then
        getTextFromFile = ""
        exit function
    end if
    adoStream.Open
    adoStream.Charset = "UTF-8"
    txt = adoStream.ReadText(-1)
    adoStream.LoadFromFile txt
    adoStream.Close
    Set adoStream = Nothing
    getTextFromFile = txt
end function

最新更新 (28-03-2021) - 11:36

最新的 sendmail.asp:

<%@Codepage = 65001%>
  <%
    Option explicit
    Response.CodePage = 65001
    Response.CharSet = "UTF-8"

    Dim SettingsConn
    Set SettingsConn = Server.CreateObject("ADODB.Connection")
    SettingsConn.ConnectionString="Provider=SQLOLEDB; DATA SOURCE=<SERVER>;UID=<USERNAME>;PWD=<PASSWORD>;DATABASE=<DATABASE>"
    SettingsConn.Open

    Dim SettingsSQL, objSettings

    SettingsSQL = "SELECT SMTPServer,SMTPPort,MailFromName,MailFromEmail,MailCC,MailBCC,EFPVersion FROM EFP_Settings WHERE ID = 1;"

    Set objSettings = SettingsConn.Execute(SettingsSQL)

    dim redir, mailto, mailfrom, subject, item, body, cc, bcc, html, template, usetemplate, testmode
    redir = request.form("redirect")
    mailto = request.form("mailto")
    cc = objSettings("MailCC")
    bcc = objSettings("MailBCC")
    subject = request.form("subject")
    template = request.form("template")

    Response.Write request.form("template")
    ' Output of request.form("template") is templates/emailtemplate_report_issue.htm
    
    Dim adoStream, getTextFromFile
    Set adoStream = CreateObject("Adodb.Stream")
    adoStream.Type = 2
    adoStream.Open
    adoStream.Charset = "UTF-8"
    adoStream.LoadFromFile server.mappath("" & template & "")
    template = adoStream.ReadText(-1)
    adoStream.Close
    Set adoStream = Nothing
    getTextFromFile = template
    

    dim msg : set msg = server.createobject("CDO.Message")
    dim smtpServer, yourEmail, yourPassword
    msg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    msg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "" & objSettings("SMTPServer") & ""
    msg.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = objSettings("SMTPPort")
    msg.Configuration.Fields.Update
    msg.subject = subject
    msg.to = mailto
    msg.from = """" & objSettings("MailFromName") & """ <" & objSettings("MailFromEmail") & ">"
    msg.Sender = """" & objSettings("MailFromName") & """ <" & objSettings("MailFromEmail") & ">"

    if len(cc) > 0 then msg.cc = cc
    if len(bcc) > 0 then msg.bcc = bcc

    body = template
    for each item in request.form
            body = replace(body, "[$" & item & "$]", replace(request.form(item),vbcrlf,"<br>"))
    next

    dim rx : set rx = new regexp
    rx.pattern = "\[\$.*\$\]"
    rx.global = true
    body = rx.replace(body, "")

    msg.htmlbody = body

    msg.send

    response.redirect redir

    set msg = nothing

    %>

电子邮件中的 Output 字符未按预期显示(从表单传递的变量和硬编码的变量)

在此处输入图像描述

这是一个编码问题,但与经典 ASP 的设置方式无关。 在评论中进行了一些扩展讨论之后,很明显问题在于CDO.Message是如何构建的。

虽然 Classic ASP 脚本中的数据被处理为 UTF-8 消息从未被告知它应该是,这可以用这一行纠正;

msg.BodyPart.Charset = "utf-8"

下面是您的代码的一个工作示例(为了可测试性,删除了一些 SQL 配置元素)。 它使用Smtp4Dev模拟 email 的发送,这是一个伪造的 SMTP Email 服务器进行开发和测试(还包括Gist )。

HTML 表单 POST(编码为 UTF-8)

<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>

HTML 模板(编码为 UTF-8)

<!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>

经典 ASP 脚本(编码为 UTF-8)

<%@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()
%>

结果 Email

Smtp4Dev作为 EML 文件下载。

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--


有用的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM