如何强制通过microsoft graph api发送的电子邮件使用utf-8编码?

zlhcx6iw  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(261)

我正在使用microsoft graph api发送html电子邮件。在发送过程中,api似乎正在更改 charset 我已经在 <meta> 标记自 UTF-8Windows-1252 这会导致在查看电子邮件时出现奇怪的字符。通过sengrid api发送相同的html会保留正确的 charset=UTF-8 在html中。
以下是正在发送的html:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    ... UTF-8 encoded content here ...
  </body>
</html>

下面是我用来发送电子邮件的ruby代码的通用版本:

message = {
  'subject' => 'My Subject',
  'body' => {
    'contentType' => 'HTML',
    'content' => html_content # as defined above
  },
  'toRecipients' => recipients,
  'from' => {
    'emailAddress' => {
      'address" => 'email@whatever.com'
    }
  },
  'sender' => {
    'emailAddress' => {
      'address' => 'email@whatever.com'
    }
  }
}

response = Curl.post('https://graph.microsoft.com/v1.0/me/sendMail', {"Message" => message}.to_json) do |http|
  http.headers['Authorization'] = "Bearer #{key}"
  http.headers['Content-Type'] = 'application/json; charset=utf-8'
end

检查已发送电子邮件的html后 meta 标记被更改为:

<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">

我在api中看不到任何可以传递以强制特定编码类型的参数,我看到的所有其他答案都建议只需设置 charset=UTF-8 在html中,足够正确设置编码。
我知道html和主题都是utf-8,但我已经尝试使用强制编码 html_content.encode("UTF-8", invalid: :replace, undef: :replace, replace: "").force_encoding("UTF-8") 遇到了同样的问题。
有什么想法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题