空间和马车退货正在添加到HTML电子邮件中

发布于 2025-02-05 19:51:38 字数 2612 浏览 1 评论 0 原文

我们有一个服务器生成的HTML文件( myfile.html ),该文件嵌入了发送给客户的电子邮件中。多年来,我们一直在使用这种方法,但问题很小。我们通过II6将Windows Server 2012与SMTP服务器一起使用。最近,HTML在电子邮件中倾斜。在检查源文件时,一切看起来都很好。直接打开HTML文件以在浏览器中查看,如您所期望的。这是我们用来将文件读取到内存中以准备电子邮件的代码:

        Set objFile = objFSO.OpenTextFile(strFilePath)
        Do While objFile.AtEndOFStream <>True
            line = objFile.ReadLine
            If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
                strHeaderWritten = "Y"
                strFileContent=strFileContent & strHeader
            End If
            strFileContent=strFileContent & line
        Loop
        set objFile = Nothing

然后我们将内容添加到电子邮件中并发送:

    strBody = strFileContent
    Set objMail = CreateObject("CDO.Message")

    Set objMail.Configuration = cdoConfig
    objMail.From = strFrom
    objMail.ReplyTo = strReplyTo
        
    objMail.To = strTo
    
    objMail.Subject = strSubject
    objMail.HTMLBody = strBody

    objMail.Fields("urn:schemas:httpmail:importance").Value = strImportance
    objMail.Send

以下是电子邮件中它列出的示例。来源没有错误:

其他人是否有这种情况发生在他们身上?

在这个数小时一直在寻找解释。非常感谢您的阅读!

我尝试使用ADO流方法进行电子邮件,但它仍然相同:

        Dim objStream
        Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Type = 2 'adTypeText
        objStream.CharSet = Application("CharacterSet")
        objStream.Open
        objStream.LoadFromFile strFilePath

        Do While Not objStream.EOS
            line = objStream.ReadText(-2)
            If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
                strHeaderWritten = "Y"
                strFileContent=strFileContent & strHeader
            End If
            If Instr(1, line, "< table") > 0 Then
                strFileContent=strFileContent & "<h3>Broken HTML</h3>"
            End If
            strFileContent=strFileContent & line
        Loop
        objStream.Close
        Set objStream = Nothing

如您所见,我还为我看到的一个持续错误之一添加了检查,我看到了 space &lt; 之间插入。以这种方式检查输出不会像检查添加空间的文本一样捕获问题。因此,它必须在编写后发生,否则我需要使用正则表达式进行测试。接下来我会尝试的。我仍然在多个电子邮件客户端看到它。这是ADO流的示例后测试:

”在此处输入图像描述”

We have a server generated HTML file (myFile.html) that we embed in emails that get sent to our clients. We've been using this method for years with minimal issues. We use Windows Server 2012 with smtp server via II6. Recently the HTML is getting skewed in the email. When checking the source file, all looks well. Directly opening the HTML file for viewing in a browser works as you'd expect. Here is the code we're using to read the file into memory to prepare for emailing:

        Set objFile = objFSO.OpenTextFile(strFilePath)
        Do While objFile.AtEndOFStream <>True
            line = objFile.ReadLine
            If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
                strHeaderWritten = "Y"
                strFileContent=strFileContent & strHeader
            End If
            strFileContent=strFileContent & line
        Loop
        set objFile = Nothing

And then we add the content to the email and send:

    strBody = strFileContent
    Set objMail = CreateObject("CDO.Message")

    Set objMail.Configuration = cdoConfig
    objMail.From = strFrom
    objMail.ReplyTo = strReplyTo
        
    objMail.To = strTo
    
    objMail.Subject = strSubject
    objMail.HTMLBody = strBody

    objMail.Fields("urn:schemas:httpmail:importance").Value = strImportance
    objMail.Send

And here are examples of what it spits out in the email. There are no errors in the source:
enter image description here

Has anyone else had this happen to them?

Been toiling over this for hours looking for an explanation. Thank so much for reading!

I tried using the ADO Stream method for the email, but it is still coming out the same:

        Dim objStream
        Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Type = 2 'adTypeText
        objStream.CharSet = Application("CharacterSet")
        objStream.Open
        objStream.LoadFromFile strFilePath

        Do While Not objStream.EOS
            line = objStream.ReadText(-2)
            If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
                strHeaderWritten = "Y"
                strFileContent=strFileContent & strHeader
            End If
            If Instr(1, line, "< table") > 0 Then
                strFileContent=strFileContent & "<h3>Broken HTML</h3>"
            End If
            strFileContent=strFileContent & line
        Loop
        objStream.Close
        Set objStream = Nothing

As you can see, I also added a check for one of the persistent errors I'm seeing where there has been a space inserted between < and table. Checking the output this way did not capture the issue as in checking the text for the added space. So it must be happening after it's been written or I need to use a regex for the test. I'll try that next. I'm still seeing it in multiple email clients. Here's an example post test of ADO Stream:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2025-02-12 19:51:38

这似乎是CDO中的一个常见问题。我在网上发现了一些问题,即空间被随机插入到HTML体中。

一个答案是使HTML主体不是一个长的字符串,因为CDO然后将插入随机空间,而是自己包含空格,以便CDO不必这样做。
您可以尝试添加 vbcrlf 或仅在您发送的文本中的普通空间。

第二个建议对我来说更有意义。这可能是编码问题。这也解释了为什么添加自己的空格可能是解决方法。
反正; CDO允许在发送之前设置CDO.Message对象的编码。
尝试 objmail.bodypart.contenttransferencoding =“引用打印机” 查看是否解决了。

This seems to be a common problem in CDO. I've found a few references online to the problem that spaces are randomly inserted into the HTMLbody.

One answer was to make the HTML body not one long string, because CDO will then insert random spaces, but to include whitespace yourself, so that CDO doesn't have to.
You could try adding VbCrLf or just plain spaces in the text you're sending.

A second suggestion made more sense to me; this can be an encoding problem. That also explains why adding your own whitespace could be a workaround.
Anyway; CDO allows for setting the encoding of the CDO.Message object before sending.
Try objMail.BodyPart.ContentTransferEncoding = "quoted-printable" to see if that solves it.

划一舟意中人 2025-02-12 19:51:38

问题是Windows使用线路断路和carrage返回。我建议将文本的正文加载并用VBLF替换所有实例,您会发现您不会再有双间隔了。

例如

body = replace(body, vbcrlf, vblf)

The issue is windows use of both line break and carrage return. I recommend loading the body of the text and replacing all instances of vbcrlf with just vblf and you will find you wont have the double spacing anymore.

e.g.

body = replace(body, vbcrlf, vblf)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文