尝试使用 Microsoft Graph API 发送电子邮件时出现内部服务器错误

问题描述 投票:0回答:1

我正在尝试通过 Laravel 应用程序使用 Microsoft Graph API 发送电子邮件;这是代码

$emailData = [
            "message" => [
                "subject" => 'Test', 
                "body" => [
                    "contentType" => "text", 
                    "content" =>  "Test Email!"
                 ], 
                "toRecipients" => [
                    [
                        "emailAddress" => [
                            "address" => "[email protected]" 
                        ] 
                    ] 
                ], 
                "from" => [
                        "emailAddress" => [
                            "address" => "[email protected]" 
                        ] 
                ] 
            ]
        ];
       
        $response = Http::withToken($this->token)
            ->withHeaders(['Content-Type' => 'application/json'])
            ->post("https://graph.microsoft.com/v1.0/users/[email protected]/sendMail", json_encode($emailData));

我已经获得了访问和刷新令牌。

我只从回复中得到这个

{"error":{"code":"ErrorInternalServerError","message":"An internal server error occurred. The operation failed."}}

我认为问题可能出在数据结构上,但我找不到它。

如有任何帮助,我们将不胜感激!

php laravel microsoft-graph-api
1个回答
0
投票

问题出在你的

HTTP
客户身上。当发送
HTTP
请求时,Laravel
JSON
客户端会自动将数据编码处理为
POST
。因此,在您的情况下,您的
$emailData
正在被双重编码,因为您已经将
json_encode($emailData)
传递给
post(...

正确的语法是。试试这个。

$response = Http::withToken($this->token)
            ->withHeaders(['Content-Type' => 'application/json'])
            ->post("https://graph.microsoft.com/v1.0/users/[email protected]/sendMail",[
"key-name"=> $emailData ]);

post(..)
接受数据数组作为第二个参数。有关详细信息,请查看 official-docs

© www.soinside.com 2019 - 2024. All rights reserved.