【问题标题】:How to send mail using Office365 REST API?如何使用 Office365 REST API 发送邮件?
【发布时间】:2018-12-11 07:05:43
【问题描述】:

我正在使用 Office365 REST API。我在发送电子邮件时遇到问题。我在核心 php 中找不到任何帮助。

我可以访问邮件和附件,但无法发送邮件。这是我到目前为止所尝试的。

// 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";
    }

 ?>

这是我用来访问邮件的run curl方法

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;
}

【问题讨论】:

标签: php office365 office365api outlook-restapi


【解决方案1】:

您得到什么错误响应?您是否有 HTTP 响应标头,或者它在离开您的盒子之前就失败了?

REST 的一件事 - 不需要 X-AnchorMailbox。那是一个 EWS 概念。目标邮箱始终位于 URL 中,我们的前端服务器将忽略您在 REST API 的 X-AnchorMailbox 标头中放置的内容。但这不会是你失败的原因。

【讨论】:

  • 英镑问题是我没有正确制作消息数组,然后我需要在传递它之前对其进行jsonencode。
  • $userID = get_user_id(); $request = array("Message" => array("Subject" =>$_POST["subject"], "ToRecipients" => $to, "Attachments" => $attachments, "Body" => array("ContentType " => "HTML", "内容" => utf8_encode($_POST["message"]) ) ) ); $request = json_encode($request); $headers = array("User-Agent: php-tutorial/1.0", "Authorization: Bearer".token()->access_token, "Accept: application/json", "Content-Type: application/json", "Content -长度:“.strlen($request));
  • @ZeeshanCh 如果您设法自行解决,请不要将您的解决方案作为评论发布。请发布您的工作代码作为答案,然后通过授予您自己的答案以绿色勾号来接受它。
猜你喜欢
  • 1970-01-01
  • 2017-10-12
  • 2018-02-23
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 2017-12-19
  • 2019-07-14
相关资源
最近更新 更多