I am using the code below to send email from Microsoft exchange: The log file shows that the error comes up after failing to send to recipient; see below:
[2024-12-17 13:27:41] Connecting with username: sender
[2024-12-17 13:27:41] Sending email to: [email protected]
[2024-12-17 13:27:42] Error: SOAP client returned status of 401.
Below is a snippet of my code, site is on windows server, tried reinstalling the libraries but still nothing. What have I missed and what can I check .
Apache Error Log is below:
I have edited the php.ini Curl like as per the suggestions.
<?php
require '../vendor/autoload.php';
use \jamesiarmes\PhpEws\Client;
use \jamesiarmes\PhpEws\Request\CreateItemType;
use \jamesiarmes\PhpEws\ArrayType\ArrayOfRecipientsType;
use \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfAllItemsType;
use \jamesiarmes\PhpEws\Enumeration\BodyTypeType;
use \jamesiarmes\PhpEws\Enumeration\MessageDispositionType;
use \jamesiarmes\PhpEws\Type\BodyType;
use \jamesiarmes\PhpEws\Type\EmailAddressType;
use \jamesiarmes\PhpEws\Type\MessageType;
function logMessage($message) {
$timestamp = date('Y-m-d H:i:s');
$logFile = __DIR__ . '/ews_debug.log';
file_put_contents($logFile, "[$timestamp] $message\n", FILE_APPEND);
}
try {
// Basic configuration
$host = 'domain.co.bz';
$username = 'username';
$password = 'usrPassword';
logMessage("Connecting with username: $username");
// Create client
$client = new Client($host, $username, $password);
// Only essential CURL options
$client->setCurlOptions([
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
]);
// Create message
$request = new CreateItemType();
$request->Items = new NonEmptyArrayOfAllItemsType();
$request->MessageDisposition = MessageDispositionType::SEND_ONLY;
$message = new MessageType();
$message->Subject = 'Test Email';
$message->ToRecipients = new ArrayOfRecipientsType();
$recipient = new EmailAddressType();
$recipient->EmailAddress = '[email protected]';
$message->ToRecipients->Mailbox[] = $recipient;
$message->Body = new BodyType();
$message->Body->BodyType = BodyTypeType::TEXT;
$message->Body->_ = 'Test message from EWS';
$request->Items->Message[] = $message;
logMessage("Sending email to: " . $recipient->EmailAddress);
$response = $client->CreateItem($request);
logMessage("Email sent successfully");
echo "Email sent successfully!";
} catch (Exception $e) {
$error = "Error: " . $e->getMessage();
logMessage($error);
echo $error;
}
?>