开发者

problem sending email with attachment

开发者 https://www.devze.com 2023-03-10 14:06 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消