Re: email rendering in unicode

From: Philippe VERDY (
Date: Sat May 21 2005 - 17:34:14 CDT

  • Next message: Dean Snyder: "Transliterating ancient scripts [was: ASCII and Unicode lifespan]"

    > De : "faraz siddiqi" <>
    > (...)and send email to urself, type randomly anything i know u dont know anything about my language urdu but write anything and check if it deliver to ur mailbox as it is or in the form o grbage, the problem is my editor render email as garbage try to email anything to urself from my editor pls let me know what i should do , my mind is really stuck in this situation since last month </P>
    > (...)
    > <% Response.Buffer = True
    > Dim strBody
    > Dim Mail
    > Dim strtoEmailAddress
    > Dim strsub
    > dim filepath
    > strBody = request.Form("T5")
    > Set Mail = Server.CreateObject("Persits.MailSender")
    > (...)

    Your problem is just at the line above: your "Mail" object is an instance of the (unspecified) "Persists.MailSender" class.
    You use this object to set the various mail fields:

    > Mail.Host = ""
    > Mail.Port = 25

    But the following line has no action on the Mail object:

    > Session.CodePage = 65001

    So by default, your "Mail" object is still not configured to specify the encoding of the attached body, and not even the encoding used for the address and subject fields:

    > Mail.From = Request.Form("T1")
    > Mail.AddAddress Request("T3")
    > Mail.Subject = Mail.EncodeHeader( Request("T4"), "utf-8")

    Above you have just put in the subject line your UTF-8-encoded subject, using UTF-8 (Mail.EncodeHeader will probably use quoted printable to render your Request("T4") string into ASCII form, compatible with headers like Subject.

    The following just indicates that you send HTML, so a MIME extension will be created, and a text-only version will be created on the fly, from the (otherwise treated like binary) HTML document you give below:

    > Mail.IsHTML = True
    > Mail.Body = "<HTML><head><title></title></head><BODY BGCOLOR=#0000FF>"&strBody&"</BODY></HTML>"

    This comes too late, and should be done before you set the Body, Subject, and even "From" and "To" addresses (which may also contain international characters, that you have also forgotten to encode for headers:

    > Mail.CharSet = "UTF-8"
    > Mail.ContentTransferEncoding = "Quoted-Printable"

    Now there is still nothing that indicates the content-type of your message body! You have just indicated a Charset but it probably serves nothing because this CharSet setting will probably only be used only when you call Mail.EncodeHeader() without specifying the charset, or to specify what will be the binary encoding used in the body.

    So place the two lines above a bit higher in your program. And add an instruction to your Mail object to set the missing Content-Type header. I don't know what is the "Persits.MailSender" class exactly on your system, but its instances should have a method like:

    Mail.AddHeader("Content-Type", "text/html; charset=UTF-8");

    Note also that your mail content is a HTML document. You should be able to specify the charset (UTF-8) used to decode the HTML document within the HTML headers:

    So My opinion is that your code shuold be like this:
    (note that I use a htmlEncode() function to encode the text so that it will not break the HTML. You may adjust this or use another function to format the attached text.

    <% Response.Buffer = True
    'Add code here to check that this post is authenticated and authorized!
    Dim strFrom = Request.Form("T1")
    Dim strTo = Request("T3")
    Dim strSubject = Request("T4")
    Dim strBody = request.Form("T5")
    Dim Mail
    Set Mail = Server.CreateObject("Persits.MailSender")
    ' Indicate the encoding methods to use for headers
    Mail.CharSet = "UTF-8"
    Mail.ContentTransferEncoding = "Quoted-Printable"
    ' Prepare the various headers with these settings
    Mail.From = Mail.EncodeHeader( strFrom, "UTF-8" )
    Mail.AddAddress Mail.EncodeHeader( strTo, "UTF-8" )
    Mail.Subject = Mail.EncodeHeader( strSubject, "UTF-8" )
    ' Make sure that the "Content-Type:" header will be present:
    Mail.AddHeader("Content-Type", "txt/html; charset=UTF-8")
    ' Use a MIME enveloppe to transport the content body:
    Mail.IsHTML = True
    ' Now that all headers are ready, set the content body
    Mail.Body = "<HTML><head>" &_
      "<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">" &_
      "<title>" & htmlEncode(strSubject) & "</title></head>" &_
      "<BODY BGCOLOR=#0000FF>" & htmlEncode(strBody) & "</BODY></HTML>"
    ' Now send the complete email via my SMTP server:
    Mail.Host = ""
    Mail.Port = 25

    Then try to send it to your test email address, and ensure that the "Content-Type:" header is effectively present in the Email headers (and as well in the <meta> tag of the HTML body).

    Finally, don't let this form on a website like this, or it will be very easily abused to act as a open relay for spam:
    - the "From" header must not be filled freely by users
    - the form processing page must not be accessible directly without verifying that it comes with a valid session cookie (else it will be easy to POST data to your webmail post processor, with the necessary fields to send any content from anyone to anyone. The email effective source will be your own SMTP server, that will be considered as spamming, so your SMTP server will be listed in blacklists!

    This archive was generated by hypermail 2.1.5 : Sat May 21 2005 - 18:15:02 CDT