开发者

How to retrieve and process Gmail data

开发者 https://www.devze.com 2023-01-25 15:49 出处:网络
I am doing a project that involves processing data from my Gmail history. Specifically, I want to generate a multi-page styled PDF that has a custom page for each of 100 or so people - showing data s

I am doing a project that involves processing data from my Gmail history.

Specifically, I want to generate a multi-page styled PDF that has a custom page for each of 100 or so people - showing data such as number of e-mails sent in the past year, number of e-mails received in the past year, average word length of e-mail, most used terms in e-mail, date of oldest e-mail sent or received, maybe even average number of exclamation points or expletives per e-mail, etc开发者_Python百科.

I saw this question, which had a helpful link to IMAP functions in the PHP manual, but can someone help me out with what the architecture and difficulty of such a project would be?

I am envisioning:

  1. write a php script to run some IMAP functions on my Gmail data and write it to a MySQL database.
  2. write another script to run a loop of MySQL queries on the database and print to a PDF based on the results


First of all, you need php imap library. Then, just use this simple step-by-step tutorial:

$email    = "email@gmail.com";//or alamatemail@nama_domain_hosted
$password = "ini password anda";
 
$imap_host = "{imap.gmail.com:993/imap/ssl}";
 
$imap_folder = "INBOX"; //it's what is called label in Gmail
 
$mailbox = imap_open($imap_host . $imap_folder,$email,$password) or die('Failed to open connection with Gmail: ' . imap_last_error());

With code above, you've already created connection to Gmail.
Now, if you want to search particular message, use this:

$emails = imap_search( $mailbox, 'ALL');

Read RFC 1176 for more detailed options. Search for string "tag SEARCH search_criteria" or read on PHP's imap_search documentation.

This code will process retrieved messages (you can then process it to MySQL as you please):

if( $emails )
{
    foreach( $emails as $email_id)
    {
        $email_info = imap_fetch_overview($mailbox,$email_id,0);
        $message = imap_fetchbody($mailbox,$email_id,2);
        echo "Subject: " . $email_info[0]->subject . "\n";
        echo "Message: " . $message . "\n";
    }
}

Answering your additional question:

  1. It's possible to process email on your local server or even from your own laptop / desktop. It work just the way desktop email client work.
  2. It's not that hard once you grasp the basic flow.
0

精彩评论

暂无评论...
验证码 换一张
取 消