首页 文章

如何使用Office365 REST API发送邮件?

提问于
浏览
0

我正在使用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

\n\n"; //echo "<pre>"; print_r($response); echo "</pre>"; die(); } return $response; }

2 回答

  • 0

    runcurl方法与问题相同..发送电子邮件的正确功能是//////////////////////////////////// //////////////////// //发送邮件功能///////////////////////// ///////////////////////////////

    if(isset($_POST["send"])) {
      $attachments = array();
       for($i = 0; $i < 1; $i++) {
         if(strlen(trim($_FILES["files"]["name"][$i])) > 0) {
            $content = base64_encode(file_get_contents($_FILES["files"]["tmp_name"] 
     [$i]));
            $attachment = array(
                "@odata.type" => "#Microsoft.OutlookServices.FileAttachment",
                "Name" => $_FILES["files"]["name"][$i],
                "ContentBytes" => $content
            );
            array_push($attachments, $attachment);
        }
      }
    
      $to = array();
      $toFromForm = explode(";", $_POST["to"]);
      foreach ($toFromForm as $eachTo) {
         if(strlen(trim($eachTo)) > 0) {
            $thisTo = array(
                "EmailAddress" => array(
                    "Address" => trim($eachTo)
                )
            );
            array_push($to, $thisTo);
         }
     }
     if (count($to) == 0) {
        die("Need email address to send email");
       }
        $userID = get_user_id();
     $request = array(
         "Message" => array(
             "Subject" =>$_POST["subject"],
             "ToRecipients" => $to,
             "Attachments" => $attachments,
             "Body" => array(
                 "ContentType" => "HTML",
                 "Content" => 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-Length: ". strlen($request)
        );
        $apiurl = $_SESSION["api_url"]. "/Users('$userID')/sendmail";
       $response = runCurl($apiurl, $request, $headers);
      //  echo "<pre>"; print_r($response); echo "</pre>";
        /* if $response["code"] == 202 then mail sent successfully */
    }
    
  • 0

    您收到了什么错误回复?你有HTTP响应 Headers ,还是在它下载之前失败了?

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

相关问题