EWS - php sending email with attachment

泄露秘密 提交于 2019-12-11 16:54:00

问题


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



来源:https://stackoverflow.com/questions/6250796/ews-php-sending-email-with-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!