簡體   English   中英

使用curl gmail未讀電子郵件計數

[英]gmail unread email count using curl

我創建了一個腳本,可以獲取未讀的電子郵件列表,因為這里的gmail是我的代碼

<?php
//function to get unread emails taking username and password as parameters
function check_email($username, $password)
{ 
    //url to connect to
    $url = "https://mail.google.com/mail/feed/atom"; 

    // sendRequest 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    $curlData = curl_exec($curl);
    curl_close($curl);
    //returning retrieved feed
    return $curlData;

}

//making page to behave like xml document to show feeds
header('Content-Type:text/xml; charset=UTF-8');
//calling function
$feed = check_email("username", "password");
echo $feed;
?>

輸出就像這樣

<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<title>Gmail - Inbox for suneth2@gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1282</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />
<modified>2012-08-01T12:33:48Z</modified>
<entry>
<title>eBCS Pro 1 August 2012</title>
<summary>bcs logo eBCS Pro 1 August 2012 Video interview Olympic IT The Met Police&#39;s director of IT, Steve</summary>
<link rel="alternate" href="http://mail.google.com/mail?account_id=test@gmail.com&amp;message_id=138e21a3404cc7b2&amp;view=conv&amp;extsrc=atom" type="text/html" />
<modified>2012-08-01T12:12:44Z</modified>
<issued>2012-08-01T12:12:44Z</issued>
<id>tag:gmail.google.com,2004:1409100718455703474</id>
<author>
<name>eBCS Newsletter</name>
<email>e-bulletin@lists.bcs.org.uk</email>
</author>
</entry>
<entry>

所以想看看

<fullcount>1282</fullcount>

標簽

當您將用戶名和密碼傳遞給此功能時,它可以顯示電子郵件列表,我只需要獲取郵件計數,是否有任何方法可以捕獲或計算項目

這就是答案,我使用php讀取xml標簽

$xmlobjc = new SimpleXMLElement($feed);

echo $xmlobjc->fullcount[0];

並用。替換標題

header('Content-Type:text/html; charset=UTF-8');

這也有效:

curl -u $(cat username):$(cat password) --silent 'https://mail.google.com/mail/feed/atom' | sed -n 's:.*<fullcount>\(.*\)</fullcount>.*:\1:p'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM