首页 > 解决方案 > 如何强制通过 Microsoft Graph API 发送的电子邮件使用 UTF-8 编码?

问题描述

我正在使用 Microsoft Graph API 发送 HTML 电子邮件。在发送过程中,API 似乎正在更改charset我在<meta>标签UTF-8中定义的Windows-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")并遇到同样的问题。

有什么想法吗?

标签: rubyemailencodingmicrosoft-graph-apioffice365

解决方案


推荐阅读