imap_open解析并插入数据库

时间:2015-12-02 17:32:40

标签: php imap imap-open

我正在尝试获取电子邮件,解析它们并将它们插入我的数据库。我在解析时非常接近,但插入部分进展不顺利。

$headerost = 'imap.gmail.com';
$user = 'email@gmail.com '; 
$pass = 'password';
$port = 993;
$protocol = "/imap/ssl";
$connect = imap_open("{{$headerost}:{$port}{$protocol}}INBOX", $user, $pass);

if (!$connect) die("Failed: $connect"); 

$count = imap_num_msg($connect); 

for ($i = 1; $i <= $count; $i++) {
    $header = imap_header($connect, $i);
    $to = $header->to[0];
    $from = $header->from[0];
    $date = $header->date;      
    $date1 = date_create($date);
    $date2 = date_format($date1, 'l - F jS, Y - g:i A');
    $mailbox = quoted_printable_decode($from->mailbox);
    $headerost = $from->host;
    $email = "$mailbox@$headerost";
    $mailbox1 = $to->mailbox;
    $headerost1 = $to->host;
    $email1 = "$mailbox1@$headerost1";
    $letter = imap_fetch_overview($connect, $i);
    $subj = imap_utf8( $letter[0]->subject);        
    $message = quoted_printable_decode(imap_fetchbody($connect, $i,1)); 
    $query2 = "
        SELECT * 
          FROM email
         WHERE `email` = '$email' 
           AND 'date' = '$date2';
    ";
    $result2 = mysql_query($query2);
    $num = mysql_num_rows($result2);
    $coming = "Client";

    if ($num == 0) {
        mysql_query ("
            INSERT INTO email (
                   coming,
                   email,
                   date,
                   actualdate,
                   subject,
                   message
            ) 
            VALUES (
                  '$coming', 
                  '$email',
                  '$date2',
                  '$date2',
                  '$subj',
                  '$message'
            )
       "); 
    }
}

$ message有时会显示整条消息,但有时它也会显示回复,有时它只显示乱码。实际日期是MYSQL中的日期格式,其输入为00-00-0000。电子邮件输入为0. $ date2,$ subj和$ coming正确输入。

0 个答案:

没有答案