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

这篇博客讲述了作者在尝试使用PHP核心功能结合Office365RESTAPI发送邮件时遇到的困难。尽管能成功访问邮件和附件,但在实际发送邮件过程中遇到了问题。代码示例展示了如何设置请求头和邮件内容,但遇到了错误,尚未找到解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

"; print_r($response); echo "
";

die();

}

return $response;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值