IMAP功能问题php

时间:2017-12-21 11:43:19

标签: php imap

我遇到了imap功能的一些问题。基本上我需要做的是阅读看不见的邮件。所有邮件都会有一个网址,我应该获取该网址并存储。

$inbox = imap_open($hostname,$username,$password);
if($inbox)//if **1
{
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');

/* if emails are returned, cycle through each... */
if($emails) //if **2
{
    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    $varients=array("1","1.1","1.2","2");

    foreach($emails as $email_number) //for loop **1
    {
        $ids [] = $email_number;

        foreach($varients as $cur_varient) //for loop **2
        {
            echo "\n\nstarting with imap function number ". $cur_varient."\n\n";

            $overview     = imap_fetch_overview($inbox,$email_number,0);//all varients of like subject, date etc.
            $from         = addslashes(trim($overview[0]->from));
            $inboxed_time = addslashes(trim(strtotime($overview[0]->date)));
            $message      = (imap_body($inbox,$email_number,$cur_varient));

            print addslashes(trim($overview[0]->subject));break;
            preg_match_all('#\bhttp?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);

            $link_matched = $match[0];                  
            $input        = 'unsubscribe.php';
            $linkexists   = false;

            foreach($link_matched as $curlink) 
            {                   
                if(stripos($curlink, $input) !== false) 
                {
                $linkexists = true;
                $unsublink  = $curlink;

                $unsublink  = str_replace('href="', '', $unsublink);
                $unsublink  = str_replace('"', '', $unsublink);

                break;
                }
            }



            if(isset($unsublink))
            {   
                $unsublink = addslashes(trim(($unsublink)));
                $thread    = 1;
                $time      = date("Y-m-d H:i:s");
                $iQry      = " INSERT INTO `SPAMS`.url_queue VALUES(";
                    $iQry     .= " 'default','".$unsublink."','".$thread."','";
                $iQry     .= "".$from."','".$inboxed_time."',UNIX_TIMESTAMP('".$time."'))";

                //mysql_query($iQry);
                print $iQry;
            }
        }//closing for loop **2

    }//closing for loop **1

} //closing if **2

// Setting flag from un-seen email to seen on emails ID.
if(isset($ids))
{
    imap_setflag_full($inbox,implode(",", $ids), "\\Seen \\Flagged"); //IMPORTANT
}

// colse the connection
imap_expunge($inbox);
imap_close($inbox);
}//closing if **1

我使用了所有不同的imap变量来确保它会读取不同类型的邮件。现在问题是,有时URL匹配被破坏了。将只提取一半的URL(我打印了整个消息,看到半个URL到达下一行)。另一个问题是,有时,获取的正文将不是当前邮件包含的正文。它获取了一些其他邮件内容。

我很困惑该怎么做,所以把我的整个代码,请帮忙。

1 个答案:

答案 0 :(得分:0)

您还必须使用正则表达式修饰符来匹配多行文本,或者您可以从电子邮件正文中删除换行符等。

preg_match("/{pattern}/mi",'Testing'); 
//m for multiline matches and i for case-insensitive matches

你的第二个问题与你想的有点不同,电子邮件有多个主体,一个用于简单文本,一个用于html,一些用于附件(并且它们的顺序在苹果产品中不同)。 https://www.electrictoolbox.com/php-imap-message-parts/

你可能面临抓错的问题。我的建议是获取所有的电子邮件正文,如下所示:

$overview = imap_fetch_overview($this->connection, $email_number, 0);
$structure = imap_fetchstructure($this->connection, $email_number);
$message = "";
//$parts = [1, 1.1, 1.2, 2];
if (!$structure->parts)//simple email
    $message .= "Simple: ". imap_fetchbody($this->connection, $email_number, 0). "<br/>";
else {
    foreach ($structure->parts as $partNumber=>$part){
            if ($partNumber != 0)
                $message .= "Part ".$partNumber.": ". imap_fetchbody($this->connection, $email_number, $partNumber)."<br/>";
        }
}