Php 如何使用Office365 REST API发送邮件?
我正在使用Office365 REST API。我在发送电子邮件时遇到了问题。我在核心php中找不到任何帮助 我可以访问邮件和附件,但无法发送邮件。以下是我到目前为止所做的尝试Php 如何使用Office365 REST API发送邮件?,php,office365,office365api,outlook-restapi,Php,Office365,Office365api,Outlook Restapi,我正在使用Office365 REST API。我在发送电子邮件时遇到了问题。我在核心php中找不到任何帮助 我可以访问邮件和附件,但无法发送邮件。以下是我到目前为止所做的尝试 // SEND MAIL FUNCTION if(isset($_GET['send_email'])){ $resuu = "okay"; $resuu = SendMail(); echo $resuu; } function SendMail(){ $userID = get_user_id(); $hea
// SEND MAIL FUNCTION
if(isset($_GET['send_email'])){
$resuu = "okay";
$resuu = SendMail();
echo $resuu;
}
function SendMail(){
$userID = get_user_id();
$headers = array(
"User-Agent: php-tutorial/1.0",
"Authorization: Bearer ".token()->access_token,
"Accept: application/json",
"client-request-id: ".makeGuid(),
"return-client-request-id: true",
"X-AnchorMailbox: ". get_user_email()
);
$newmail = '{
"Message": {
"Subject": "Sending 1st email dont fails plz?",
"Body": {
"ContentType": "Text",
"Content": "The new cafeteria is open."
},
"ToRecipients": [
{
"EmailAddress": {
"Address": "chxanu123@gmail.com"
}
}
]
},
"SaveToSentItems": "true"
}';
$outlookApiUrl = $_SESSION["api_url"] . "/Users('$userID')/sendmail";
$response = runCurl($outlookApiUrl, $newmail, $headers);
return $response;
//Session[api_url] contains $_SESSION["api_url"] = //"https://outlook.office.com/api/v2.0";
}
?>
下面是我用来访问邮件的runcurl方法
function runCurl($url, $post = null, $headers = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $post == null ? 0 : 1);
if($post != null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if($headers != null) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code >= 400) {
echo "Error executing request to Office365 api with error
code=$http_code<br/><br/>\n\n";
//echo "<pre>"; print_r($response); echo "</pre>";
die();
}
return $response;
}
函数runCurl($url,$post=null,$headers=null){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,$POST==null?0:1);
如果($post!=null){
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
}
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
如果($headers!=null){
curl_setopt($ch,CURLOPT_头,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
}
$response=curl\u exec($ch);
$http\u code=curl\u getinfo($ch,CURLINFO\u http\u code);
卷曲关闭($ch);
如果($http_代码>=400){
echo“执行对Office365 api的请求时出错,错误为
code=$http\u code\n\n”; //回显“;打印($response);回显“; 模具(); } 返回$response; }
您得到了什么错误响应?您是否有HTTP响应头,或者在它离开您的框之前失败了
剩下的一件事就是不需要X-AnchorMailbox。这是一个EWS概念。目标邮箱始终位于URL中,我们的前端服务器将忽略您在RESTAPI的X-AnchorMailbox头中输入的内容。但这并不是你失败的原因。请阅读-总结是,这不是向志愿者致辞的理想方式,可能对获得答案产生反作用。请不要将此添加到您的问题中。问题是我没有正确创建消息数组,然后我需要在传递它之前对其进行JSONECODE。$userID=get_user_id()$请求=数组(“消息”=>数组(“主题”=>u POST[“主题”],“ToRecipients”=>$to,“附件”=>$Attachments),“正文”=>数组(“内容类型”=>“HTML”,“内容”=>utf8编码($\u POST[“消息”]))$request=json_encode($request)$headers=array(“用户代理:php教程/1.0”,“授权:承载者”.token()->访问令牌,“接受:应用程序/json”,“内容类型:应用程序/json”,“内容长度:”.strlen($request))@ZeeshanCh如果你能自行解决问题,请不要发表评论。请将您的工作代码作为答案发布,然后通过给您自己的答案打绿色勾来接受它。