2

INBOXgmailで自分のメールを取得しています。自分が付与したIDのみのメールを取得したい。これを実装する方法がわかりません。

でやろうとしてimap_searchも上手くいかなくて…

$emails = imap_search($inbox,'rajeev@gmail.com');

しかし、それは私に受信トレイのすべての電子メールをくれました。

私が望むことを達成するための提案はありますか?

私のコードは -

function connect_mail(){

    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = '***@gmail.com';
    $password = 'password'; 
    $inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));   
    $emails = imap_search($inbox,'UNREAD');     
    $Msgcount = count($emails); 
        for ($x = 1; $x <= $Msgcount; $x++)         
        {   
            $overview = imap_fetch_overview($inbox, $x);            
            $title = $overview[0]->subject;
                        $struct = imap_fetchstructure($inbox,$x);
            $contentParts = count($struct->parts);
            if ($contentParts >= 2) {
                for ($i=2;$i<=$contentParts;$i++) {
                    $att[$i-2] = imap_bodystruct($inbox,$x,$i);
                }
                for ($k=0;$k<sizeof($att);$k++) {
                    if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value    == "US-ASCII") {
                        if ($att[$k]->parameters[1]->value != "") {
                            $selectBoxDisplay[$k] = $att[$k]->parameters[1]->value;
                        }
                    } 
                    elseif ($att[$k]->parameters[0]->value != "iso-8859-1" &&    $att[$k]->parameters[0]->value != "ISO-8859-1") {
                        $selectBoxDisplay[$k] = $att[$k]->parameters[0]->value;
                    }
                }
            }

            if (sizeof($selectBoxDisplay) > 0) {
                echo "<select name=\"attachments\" size=\"3\" class=\"tblContent\"    onChange=\"handleFile(this.value)\" style=\"width:170;\">";
                for ($j=0;$j<sizeof($selectBoxDisplay);$j++) {
                    $filename = $selectBoxDisplay[$j];
                    echo "\n<option value=\"$j\">". $selectBoxDisplay[$j]    ."</option>";

                }
                echo "</select>";
             }
            $strFileName = $att[$file]->parameters[0]->value;
            $strFileType = strrev(substr(strrev($filename),0,4));
            $fileContent = imap_fetchbody($inbox, $x, $file+2);
            $ContentType = "application/octet-stream";

            list($file_first_name, $extension) = explode('. ', $filename);


            if ($extension == ".jpg" || $extension == "jpeg" || $extension == ".JPG")
                $ContentType = "image/jpeg";
            if ($extension == ".doc")
                $ContentType = "application/msword";
            header ("Content-Type: $ContentType"); 
            header ("Content-Disposition: attachment; filename=$strFileName; size=$fileSize;"); 

            if (substr($ContentType,0,4) == "text") {
                echo imap_qprint($fileContent);
            } 
            else {
                echo base64_decode($fileContent);
            }           
}
}
4

1 に答える 1

0

まだ未読の特定のユーザーからのメールを検索したいようです。あなたのでこれを試してくださいimap_search()

$emails = imap_search ( $mailbox, 'FROM "user@email.com" UNSEEN' );

上記のコードは、フラグuser@email.comを持つユーザーからの電子メールをループします。UNSEEN

コメントチャットに基づいて更新

あなたがする直前に:

for ($x = 1; $x <= $Msgcount; $x++)         
{
    //long process here
}

もう1つのチェックでラップします:

$emails = imap_search ( $mailbox, 'FROM "user@email.com" UNSEEN' );
if(false !== $emails){

}

最終結果は次のようになります。

$emails = imap_search ( $mailbox, 'FROM "user@email.com" UNSEEN' );
if(false !== $emails){
    for ($x = 1; $x <= $Msgcount; $x++)         
    {
        //long process here
    }
}
于 2012-06-13T12:15:56.020 に答える