如何跟踪用户的电子邮件和运行脚本

时间:2014-01-15 19:41:54

标签: php email

我有一个客户需要PHP中的自动回复脚本,但不仅仅是在表单中,即使他在收件箱中收到直接电子邮件,那么脚本应该可以工作。

我很困惑,如果有可能,我们可以在PHP中跟踪他的电子邮件吗?

1 个答案:

答案 0 :(得分: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);