php - 使用 PHP 管理 GMail 邮件过滤器 XML 文件

标签 php xml gmail simplexml

由于 GMail 可以导入和导出邮件过滤器,因此我想使用 PHP 脚本管理从 GMail 导出的 XML 文件,因为有一些 known issues搜索过滤器中的字符数。

我在 PHP 中找到了 simplexml_load_file 函数,并对执行的导出执行了 var_dump(),但我现在似乎无法访问生成的 XML 文件中的应用程序命名空间。

按照 PHP 手册中的各个页面,我创建了这个非常简单的脚本来读取 XML,以便我可以开始处理我已经创建的过滤器。不幸的是,它似乎缺少关键部分!

<pre><?php

if(file_exists("mailfilters.xml")) {
  $xml = simplexml_load_file("mailfilters.xml");
  $namespaces = $xml->getNamespaces(true);
  foreach ($namespaces as $prefix => $ns) {
      $xml->registerXPathNamespace($prefix, $ns);
  }
  var_dump($xml);
} else {
  die("Failed to open filter file");
}

?></pre>

这将返回此数据(摘录)

["entry"]=>
array(268) {
  [0]=>
  object(SimpleXMLElement)#3 (5) {
    ["category"]=>
    object(SimpleXMLElement)#271 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991916868"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#272 (0) {
    }
  }
  [1]=>
  object(SimpleXMLElement)#4 (5) {
    ["category"]=>
    object(SimpleXMLElement)#272 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991925003"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#271 (0) {
    }
  }

这是我今天下载的 XML 文件的摘录,用于创建上述输出:

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
    <title>Mail Filters</title>
<id>tag:mail.google.com,2008:filters:1284991916868,...,1287734777820</id>
<updated>2010-10-28T11:59:31Z</updated>
<author>
    <name>My Name</name>
    <email><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="274a5e67424a464e4b0946434355094254" rel="noreferrer noopener nofollow">[email protected]</a></email>
</author>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284991916868</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='from' value='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12737c52777f737b7e3c737676603c7761" rel="noreferrer noopener nofollow">[email protected]</a>'/>
    <apps:property name='shouldArchive' value='true'/>
    <apps:property name='shouldTrash' value='true'/>
</entry>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284993579743</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='subject' value='Some Relevant Subject'/>
    <apps:property name='label' value='MyCoolLabel'/>
    <apps:property name='shouldArchive' value='true'/>
</entry>

其他“apps:property”动词包括:

<apps:property name='hasTheWord' value=''/>
<apps:property name='shouldAlwaysMarkAsImportant' value=''/>
<apps:property name='doesNotHaveTheWord' value=''/>

最佳答案

我最近遇到了类似的“问题”。我试图从 Google Apps 获取电子邮件签名设置。 Google 的 XML 响应如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<id>https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/signature</id>
<updated>2012-08-31T15:22:03.067Z</updated>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483b212f26293c3d3a2d773027293d3c20173a2d393d2d3b3c273a17212c753d3b2d3a082c2725292126662b2725" rel="noreferrer noopener nofollow">[email protected]</a>'/>
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f1c0608010e1b1a1d0a5017000e1a1b07301d0a1e1a0a1c1b001d30060b521a1c0a1d2f0b00020e0601410c0002" rel="noreferrer noopener nofollow">[email protected]</a>'/>
<apps:property name='signature' value='Here goes the email signature...'/>
</entry>

我无法使用 SimpleXMLElement 从 apps:property 中获取属性。这就是我最终解决它的方法:

    // create SimpleXMLElement with XML input as string
    $xml = new SimpleXMLElement($feed);

    // get namespace from XML
    $namespaces = $xml->getNamespaces(true);

    // get children (using namespace) and attributes
    $appsProperty = $xml->children($namespaces['apps'])->attributes();

    // attributes are now stored in array $appsProperty and can be accessed
    echo "Name: ".$appsProperty['name'];
    echo "Signature: ".$appsProperty['value'];

关于php - 使用 PHP 管理 GMail 邮件过滤器 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4043183/

相关文章:

php - 从表中选择所有属性和某些属性在 codeigniter 中得到不同的结果

javascript - 使用each()打印数组时jquery ajax()问题

xml - 任何想法为什么图案化的 SVG 文件在浏览器中显示为空白?

javascript - (ZEND) 在 javascript 中路由到 index.phtml

php - 这是 PDO 'good code' 的包装器吗?有没有潜在的问题?

objective-c - 文档不受源代码控制,nib 显示为 xml 文本

javascript - 异步 xml 请求不返回

authentication - 如何在我的网站上实现 ‘sign in with google’?

java - 使用 SSL 连接通过 Gmail SMTP 服务器发送电子邮件通知

smtp - 无法连接到 gmail 的 smtp 服务器。为什么?