jamesiarmes / php-ews

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

create-inline-attachment.php #467

Open kiru1978 opened 6 years ago

kiru1978 commented 6 years ago

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

Description of problem: I used your example for my platform but it doesn't work. I have this error response:

The request failed schema validation: The element 'FileAttachment' in namespace 'http://schemas.microsoft.com/exchange/services/2006/types' has invalid child element 'Size' in namespace 'http://schemas.microsoft.com/exchange/services/2006/types'. List of possible elements expected: 'ContentLocation, Content' in namespace 'http://schemas.microsoft.com/exchange/services/2006/types'. Example request: I used your library in my Laravel App. The calendar works perfect but now i have this problem with the file Attachment in the email. can you Help me please?

I changed also all schemas link in your library files from 2006 to 2013 (because i use exhange 2013) but it doesn't work.

kiru1978 commented 6 years ago

Here the code of my function

public function sendEmail(\Illuminate\Http\Request $request){

    // Replace with the path to the image file to be attached and the recipient
    // information.
    $file_path = public_path().'/img/defaults/article_thumb.png';

    $to = explode(";",str_replace(',', ';',str_replace(' ', '',  trim($request->emailto))));
    $subject = $request->subject;
    $emailmessage = $request->message;

    // Set connection information.
    $host = $this->host;
    $username = $this->username;
    $password = $this->exchange_pass; 
    $version = $this->version;
    $client = new Client($host, $username, $password, $version);

    // Open file handlers.
    $file = new \SplFileObject($file_path);

    $finfo = finfo_open();

    $filename = $file->getBasename();

    // Build the request,
    $request = new CreateItemType();
    $request->Items = new NonEmptyArrayOfAllItemsType();

    // Save the message, but do not send it.
    $request->MessageDisposition = MessageDispositionType::SEND_AND_SAVE_COPY;

    // Create the message.
    $message = new MessageType();
    $message->Subject = $subject;
    $message->ToRecipients = new ArrayOfRecipientsType();
    $message->Attachments = new NonEmptyArrayOfAttachmentsType();

    // Set the sender.
    $message->From = new SingleRecipientType();
    $message->From->Mailbox = new EmailAddressType();
    $message->From->Mailbox->EmailAddress = $username;

    // Set the recipient.
    foreach($to as $mailbox){
        $recipient = new EmailAddressType();
        $recipient->Name = '';
        $recipient->EmailAddress = $mailbox;
        $message->ToRecipients->Mailbox[] = $recipient;
    }

    // Set the message body.
    $message->Body = new BodyType();
    $message->Body->BodyType = BodyTypeType::HTML;
    $message->Body->_ = $emailmessage;

    // Build the file attachment.
    $attachment = new FileAttachmentType();

    $attachment->IsInline = true;
    $attachment->Content = $file->openFile()->fread($file->getSize());
    $attachment->Name = $filename;
    $attachment->IsInline = true;
    $attachment->ContentType = finfo_file($finfo, $file_path);
    $attachment->ContentId = $filename;
    $attachment->Size = $file->getSize();

    $message->Attachments->FileAttachment[] = $attachment;

    // Add the message to the request.
    $request->Items->Message[] = $message;

    $response = $client->CreateItem($request);

    // Iterate over the results, printing any error messages or message ids.
    $response_messages = $response->ResponseMessages->CreateItemResponseMessage;

    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;
            $result = 0;
            $messaggio = "Message failed to create with \'$code: $message\'";
            continue;
        }
        else {
            $result = 1;
            $messaggio = "Message created successfully.";
        }
        // Iterate over the created messages, printing the id for each.
        foreach ($response_message->Items->Message as $item) {
            $output = '- Id: ' . $item->ItemId->Id . "\n";
            $output .= '- Change key: ' . $item->ItemId->ChangeKey . "\n";

        }
        return json_encode(['result' => $result, 'message' => $messaggio]);
    }

}