简体   繁体   English

IMAP函数php的问题

[英]issues with IMAP functions php

I am facing some problem with imap function. 我在使用imap函数时遇到了一些问题。 Basically What I need to do is to read unseen mails. 基本上,我需要做的是阅读看不见的邮件。 There will be a url in all the mails, i should fetch that URL and store. 所有邮件中都会有一个网址,我应该获取该网址并进行存储。

$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

I have used all different varients of imap to make sure it will read different types of mails. 我已经使用了imap的所有不同变体来确保它将读取不同类型的邮件。 Now issue is, sometime the URL matched is broken. 现在的问题是,有时匹配的URL损坏了。 Only half URL will be fetched(I printed the entire message, saw that half URL is coming to next line). 只有一半的URL将被提取(我打印了整个消息,看到一半的URL进入下一行)。 The other issue is, sometimes, the body fetched will not be the one which the current mail contains. 另一个问题是,有时提取的正文将不是当前邮件所包含的正文。 It fetched some other mail content. 它获取了其他一些邮件内容。

I am puzzled what to do, so putting my entire code, please help. 我不知道该怎么办,因此请输入我的完整代码。

You will have to use a regex modifiers to match multiline texts too, or you can strip newlines and such from the body of the emails. 您还必须使用正则表达式修饰符来匹配多行文本,或者可以从电子邮件正文中删除换行符等。

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

Your second issue is a bit different than you'd think, emails have multiple bodies, one for simple texts, one for html, some for attachments (and their order is different in apple products). 您的第二个问题与您想象的有些不同,电子邮件具有多个正文,一个正文用于简单的文本,一个正文用于html,一些附件(在Apple产品中,它们的顺序不同)。 https://www.electrictoolbox.com/php-imap-message-parts/ https://www.electrictoolbox.com/php-imap-message-parts/

You are probably facing the issue of grabbing the wrong one. 您可能正面临抓住错误的问题。 My recommendation would be to fetch all of the email bodies, like this: 我的建议是获取所有电子邮件正文,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM