问题描述
我没有运气使用php-ews从Exchange 2010服务器获取电子邮件的邮件正文。这是我的代码: $ ews = new ExchangeWebServices(hostname,username,password,ExchangeWebServices :: VERSION_2010);
$ request = new EWSType_FindItemType();
$ itemProperties = new EWSType_ItemResponseShapeType();
$ itemProperties-> BaseShape = EWSType_DefaultShapeNamesType :: ALL_PROPERTIES;
$ itemProperties-> BodyType = EWSType_BodyTypeResponseType :: BEST;
$ request-> ItemShape = $ itemProperties;
//如果我不包括以下三个句子,我得到一个PHP致命错误,说没有找到Basepoint
$ request-> IndexedPageItemView = new EWSType_IndexedPageViewType() ;
$ request-> IndexedPageItemView-> BasePoint ='Beginning';
$ request-> IndexedPageItemView-> Offset = 0;
$ request-> ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$ request-> ParentFolderIds-> DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$ request-> ParentFolderIds-> DistinguishedFolderId-> Id = EWSType_DistinguishedFolderIdNameType :: INBOX;
$ request-> Traversal = EWSType_ItemQueryTraversalType :: SHALLOW;
$ result = new EWSType_FindItemResponseMessageType();
$ result = $ ews-> FindItem($ request);
if($ result-> ResponseMessages-> FindItemResponseMessage-> ResponseCode =='NoError'&& $ result-> ResponseMessages-> FindItemResponseMessage-> ResponseClass =='Success'){
$ message = $ result-> ResponseMessages-> FindItemResponseMessage-> Items-> Message;
//输入if循环,但$ message变量不含
print_r($ message); exit;
// var_dump($ message)显示NULL
}
if 输入循环,但$ message变量不包含任何内容。通常它应该是一个可以从中检索电子邮件正文的对象。
我做错了什么?
我在if()语句里面试了一下,我收到了$ message对象:
$ message = $ result- > ResponseMessages-> FindItemResponseMessage-> RootFolder->项 - >消息;
print_r($ message); exit;
缺少RootFolder属性。但是,生成的$ message变量不包含任何包含电子邮件正文的body属性。这是print_r($ message)的样子:
stdClass Object
/ pre>
(
[项目Id] => stdClass的对象
(
[ID] => AAMkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwBGAAAAAABI9LX / rIDdRr5XMdDM8RGZBwAL3uOTQAYYQ4U8CTQP / + wKAAAAWsnbAAAL3uOTQAYYQ4U8CTQP / + wKAAAAWvd0AAA =
[ChangeKey] => CQAAABYAAAAL3uOTQAYYQ4U8CTQP / + wKAAAAWyCi
)[ParentFolderId] => ; stdClass Object
(
[Id] => AQMkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwAuAAADSPS1 / 6yA3Ua + VzHQzPERmQEAC97jk0AGGEOFPAk0D // sCgAAAVrJ2wAAAA ==
[ChangeKey] => AQAAAA ==
)
[ItemClass] => IPM.Note
[主题] =>测试
[敏感度] =>正常
[DateTimeReceived] => 2012-09-05T13 :03:08Z
[Size] => 4754
[重要性] =>正常
[IsSubmitted] =>
[IsDraft] =>
[IsFromMe] =>
[IsRe发送] =>
[IsUnmodified] => 1
[DateTimeSent] => 2012-09-05T13:03:00Z
[DateTimeCreated] => 2012-09-05T13:03:08Z
[DisplayCc] =>
[DisplayTo] => sara cooper
[HasAttachments] =>
[文化] => en-IN
[EffectiveRights] => stdClass Object
(
[CreateAssociated] =>
[CreateContents] =>
[CreateHierarchy] =>
[Delete] => 1
[修改] => 1
[读取] => 1
)
[LastModifiedName] => Lin T
[LastModifiedTime] => 2012-09-05T13:03:08Z
[IsAssociated] =>
[WebClientReadFormQueryString] => ?ae = Item& a = Open& t = IPM.Note& id = RgAAAABI9LX%2frIDdRr5XMdDM8RGZBwAL3uOTQAYYQ4U8CTQP%2f%2bwKAAAAWsnbAAAL3uOTQAYYQ4U8CTQP%2f%2bwKAAAAWvd0AAAJ& exvsurl = 1
[ConversationId] => stdClass Object
(
[Id] => AAQkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwAQAJ56TrE0QUIAk59ALIqWrrY =
)
[发件人] => stdClass Object
(
[Mailbox] => stdClass Object
(
[Name] => Lin T
[MailboxType] => OneOff
)
)
[IsReadReceiptRequested] =>
[ConversationIndex] => Í[ConversationTopic] =>测试
[From] => stdClass Object
(
[Mailbox] => stdClass Object
(
[Name] => Lin T
[MailboxType] => OneOff
)
)
[InternetMessageId] => < [email protected]>
[IsRead] =>
[ReceivedBy] => stdClass Object
(
[Mailbox] => stdClass对象
(
[Name] => sara cooper
[MailboxType] => OneOff
)
)
[ReceivedRepresenting] => stdClass Object
(
[Mailbox] => stdClass对象
(
[Name] => sara cooper
[MailboxType] => OneOff
)
)
)
我还需要发件人和接收者的EMAIL ADDRESS。这里只显示名字和姓氏。请帮忙,这真的很紧急。
解决方案我终于找到了!以下是从Exchange 2010服务器检索未读电子邮件的方法:
$ ews = new ExchangeWebServices(hostname,username ,密码,ExchangeWebServices :: VERSION_2010);
$ request = new EWSType_FindItemType();
$ itemProperties = new EWSType_ItemResponseShapeType();
$ itemProperties-> BaseShape = EWSType_DefaultShapeNamesType :: ID_ONLY;
$ itemProperties-> BodyType = EWSType_BodyTypeResponseType :: BEST;
$ request-> ItemShape = $ itemProperties;
$ fieldType = new EWSType_PathToUnindexedFieldType();
$ fieldType-> FieldURI ='message:IsRead';
$ constant = new EWSType_FieldURIOrConstantType();
$ constant-> Constant = new EWSType_ConstantValueType();
$ constant-> Constant-> Value =0;
$ IsEqTo = new EWSType_IsEqualToType();
$ IsEqTo-> FieldURIOrConstant = $ constant;
$ IsEqTo-> Path = $ fieldType;
$ request->限制=新EWSType_RestrictionType();
$ request->限制 - > IsEqualTo = new EWSType_IsEqualToType();
$ request->限制 - > IsEqualTo-> FieldURI = $ fieldType;
$ request->限制 - > IsEqualTo-> FieldURIOrConstant = $ constant;
$ request-> IndexedPageItemView = new EWSType_IndexedPageViewType();
$ request-> IndexedPageItemView-> BasePoint ='Beginning';
$ request-> IndexedPageItemView-> Offset = 0;
$ request-> ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType();
$ request-> ParentFolderIds-> DistinguishedFolderId = new EWSType_DistinguishedFolderIdType();
$ request-> ParentFolderIds-> DistinguishedFolderId-> Id = EWSType_DistinguishedFolderIdNameType :: INBOX;
$ request-> Traversal = EWSType_ItemQueryTraversalType :: SHALLOW;
$ result = new EWSType_FindItemResponseMessageType();
$ result = $ ews-> FindItem($ request);
/ * echo'< pre>';
print_r($ result); exit; * /
if($ result-> ResponseMessages-> FindItemResponseMessage-> ResponseCode =='NoError'&& $ result-> ResponseMessages- > FindItemResponseMessage-> ResponseClass =='Success'){
$ count = $ result-> ResponseMessages-> FindItemResponseMessage-> RootFolder-> TotalItemsInView; $($ i = 0; $ i $ $ count; $ i ++)
$ message_id = $ result-> ResponseMessages-> FindItemResponseMessage-> RootFolder-> Items->消息[$ i]于 - > ItemId->标识;
$ request = new EWSType_GetItemType();
$ request-> ItemShape = new EWSType_ItemResponseShapeType();
$ request-> ItemShape-> BaseShape = EWSType_DefaultShapeNamesType :: ALL_PROPERTIES;
$ request-> ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$ request-> ItemIds-> ItemId = new EWSType_ItemIdType();
$ request-> ItemIds-> ItemId-> Id = $ message_id;
$ response = $ ews-> GetItem($ request);
// print_r($ response); exit;
if($ response-> ResponseMessages-> GetItemResponseMessage-> ResponseCode =='NoError'&&
$ response-> ResponseMessages-> GetItemResponseMessage-> ResponseClass ==' Success'){
$ message = $ response-> ResponseMessages-> GetItemResponseMessage-> Items-> Message;
//处理消息数据。
}
}
}
I am having no luck fetching the message body of an email from an Exchange 2010 server using php-ews. This is my code :
$ews = new ExchangeWebServices("hostname", "username","password",ExchangeWebServices::VERSION_2010); $request = new EWSType_FindItemType(); $itemProperties = new EWSType_ItemResponseShapeType(); $itemProperties->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES; $itemProperties->BodyType = EWSType_BodyTypeResponseType::BEST; $request->ItemShape = $itemProperties; //If I do not include the below three sentences I get a PHP Fatal error saying "Basepoint" not found $request->IndexedPageItemView = new EWSType_IndexedPageViewType(); $request->IndexedPageItemView->BasePoint = 'Beginning'; $request->IndexedPageItemView->Offset = 0; $request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType(); $request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType(); $request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX; $request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW; $result = new EWSType_FindItemResponseMessageType(); $result = $ews->FindItem($request); if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success'){ $message = $result->ResponseMessages->FindItemResponseMessage->Items->Message; //This if loop is entered but the $message variable contains nothing print_r($message);exit; //var_dump($message) displays NULL }
The "if" loop is entered however the $message variable contains nothing. Typically it should be an object from which I can retrieve the body of the email.
What am I doing wrong ?
I tried this inside the if() statement and I got the $message object :
$message = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message; print_r($message);exit;
The "RootFolder" attribute was missing. However the resulting $message variable does not contain any "body" attribute that contains the body of the email. This is how the print_r($message) looks like :
stdClass Object
( [ItemId] => stdClass Object ( [Id] => AAMkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwBGAAAAAABI9LX/rIDdRr5XMdDM8RGZBwAL3uOTQAYYQ4U8CTQP/+wKAAAAWsnbAAAL3uOTQAYYQ4U8CTQP/+wKAAAAWvd0AAA= [ChangeKey] => CQAAABYAAAAL3uOTQAYYQ4U8CTQP/+wKAAAAWyCi )
[ParentFolderId] => stdClass Object ( [Id] => AQMkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwAuAAADSPS1/6yA3Ua+VzHQzPERmQEAC97jk0AGGEOFPAk0D//sCgAAAVrJ2wAAAA== [ChangeKey] => AQAAAA== ) [ItemClass] => IPM.Note [Subject] => Test [Sensitivity] => Normal [DateTimeReceived] => 2012-09-05T13:03:08Z [Size] => 4754 [Importance] => Normal [IsSubmitted] => [IsDraft] => [IsFromMe] => [IsResend] => [IsUnmodified] => 1 [DateTimeSent] => 2012-09-05T13:03:00Z [DateTimeCreated] => 2012-09-05T13:03:08Z [DisplayCc] => [DisplayTo] => sara cooper [HasAttachments] => [Culture] => en-IN [EffectiveRights] => stdClass Object ( [CreateAssociated] => [CreateContents] => [CreateHierarchy] => [Delete] => 1 [Modify] => 1 [Read] => 1 ) [LastModifiedName] => Lin T [LastModifiedTime] => 2012-09-05T13:03:08Z [IsAssociated] => [WebClientReadFormQueryString] => ?ae=Item&a=Open&t=IPM.Note&id=RgAAAABI9LX%2frIDdRr5XMdDM8RGZBwAL3uOTQAYYQ4U8CTQP%2f%2bwKAAAAWsnbAAAL3uOTQAYYQ4U8CTQP%2f%2bwKAAAAWvd0AAAJ&exvsurl=1 [ConversationId] => stdClass Object ( [Id] => AAQkADZjOTZhNjNkLTBmZTAtNDdkZi04NGI5LTdiZWY0ZWYzOGNhMwAQAJ56TrE0QUIAk59ALIqWrrY= ) [Sender] => stdClass Object ( [Mailbox] => stdClass Object ( [Name] => Lin T [MailboxType] => OneOff ) ) [IsReadReceiptRequested] => [ConversationIndex] => Í‹fÃIžzN±4AB"Ÿ@,Š–®¶ [ConversationTopic] => Test [From] => stdClass Object ( [Mailbox] => stdClass Object ( [Name] => Lin T [MailboxType] => OneOff ) ) [InternetMessageId] => <[email protected]> [IsRead] => [ReceivedBy] => stdClass Object ( [Mailbox] => stdClass Object ( [Name] => sara cooper [MailboxType] => OneOff ) ) [ReceivedRepresenting] => stdClass Object ( [Mailbox] => stdClass Object ( [Name] => sara cooper [MailboxType] => OneOff ) )
)
I also need the EMAIL ADDRESS of the sender and the receiver. Here only the first name and last names are displayed. Please help this is getting really urgent.
解决方案I finally got it working! Here is how you retrieve unread emails from a Exchange 2010 Server:
$ews = new ExchangeWebServices("hostname", "username", "password",ExchangeWebServices::VERSION_2010); $request = new EWSType_FindItemType(); $itemProperties = new EWSType_ItemResponseShapeType(); $itemProperties->BaseShape = EWSType_DefaultShapeNamesType::ID_ONLY; $itemProperties->BodyType = EWSType_BodyTypeResponseType::BEST; $request->ItemShape = $itemProperties; $fieldType = new EWSType_PathToUnindexedFieldType(); $fieldType->FieldURI = 'message:IsRead'; $constant = new EWSType_FieldURIOrConstantType(); $constant->Constant = new EWSType_ConstantValueType(); $constant->Constant->Value = "0"; $IsEqTo = new EWSType_IsEqualToType(); $IsEqTo->FieldURIOrConstant = $constant; $IsEqTo->Path = $fieldType; $request->Restriction = new EWSType_RestrictionType(); $request->Restriction->IsEqualTo = new EWSType_IsEqualToType(); $request->Restriction->IsEqualTo->FieldURI = $fieldType; $request->Restriction->IsEqualTo->FieldURIOrConstant = $constant; $request->IndexedPageItemView = new EWSType_IndexedPageViewType(); $request->IndexedPageItemView->BasePoint = 'Beginning'; $request->IndexedPageItemView->Offset = 0; $request->ParentFolderIds = new EWSType_NonEmptyArrayOfBaseFolderIdsType(); $request->ParentFolderIds->DistinguishedFolderId = new EWSType_DistinguishedFolderIdType(); $request->ParentFolderIds->DistinguishedFolderId->Id = EWSType_DistinguishedFolderIdNameType::INBOX; $request->Traversal = EWSType_ItemQueryTraversalType::SHALLOW; $result = new EWSType_FindItemResponseMessageType(); $result = $ews->FindItem($request); /*echo '<pre>'; print_r($result);exit;*/ if ($result->ResponseMessages->FindItemResponseMessage->ResponseCode == 'NoError' && $result->ResponseMessages->FindItemResponseMessage->ResponseClass == 'Success'){ $count = $result->ResponseMessages->FindItemResponseMessage->RootFolder->TotalItemsInView; for ($i = 0; $i < $count; $i++){ $message_id = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message[$i]->ItemId->Id; $request = new EWSType_GetItemType(); $request->ItemShape = new EWSType_ItemResponseShapeType(); $request->ItemShape->BaseShape = EWSType_DefaultShapeNamesType::ALL_PROPERTIES; $request->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType(); $request->ItemIds->ItemId = new EWSType_ItemIdType(); $request->ItemIds->ItemId->Id = $message_id; $response = $ews->GetItem($request); //print_r($response);exit; if( $response->ResponseMessages->GetItemResponseMessage->ResponseCode == 'NoError' && $response->ResponseMessages->GetItemResponseMessage->ResponseClass == 'Success' ) { $message = $response->ResponseMessages->GetItemResponseMessage->Items->Message; //process the message data. } } }
这篇关于PHP和Exchange Web服务:使用php-ews获取邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!