图形API使用Powershell发送电子邮件,HTML正文中带有&符号

zc0qhyus  于 2023-02-04  发布在  Shell
关注(0)|答案(1)|浏览(90)

我正在尝试发送图形API电子邮件与html正文有一个href URL,其中有&符号。
尝试将&编码为%26,收到的电子邮件中包含%26,单击时抛出404错误。
尝试将内容类型更改为"application/x-www-form-urlencoded",但未触发电子邮件。
样本代码:
我有$toJsonAddress作为一个数组。

$htmlhead="<html>
<style>
    body {
        font-family: 'verdana';font-size: 16px;
    }
    h1 {
        font-family: 'verdana';font-size: 18px;color: grey;
    }
    h2 {
        font-family: 'verdana';font-size: 18px;color: #2596be;
    }
    h3 {
        font-family: 'verdana';font-size: 16px;color: black;
    }
</style>"
#Header for the message
$HtmlBody = "<body>
<h1>test</h1>
<p>request <a href=""https://www.somedomain/help?id=sc_cat_item&amp;sys_id=12345"">here.</a></p>
"
$content = "</body></html>" + $HtmlHead + $HtmlBody | ConvertTo-Json
Write-Output "content is -->> $content"

$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
                    {
                        "message": {
                          "subject": "$mailSubject",
                          "body": {
                            "contentType": "HTML",
                            "content": "$content "
                          },
                          "toRecipients": [
                          $toJsonAddress
                          ]
                        },
                        "saveToSentItems": "false"
                      }
"@

Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend -ContentType "application/x-www-form-urlencoded"

JSON转换、打印和接收如下错误:

content is -->> "</body></html><html>\r\n<style>\r\n\tbody {\r\n\t\tfont-family: 'verdana';font-size: 16px;\r\n\t}\r\n\th1 {\r\n\t\tfont-family: 'verdana';font-size: 18px;color: grey;\r\n\t}\r\n\th2 {\r\n\t\tfont-family: 'verdana';font-size: 18px;color: #2596be;\r\n\t}\r\n\th3 {\r\n\t\tfont-family: 'verdana';font-size: 16px;color: black;\r\n\t}\r\n</style><body>\r\n     <h1>test</h1>\r\n<p>request <a href=\"https://www.somedomain/help?id=sc_cat_item&amp;sys_id=12345\">here.</a></p>\r\n"
Invoke-RestMethod: C:\Temp\imf4ah0e.3rb\fbb14734-21b4-4a83-a2ea-c774b3f7045a.ps1:67
Line |
  67 |  Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body  …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {"error":{"code":"RequestBodyRead","message":"A supported MIME type
     | could not be found that matches the content type of the response. None
     | of the supported type(s) 'Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaType,
     | Microsoft.OData.ODataMediaTyp...' matches the content type
     | 'application/x-www-form-urlencoded'."
pcww981p

pcww981p1#

尝试使用字符&amp;代替&作为与号。
此外,href属性中的URL应该用双引号括起来

$HTMLBody = "<html><body><p>Portal: <a href=""https://sampledns/overview?ff=abc&amp;ln=xyz"">some text</a></p></body></html>"

相关问题