jamesiarmes / php-ews

PHP Exchange Web Services
http://jamesarmes.com/php-ews/
MIT License
567 stars 302 forks source link

transform from CDO #561

Open JulieVelghe opened 4 years ago

JulieVelghe commented 4 years ago

Version (e.g. 1.0, dev-master):1.0 PHP version: Microsoft Exchange version:2016

Description of problem: I have to take over an old php project in which the code to connect to the exchange server was written with CDO. The code itself doesn't send the mails, but rather sends a message to the exchange server, that then works with the message data to send out confirmation mails. The exchange server used to be Exchange2010, but now the client upgraded to Exchange2016 so CDO is no longer supported. ex: $this->message = new COM('CDO.Message'); $this->message->Configuration = $this->configure(); //filling in subject, from, to ... $messageCon = new COM('CDO.Configuration'); $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserver'] = "mysmtpserver"; $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpserverport'] = 25; $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/sendusing'] = 2; $messageCon->Fields['http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout'] = 60; $messageCon->Fields->Update(); $this->message->Send();

Now I have to transform this cdo code to an alternative. I thought about this library, but I cannot get it to work. As of now, I have the following code (that doesn't work)

$server = 'mysmtpserver'; $username = ''; $password = ''; $version = Client::VERSION_2016; $ews = new Client($server, $username, $password, $version); $this->ews->setCurlOptions(array(CURLOPT_LOGIN_OPTIONS => 'AUTH=0')); $this->ews->setCurlOptions(array(CURLOPT_SSL_VERIFYPEER => false)); $msgRequest = new CreateItemType(); $msgRequest->Items = new NonEmptyArrayOfAllItemsType(); $msgRequest->MessageDisposition = MessageDispositionType::SENDONLY; $msg = new MessageType(); $msg->Subject = $subject; $msg->ToRecipients = new ArrayOfRecipientsType(); $msg->From = new SingleRecipientType(); $msg->From->Mailbox = new EmailAddressType(); $msg->From->Mailbox->EmailAddress = $fromAddress; $toAddresses = array(); foreach ($toAddress as $i => $address) { $recipient = new EmailAddressType(); $recipient->EmailAddress = $address; $msg->ToRecipients->Mailbox[$i] = $recipient; } $msg->Body = new BodyType(); if ($htmlBody != '') { $msg->Body->BodyType = BodyTypeType::HTML; $msg->Body-> = $htmlBody; } else { $msg->Body->BodyType = BodyTypeType::TEXT; $msg->Body->_ = $textBody; } $msgRequest->Items->Message[] = $msg; $response = $this->$ews->CreateItem($msgRequest); $response_messages = $response->ResponseMessages->SendItemResponseMessage; foreach ($response_messages as $response_message) { // Make sure the request succeeded. if ($response_message->ResponseClass != ResponseClassType::SUCCESS) { $code = $response_message->ResponseCode; $message = $response_message->MessageText; $this->error = $code; $this->failed = true; continue; } $this->failed = false; }

Any help would be greatly appreciated

Example request:

// Formatted example request.

Example response:

// Formatted example response.

Additional details: