-1

I am working with Office365 REST API. I am facing problem in sending email. I can't find any help in core php.

I am able to access mails and attachments but unable to send a mail. Here is what I have tried so far.

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

 ?>

Here is the run curl method I used to access mails

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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Zeeshan Ch
  • 51
  • 6
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 09 '18 at 21:52

1 Answers1

0

What error response are you getting? Do you have HTTP response headers, or is it failing before it gets off your box?

One thing with REST - no need for the X-AnchorMailbox. That is an EWS concept. The target mailbox is always in the URL and our front end servers will ignore what you put in the X-AnchorMailbox header for the REST API. But that wouldn't be the cause of your failure.

  • Sterling The problem was i didnt made message array properly, then i needed to jsonencode it before passing it. – Zeeshan Ch Dec 10 '18 at 22:58
  • $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) ); – Zeeshan Ch Dec 10 '18 at 23:02
  • @ZeeshanCh If you managed to self-solve, please do not post your solution as a comment. Please post your working code as an answer and then accept it by awarding your own answer the green tick. – mickmackusa Feb 22 '19 at 05:21