ruby - 如何强制通过 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")
并遇到同样的问题。
有什么想法吗?
解决方案
推荐阅读
- javascript - 输出到新行
- c++ - 重新分配向量时,向量中的内存会发生什么
- apache-spark - “org.apache.spark.SparkException:Kryo 序列化失败:缓冲区溢出。可用:0,必需:n”错误是否有解决方法?
- java - 控制器在浏览器中打开文件而不是下载它
- anylogic - 队列中的平均等待数和等待时间
- c# - 使用 RedirectToAction 方法传递值
- reactjs - 无法解决 reactjs 中谷歌身份验证中的 firebase/app 错误
- elasticsearch - KafkaConnect elasticSearch 文档 ID 创建
- tensorflow - keras Conv2d 值错误:负维度大小
- django - gunicorn ImportError:没有名为 webob 的模块