【问题标题】:issues with IMAP functions phpIMAP 函数 php 的问题
【发布时间】:2018-06-04 02:08:28
【问题描述】:

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

$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 将进入下一行)。另一个问题是,有时获取的正文不是当前邮件包含的正文。它获取了一些其他邮件内容。

我不知道该怎么办,所以把我的整个代码,请帮忙。

【问题讨论】:

    标签: php imap


    【解决方案1】:

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

    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/>";
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2020-10-25
      • 2011-09-26
      相关资源
      最近更新 更多