EWS - php sending email with attachment

2019-08-17 02:37发布

问题:

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);

function - PHP Classes:

    function send_message($to, $subject, $content, $bodytype="Text", $saveinsent=true, $markasread=true) {
    $this->setup();

    if($saveinsent) {
        $CreateItem->MessageDisposition = "SendOnly";
        $CreateItem->SavedItemFolderId->DistinguishedFolderId->Id = "sentitems";
    }
    else
        $CreateItem->MessageDisposition = "SendOnly";

    $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;

    if($markasread)
        $CreateItem->Items->Message->IsRead = "true";

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

    $this->teardown();

    if($response->ResponseMessages->CreateItemResponseMessage->ResponseCode == "NoError")
        return true;
    else {
        $this->lastError = $response->ResponseMessages->CreateItemResponseMessage->ResponseCode;
        return false;
    }

}

回答1:

You have to first save the email as a draft (with the appropriate message disposition), then CreateAttachment() so it has an attachment, then edit it with UpdateItem() so the message disposition is SendOnly. Then it will be sent.

See David Sterling's reply on this thread: http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/f7d5257e-ec98-40fd-b301-f378ba3080fd/ (It's about Meeting Requests but they work the same way.)