Source: ews_send.phps - download
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ews_send.phps 0.1
* by daif alotaibi (http://daif.net)
* daif@daif.net
*
* Link:
* http://daif.net/script/ews_send.phps
*
* Examples:
* $config = [
* 'ews_url' => 'https://mail.domain.ltd/EWS/exchange.asmx',
* 'username' => 'user',
* 'password' => '1234',
* ];
*
*/
// Configurations
$config = [
'ews_url' => 'https://mail.domain.ltd/EWS/exchange.asmx',
'username' => 'user',
'password' => '1234',
];
// set variables
$to = 'test.email@gmail.com';
$subject = 'Test subject';
$message = 'Test message';
// send the message
if(ews_send($to, $subject, $message, $config)) {
echo 'message has been sent';
} else {
echo 'error cannot send the message';
}
// Exchange web service function
function ews_send($to, $subject, $message, $config) {
$envelope = '<?xml version="1.0" encoding="utf-8"?>';
$envelope .= '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
$envelope .= ' xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" ';
$envelope .= ' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" ';
$envelope .= ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
$envelope .= ' <soap:Header>';
$envelope .= ' <t:RequestServerVersion Version="Exchange2013" />';
$envelope .= ' </soap:Header>';
$envelope .= ' <soap:Body>';
$envelope .= ' <m:CreateItem MessageDisposition="SendAndSaveCopy">';
$envelope .= ' <m:SavedItemFolderId>';
$envelope .= ' <t:DistinguishedFolderId Id="sentitems" />';
$envelope .= ' </m:SavedItemFolderId>';
$envelope .= ' <m:Items>';
$envelope .= ' <t:Message>';
$envelope .= ' <t:Subject>'.trim(strip_tags($subject)).'</t:Subject>';
$envelope .= ' <t:Body BodyType="HTML">'.htmlentities($message, ENT_QUOTES | ENT_IGNORE, 'UTF-8').'</t:Body>';
$envelope .= ' <t:ToRecipients>';
$envelope .= ' <t:Mailbox>';
$envelope .= ' <t:EmailAddress>'.$to.'</t:EmailAddress>';
$envelope .= ' </t:Mailbox>';
$envelope .= ' </t:ToRecipients>';
$envelope .= ' </t:Message>';
$envelope .= ' </m:Items>';
$envelope .= ' </m:CreateItem>';
$envelope .= ' </soap:Body>';
$envelope .= '</soap:Envelope>';
// header
$header[] = "Content-type: text/xml";
$header[] = "Connection: KEEP-Alive";
$header[] = "User-Agent: PHP-SOAP-CURL";
$header[] = "Method: POST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['ews_url']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, $config['username'].':'.$config['password']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $envelope);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$response = curl_exec($ch);
curl_close($ch);
if($response && preg_match('/NoError/', $response)) {
return TRUE;
}
return FALSE;
}
?>