发送带附件的电子邮件的问

时间:2011-06-06 16:07:37

标签: php soap exchange-server

我刚接触使用Exchangeclient类中的EWS。

我正在寻找一个简单的示例,说明如何发送带附件的电子邮件。我找到了有关如何发送电子邮件但未发送带附件的电子邮件的示例。

这是我的剧本:

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

我有以下肥皂请求。

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

我得到的错误是:

致命错误:未捕获的SoapFault异常:[a:ErrorSchemaValidation]请求模式验证失败:缺少必需的属性“Id”。

1 个答案:

答案 0 :(得分:2)

要发送带附件的电子邮件,您必须首先创建没有任何收件人的邮件(项目)(以及MessageDisposition“SendToNone”或类似内容)并将其保存在“草稿”文件夹中。然后创建一个CreateAttachment的请求,就像这样,其中$ key是您之前创建的项目的更改密钥(您必须回读服务器响应并在某处保存更改密钥,因为更改密钥会更改项目,并且每次修改都会更改):

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

然后更新消息(使用UpdateItem)以包含收件人,以便MessageDisposition类似于SendToAllAndSaveCopy。

(免责声明:我现在正在使用这种方法,除了为Attachments->FileAttachment->Content确定正确的格式外,它一切正常,看起来它应该是附件的编码base64数据 - 但是我的电脑无法打开我发送的附件。)

无论如何,我相信这是做到这一点的方式,当然我已经能够发送带有附件的邮件。