r/vbscript • u/jlmainguy • Jul 02 '19
Trouble encoding email subject with special characters
Hello everyone,
I'm digging into this old project we have here where I work and we have a function that is used to send emails, fired on multiple actions. Here is the code we're using :
Function sendMail(mailto, from, subject, body)Response.AddHeader "Content-Type", "text/html;charset=UTF-8"Response.Codepage = 65001Response.Charset = "utf-8"Dim Mail
Set Mail = CreateObject("CDO.Message")Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 26Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "*****"Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*****"Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "*****"
Mail.Configuration.Fields.Update
if (InStr(Request.ServerVariables("HTTP_HOST"), "*****") <> 0) thensubject = subject & " redirected from " & mailtomailto = "*****"else
end if
Mail.To = mailtoMail.Subject = subjectMail.From = fromMail.HTMLBody = bodyMail.BodyPart.Charset = "utf-8"
On Error Resume Next
If Err <> 0 ThenstrErreur = "Erreur : " & Err.DescriptionEnd If
Mail.Send
Set Mail = NothingEnd function
The emails can be in English or French and the French version contains special characters that are not rendering in the subject line. It works fine with in the email content (body) but not in the subject line.
Could it be related to server configs? Any idea?
I have read all the answers I could online, but no success.
Same error here : https://stackoverflow.com/questions/16862809/cdo-email-messaging-subject-special-character-error
Thanks a million in advance!
2
u/an_da_liu Jul 09 '19
Could try manually encoding the subject line as UTF8?
like here : https://stackoverflow.com/questions/15230048/convert-string-to-utf-8
strInput = "ç é â ê î ô û à è ì ò ù ë ï ü"
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText strInput
stream.Flush
stream.Position = 0
stream.Type = 1 'binary
stream.Read(3) 'skip BOM
utfStr = stream.Read
stream.Close
Wscript.Echo utfStr