office365 rest api 发送的邮件附件未显示在收件箱中
message attachment sent by office365 rest api is not shown in inbox
我正在使用 office 365 rest api 并发送附件,当我将附件发送到 office 365 邮件以外的任何电子邮件地址时,例如 (gmail.com, yahoo.com 等)然后在收件箱中看到附件,但如果是 Office 365 邮件,它只显示内容。
谢谢,
香卡
我可以使用 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';
}
谢谢, 香卡
我可以使用 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';
}