简体   繁体   中英

problem sending email with attachment

I'm new to using EWS from Exchangeclient classes.

I'm looking for a simple example how to send an email with an attachment. I've found examples about how to send an email but not sending an email with an attachment.

This is my script:

$exchangeclient = new Exchangeclient();
$exchangeclient->init($username, $password, NULL, 'ews/Services.wsdl');
$exchangeclient->send_message($mail_from, $subject, $body, 'HTML', true, true);

I have the following soap request.

    $CreateItem->MessageDisposition = "SendAndSaveCopy";
    $CreateItem->SavedItemFolderId->DistinguishedFolderId->Id = "sentitems";

    $CreateItem->Items->Message->ItemClass = "IPM.Note";
    $CreateItem->Items->Message->Subject = $subject;
    $CreateItem->Items->Message->Body->BodyType = $bodytype;
    $CreateItem->Items->Message->Body->_ = $content;
    $CreateItem->Items->Message->ToRecipients->Mailbox->EmailAddress = $to; 
$CreateItem->Items->Message->Attachments->FileAttachment->AttachmentId = $attach['AttachmentId'];
    $CreateItem->Items->Message->Attachments->FileAttachment->Name = $attach['Name'];
    $CreateItem->Items->Message->Attachments->FileAttachment->ContentType = $attach['ContentType'];
    $CreateItem->Items->Message->Attachments->FileAttachment->ContentId = $attach['AttachmentId'];
    $CreateItem->Items->Message->Attachments->FileAttachment->Content = $attach['ContentId'];
    $CreateItem->Items->Message->Attachments->FileAttachment->Size = $attach['Size']; 

The error I am getting is:

Fatal error: Uncaught SoapFault exception: [a:ErrorSchemaValidation] The request failed schema validation: The required attribute 'Id' is missing.

In order to send email with an attachment you have to first create the Message (Item) without any recipients (and a MessageDisposition of "SendToNone" or something like that) and save it in your Drafts folder. THEN create a request for a CreateAttachment, like so, where $key is the changekey of the item you created earlier (you have to read back the server response and save the changekey somewhere, because the changekey changes for an item with every modification it undergoes):

$attachrequest->ParentItemId->ChangeKey = $key;
$attachrequest->Attachments->FileAttachment->Name = $attachment_name;
$attachrequest->Attachments->FileAttachment->ContentLocation = $attachment;
$attachrequest->Attachments->FileAttachment->Content = $attachment_content;
$attachrequest->Attachments->FileAttachment->ContentType = $attachment_contenttype;
$response = self::$ews->CreateAttachment($attachrequest); 

THEN you update the message (with an UpdateItem) to include recipients and so that the MessageDisposition is something like SendToAllAndSaveCopy.

(Disclaimer: I'm using this method now and it's all working fine, except for identifying the right format for Attachments->FileAttachment->Content , which looks like it should be the encoded base64 data of the attachment--but my computer can't open the attachments I'm sending.)

At any rate I believe this is the way to do it, and certainly I have been able to send messages with attachments with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM