如何从未读的答案打印邮件正文和主题? [英] How to to print mail body and subject from unread answer?

查看:176
本文介绍了如何从未读的答案打印邮件正文和主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要收到邮件主题和正文,只收到我收件箱的未读邮件。


  1. 我想一次读取一个未读邮件,然后将其标记为读取

  2. 我需要来自地址和邮件正文主题

以下代码显示了所有未读邮件的邮件ID。

  require_once('../mail3/php-ews-master/ExchangeWebServices.php'); 
require_once('../mail3/php-ews-master/EWS_Exception.php');
require_once('../mail3/php-ews-master/EWSType.php');
require_once('../mail3/php-ews-master/NTLMSoapClient.php');

函数__autoload($ class_name)
{
//从基本路径开始并从类名称确定位置
$ base_path ='../ mail3 / PHP-EWS-主';
$ include_file = $ base_path。 '/'。 str_replace('_','/',$ class_name)。 .PHP; ($ include_file)?
}

$ ews =新的ExchangeWebServices(servername,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->限制=新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);

获取后如何将邮件标记为已读?

  stdClass对象

[ResponseMessages] => stdClass对象

[FindItemResponseMessage] => stdClass对象

[ResponseCode] => NoError
[ResponseClass] => Success
[RootFolder] => stdClass对象

[项目] => stdClass对象

[Message] => Array

[0] => stdClass对象

[项目Id] => stdClass的对象

[ID] => AAMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAAAAADANtAZyWYTTKe / PT + BZ + SXBwD + fIgCJQITS5O3LAEwY6 + oAAAANbjBAAB51OTN2pqDQbTnOkGjBC0FAAGN2YkTAAA =
[ChangeKey] => CQAAABYAAAD + fIgCJQITS5O3LAEwY6 + oAAC4WS4O




[1] => stdClass的对象

[项目Id] => stdClass的对象

[ID] => AAMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAAAAADANtAZyWYTTKe / PT + BZ + SXBwD + fIgCJQITS5O3LAEwY6 + oAAAANbjBAAB51OTN2pqDQbTnOkGjBC0FAAGN2YkSAAA =
[ChangeKey] => CQAAABYAAAD + fIgCJQITS5O3LAEwY6 + oAAC4WS35




[IndexedPagingOffset] => 2
[IncludesLastItemInRange] => 1
[TotalItemsInView] => 2





解决方案

我看到你正在使用 jamesiarmes / php-ews 版本的 php-ews ,所以我会尽量回答。我可能会有点偏离,因为我不使用这个版本,首先会鼓励你升级到我维护和更新的一个fork,因为它更容易使用,你会得到更多的支持,它是PSR-2和4兼容并且仍然保持。这被称为 garethp / php-ews 。我会给出我的答案,因为它很简单,然后移动到您使用的代码库



使用garethp / php-ews解决



本质上有三个部分。读取阅读和阅读。第一个是从服务器获取未读的电子邮件,这样做就是这样完成的。

  require_oncevendor / autoload.php; 

使用jamesiarmes\PEWS\API\Type;
使用jamesiarmes\PEWS\Mail\MailAPI;

$ api = MailAPI :: withUsernameAndPassword('server','username','password');

$ unreadMail = $ api-> getUnreadMailItems();

解决方案的第二部分是读取项目。当您获取其列表时,EWS不返回邮件项目的正文。它认为身体是一个二等级的财产,所以你需要具体要求一个邮件项的信息来获得身体。所以,为了做到这一点,我们做以下

  $ item = $ unreadMail [0]; 
$ item = $ api-> getItem($ item-> getItemId());

$ subject = $ item-> getSubject();
$ sender = $ item-> getSender() - > getMailbox() - > getEmailAddress();
$ body =(string)$ item-> getBody();

最后一部分是将项目标记为已读,这样做就是这样。

  $ API-> markMailAsRead($本期特价货品> getItemId()); 

邮件项现在应该显示为已读。所以,如果我们把它们全部放在一起,它应该是这样的:

  require_oncevendor / autoload.php; 

使用jamesiarmes\PEWS\API\Type;
使用jamesiarmes\PEWS\Mail\MailAPI;

$ api = MailAPI :: withUsernameAndPassword('server','username','password');

$ unreadMail = $ api-> getUnreadMailItems();

foreach($ unreadMail as $ item){
$ item = $ api-> getItem($ item-> getItemId());

$ subject = $ item-> getSubject();
$ sender = $ item-> getSender() - > getMailbox() - > getEmailAddress();
$ body =(string)$ item-> getBody();

$ api-> markMailAsRead($ item-> getItemId());
}



使用jamesiarmes / php-ews解决



这是分为三个步骤(见上文):获取,读取和标记为已读。你知道如何获取,显然。所以我们将跳到其他两个部分。

  $ result = $ ews-> FindItem($ request) ; 

foreach($ result-> ResponseMessages-> FindItemResponseMessage-> RootFolder-> Items->消息为$ item){
$ 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();项目ID->

$ response = $ ews-> GetItem($ request);

//您可能必须在$响应这里做一个var_dump。我只是猜测这是你如何找到消息项目,因为我不再使用这个代码库了。
$ item = $ response-> ResponseMessages-> GetItemResponseMessage-> Items-> Message;

//您应该在$项上执行一个var_dump,以了解如何在此处获取正文,主题和发件人。我不是100%肯定如何做到这一点。

//将项目标记为已读(希望)
$ request = new EWSType_UpdateItemType();
$ request-> MessageDisposition ='SaveOnly';
$ request-> ConflictResolution ='AlwaysOverwrite';
$ request-> ItemChanges = [];

$ change = new EWSType_ItemChangeType();

$ change = new EWSType_ItemChangeType();
$ change-> ItemId = new EWSType_ItemIdType();
$ change-> ItemId-> Id = $ item-> ItemId-> Id;
$ change-> ItemId-> ChangeKey = $ item-> ItemId-> ChangeKey;
$ change-> Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();
$ change->更新 - > SetItemField = array(); //要更新的字段数组

//更新Firstname(简单属性)
$ field = new EWSType_SetItemFieldType();
$ field-> FieldURI-> FieldURI ='message:IsRead';
$ field-> Message = new EWSType_MessageItemType();
$ field-> Message-> IsRead = true;

$ change-> Updates-> SetItemField [] = $ field;

//设置所有更改
$ request-> ItemChanges [] = $ change;

//发送请求
$ response = $ ews-> UpdateItem($ request);
echo'< pre>'。print_r($ response,true)。'< / pre>';
}

没有实际测试,那应该大致如何做。你可能需要玩弄才能使其发挥作用。你可以看到为什么我建议用这个叉子。


I want to get the mail subject and body of only unread mails of my inbox.

  1. I want to read one unread mail at a time and mark it as read afterwards.
  2. I need the subject, from address and mail body.

The below code shows gives me the mail IDs of all unread mails.

require_once ('../mail3/php-ews-master/ExchangeWebServices.php');
require_once ('../mail3/php-ews-master/EWS_Exception.php');
require_once ('../mail3/php-ews-master/EWSType.php');
require_once ('../mail3/php-ews-master/NTLMSoapClient.php');

function __autoload($class_name)
{
// Start from the base path and determine the location from the class name,
$base_path = '../mail3/php-ews-master';
$include_file = $base_path . '/' . str_replace('_', '/', $class_name) . '.php';

return (file_exists($include_file) ? require_once $include_file : false);
}

$ews = new ExchangeWebServices("servername", "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);

After getting it how can I mark the mail as read?

  stdClass Object
  (
  [ResponseMessages] => stdClass Object
   (
    [FindItemResponseMessage] => stdClass Object
    (
      [ResponseCode] => NoError
      [ResponseClass] => Success
      [RootFolder] => stdClass Object
         (
           [Items] => stdClass Object
             (
               [Message] => Array
                 (
                   [0] => stdClass Object
                     (
                       [ItemId] => stdClass Object
                         (
                           [Id] => AAMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAAAAADANtAZyWYTTKe/pt+BZ+SXBwD+fIgCJQITS5O3LAEwY6+oAAAANbjBAAB51OTN2pqDQbTnOkGjBC0FAAGN2YkTAAA=
                           [ChangeKey] => CQAAABYAAAD+fIgCJQITS5O3LAEwY6+oAAC4WS4O
                         )

                     )

                   [1] => stdClass Object
                     (
                       [ItemId] => stdClass Object
                         (
                           [Id] => AAMkADM1NjQ4ZjU0LWI3OWYtNGZiMC1iYTgzLTU4N2E1MGMwYWNkMQBGAAAAAADANtAZyWYTTKe/pt+BZ+SXBwD+fIgCJQITS5O3LAEwY6+oAAAANbjBAAB51OTN2pqDQbTnOkGjBC0FAAGN2YkSAAA=
                           [ChangeKey] => CQAAABYAAAD+fIgCJQITS5O3LAEwY6+oAAC4WS35
                         )
                     )
                 )
             )
           [IndexedPagingOffset] => 2
           [IncludesLastItemInRange] => 1
           [TotalItemsInView] => 2
         )
      )
   )
 )

解决方案

I see you're using the jamesiarmes/php-ews version of php-ews, so I'll try to answer for that. I might be a little bit off, since I don't use that version and will first encourage you to upgrade to a fork I maintain and update, because it's easier to use, you'll get more support, it's PSR-2 and 4 compatible and is still maintained. It's called garethp/php-ews. I'll give my answer for that first, as it's short and easy, then move to the code base you use

Solving with garethp/php-ews

Essentially there's three parts to it. Fetching, reading and marking as read. The first is to get only unread emails from the server, which is done as such

require_once "vendor/autoload.php";

use jamesiarmes\PEWS\API\Type;
use jamesiarmes\PEWS\Mail\MailAPI;

$api = MailAPI::withUsernameAndPassword('server', 'username', 'password');

$unreadMail = $api->getUnreadMailItems();

The second part of the solution is to read the item. EWS doesn't return the body of a mail item when you fetch a list of them. It considers the body to be a second class property, so you need to specifically ask for the information of that one mail item to get the body. So, in order to do that, we do the following

$item = $unreadMail[0];
$item = $api->getItem($item->getItemId());

$subject = $item->getSubject();
$sender = $item->getSender()->getMailbox()->getEmailAddress();
$body = (string) $item->getBody();

And the last part is to mark an item as read, which is done as such.

$api->markMailAsRead($item->getItemId());

The mail item should now show up as read. So, if we put them all together, it should come something like

require_once "vendor/autoload.php";

use jamesiarmes\PEWS\API\Type;
use jamesiarmes\PEWS\Mail\MailAPI;

$api = MailAPI::withUsernameAndPassword('server', 'username', 'password');

$unreadMail = $api->getUnreadMailItems();

foreach ($unreadMail as $item) {
    $item = $api->getItem($item->getItemId());

    $subject = $item->getSubject();
    $sender = $item->getSender()->getMailbox()->getEmailAddress();
    $body = (string) $item->getBody();

    $api->markMailAsRead($item->getItemId());
}

Solving with jamesiarmes/php-ews

This is split up in to three steps (see above): Fetching, reading and marking as read. You know how to fetch, obviously. So we'll skip to the other two parts in one.

$result = $ews->FindItem($request);

foreach ($result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->Message as $item) {
    $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 = $item->ItemId->Id; 

    $response = $ews->GetItem($request);

    //You may have to do a var_dump on the $response here. I'm only guessing that this is how you locate the message item, since I don't use this code base any more.
    $item = $response->ResponseMessages->GetItemResponseMessage->Items->Message;

    //You should do a var_dump on the $item to see how to get the body, subject and sender here. I'm not 100% sure how to do it on this one.

    //Mark the item as read (hopefully)
    $request = new EWSType_UpdateItemType();
    $request->MessageDisposition = 'SaveOnly';
    $request->ConflictResolution = 'AlwaysOverwrite';
    $request->ItemChanges = [];

    $change = new EWSType_ItemChangeType();

    $change = new EWSType_ItemChangeType();
    $change->ItemId = new EWSType_ItemIdType();
    $change->ItemId->Id = $item->ItemId->Id;
    $change->ItemId->ChangeKey = $item->ItemId->ChangeKey;
    $change->Updates = new EWSType_NonEmptyArrayOfItemChangeDescriptionsType();
    $change->Updates->SetItemField = array(); // Array of fields to be update

    // Update Firstname (simple property)
    $field = new EWSType_SetItemFieldType();
    $field->FieldURI->FieldURI = 'message:IsRead';
    $field->Message = new EWSType_MessageItemType();
    $field->Message->IsRead = true;

    $change->Updates->SetItemField[] = $field;

    // Set all changes
    $request->ItemChanges[] = $change;

    // Send request
    $response = $ews->UpdateItem($request);
    echo '<pre>'.print_r($response, true).'</pre>';
}

Without actually testing it, that should be roughly how you would do it. You may have to play around with that to make it work. You can see why I suggest using my fork over this.

这篇关于如何从未读的答案打印邮件正文和主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆