Search code examples
php-ews

sending email to multiple accounts using php ews


The below code is used for sending email using jamesiarmes/php-ews in my application

 $request = new \jamesiarmes\PhpEws\Request\CreateItemType();
  $request->MessageDisposition = "SendOnly";
  $request->SavedItemFolderId->DistinguishedFolderId->Id = "sentitems";
  $request->Items->Message->ItemClass = "IPM.Note";
  $request->Items->Message->Subject = "exchange new mail";
  $request->Items->Message->Body->BodyType = 'HTML';
  $request->Items->Message->Body->_ = "This is a test mail as a part of exchange settings set up ";
 $request->Items->Message->ToRecipients->Mailbox->EmailAddress = "[email protected]";
 $response = $this->app['ews']->CreateItem($request);

But the problem is I can add only one email address as recipient, how can I add multiple email addresses in ToRecipients?


Solution

  • I checked out the php-ews documentation. You can create an array with multiple recipients this way:

    $toAddresses = array();
    
    $toAddresses[0] = new EWSType_EmailAddressType();
    $toAddresses[0]->EmailAddress = '[email protected]';
    $toAddresses[0]->Name = 'John Harris';
    
    $toAddresses[1] = new EWSType_EmailAddressType();
    $toAddresses[1]->EmailAddress = '[email protected]';
    $toAddresses[1]->Name = 'Sara Smith';
    

    And then add it to your object like this:

    $request->Items->Message->ToRecipients = $toAddresses;
    

    Try this and feedback me please.