Send UTF-8 email through ASP with CDONT

5

I'm trying to send a form to an email using CDONT, but the charset is arriving incorrect and is not displaying the accented characters correctly.

<%
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"
mail_to = "[email protected]"

Dim f, emsg, mail_to, r, o, c, other
emsg =  "<!DOCTYPE html><html lang=""pt-br""><head><meta charset=""UTF-8"" /></head><body>" & _
        "<strong>Solicitante: " & Request("txtSol") & vbCrLF & _
        "Data da solicitação:</strong> " & Request("txDatatSol") & vbCrLF  & _
        "Setor: " & Request("cboSetor") & vbCrLF & _
        "Descrição do produto/serviço: "  & vbCrLF & Request("txtProdServ") & vbCrLF & _
        "Justificativa da solicitação: "  & vbCrLF & Request("txtJust") & vbCrLF & _
        "Valor médio: " & Request("txValor") & vbCrLF & _
        "Prazo limite: " & Request("txDatatLim") & vbCrLF & _
        "Especificações técnicas do produto/serviço e requisitos da qualidade exigidos: "  & vbCrLF & Request("txtEspcTec") & vbCrLF & _
        "Produto Crítico? " & Request("rdoCritico") & vbCrLF & _
        "Possíveis fornecedores: " & vbCrLF & Request("txtFornc")  & vbCrLF & _
        "Fornecedor exclusivo? " & Request("rdoExclusivo") & vbCrLF & _
        "Você confirma o envio do parecer técnico? " & Request("rdoParecer") & _
        "</body> </html>"

For Each f In Request.Form
    If mid(f,1,1)<>"S"  = True Then
        emsg = emsg & f & " = " & Trim(Request.Form(f))
    End If
Next

Set objNewMail = Server.CreateObject("CDONTS.NewMail")
    objNewMail.From = Request("txtSol")
    objNewMail.Subject = "Solicitação de compra"
    objNewMail.To = mail_to
    objNewMail.BodyFormat=0
    objNewMail.MailFormat=0
    objNewMail.Body = emsg 
    objNewMail.Send
    Set objNewMail = Nothing

response.redirect "home.asp?status=enviado"

%>
    
asked by anonymous 10.04.2015 / 15:01

2 answers

3

To work with UTF-8 is not enough Response.CodePage and Response.Charset , both documents need to be saved at UTF-8 without BOM , both the upload file and the file containing the form.

Two softwares that can be used to save the document with the necessary encoding are notepad ++ or SublimeText:

Using notepad ++:

UsingSublimeText:

If you are sure that both files are saved this way, then you need to apply Response.CodePage and Response.Charset to both documents.

If you have already done all these procedures and the sending of the email continues with problems in the characters, then it will be necessary to use one of these properties, as the colleague mentioned in the other response:

objNewMail.BodyPart.Charset = "utf-8"

or

objNewMail.HTMLBodyPart.Charset = "utf-8"

One detail, I do not have much knowledge of classic Asp, but I believe mail_to = "[email protected]" should go after Dim :

Dim f, emsg, mail_to, r, o, c, other

mail_to = "[email protected]"
emsg =  "<!DOCTYPE html>...
    
19.04.2015 / 16:06
0

In order to send emails with the correct UTF-8 encoding, use:

bjCDOMailer.TextBodyPart.Charset = "utf-8"

For the encoding to work correctly:

objCDOMailer.BodyPart.Charset = "utf-8" 
objCDOMailer.HTMLBodyPart.Charset = "utf-8"
    
14.04.2015 / 14:10