我正在使用office 365 rest api并发送附件,当我发送附件到办公室365邮件以外的任何电子邮件地址(如gmail.com,yahoo.com等)时在收件箱中看到的附件,但在办公室365邮件的情况下,它只显示内容。
谢谢, 香卡
答案 0 :(得分:0)
我可以使用office 365的rest api向office 365和yahoo,gmail等发送附件。
附件数组需要创建如下
<?php
$k=0;
$attchmentArr=array();
foreach($requestjson['name'] as $file){
$attchmentArr[]=array(
"@odata.type"=> "#Microsoft.OutlookServices.FileAttachment",
"Name"=> $file,
"ContentBytes"=> $requestjson['content_bytes'][$k],
);
$k++;
}
?>
您需要创建如下的数组。
<?php
$post_data=array(
"Message"=>array(
"Subject"=>$requestjson['subject'],
"Body"=>array(
'ContentType'=>'HTML',
'Content'=> $requestjson['message'],
),
"From"=>array(
"EmailAddress"=>array(
'Name'=>$fromName[0],
'Address'=>CARE_ACOCUNT
)
),
"ToRecipients"=>array(
array(
"EmailAddress"=>array(
'Name'=>$toName[0],
"Address"=>$requestjson['to_address']
)
)
),
"Attachments"=>$attchmentArr,
),
"SaveToSentItems"=> "true"
);
?>
然后使用curl发送邮件
<?php
$url="https://outlook.office365.com/api/beta/me/sendmail";
$post_data = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_USERPWD, CARE_ACOCUNT . ':' . CARE_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $post_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = json_decode($response, true);
$status_arr=array(200,201,202,204);
curl_close($ch);
if(in_array($status,$status_arr)){
return $response;
} else {
return 'error';
}