vbscriptasp-classiciis-6windows-server-2012

Spaces and carriage returns are being added to HTML email


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


Solution

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