2014-01-15 89 views
-2

我有1個客戶端需要PHP中的自動響應腳本,但不僅僅是在表單中,即使他在他的收件箱中直接發送電子郵件,腳本也應該可以工作。如何跟蹤用戶的電子郵件和運行腳本

我只是困惑,這是可能的PHP或不可能,如果它的可能那麼我們如何追蹤他的電子郵件?

+0

聽起來似乎應設置郵件服務器上 – 2014-01-15 19:46:20

+0

肯定的,但客戶希望腳本只 –

+0

什麼是「腳本只」在這方面是什麼意思? – 2014-01-15 19:48:12

回答

0
<?php 
date_default_timezone_set('America/Los_Angeles'); 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = ''; 
$password = ''; 

function getUnreadMail($host, $login, $passwd) { 
    $result = array(); 
    $mbox = imap_open($host, $login, $passwd); 
    $emailIds = imap_search($mbox,'UNSEEN'); 
    foreach ($emailIds as $id) { 
     $header = imap_headerinfo($mbox, $id); 
     $email = $header->from[0]->mailbox .'@'.$header->from[0]->host; 
     $result[] = $email; 
     sendEmail($mbox, $email, $login, $cc="", $bcc=""); 
    } 
    return $result; 
} 

function sendEmail ($mbox, $to, $return_path, $cc="", $bcc="") { 
    $subject = "Test Email"; 
    $body = "This is only a test."; 
    $headers = "From: ".$return_path."\r\n". 
       "Reply-To: ".$return_path."\r\n"; 
    $a = imap_mail($to, $subject, $body, $headers, $cc, $bcc, $return_path); 
    imap_append($mbox, "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail", 
    "From: ".$return_path."\r\n". 
    "To: ".$to."\r\n". 
    "Subject: ".$subject."\r\n". 
    "Date: ".date("r", strtotime("now"))."\r\n". 
    "\r\n". 
    $body. 
    "\r\n" 
    ); 
    echo "Message saved to Send folder!<br />"; 
} 

$count = getUnreadMail($hostname, $username, $password); 
echo '<pre>'; 
print_r($count);