为什么无法将我的gmail标头信息主题插入sqlite3数据库? [英] Why can't insert my subject of gmail header info into a sqlite3 database?

查看:123
本文介绍了为什么无法将我的gmail标头信息主题插入sqlite3数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码已经过测试,可以将所有message_id,uid,subject gmail标头信息插入到我的sqlite3数据库中.

The code have been tested that all the message_id,uid,subject gmail header info can be inserted into my sqlite3 database.

<?php
    $db='/home/email.db';

   // function get_gmail  can get all the message_id,uid,subject gmail header info.
   function get_gmail(){
        $email_data=array();
        $hostname = '{imap.gmail.com:993/imap/ssl}';
        $username = 'xxxx@gmail.com';
        $password = 'yyyy';
        $inbox = imap_open($hostname,$username,$password);
        $nums=imap_num_msg($inbox);
        for ($i=1;$i<=$nums;$i++){
            $overview = imap_fetch_overview($inbox, $i, 0);
            $x1 = $overview[0]->message_id;
            $x2 = $overview[0]->uid;
            $x3 = $overview[0]->subject;
            $email_data[]=array($x1,$x2,$x3);
            }
        imap_close($inbox);
        return  $email_data;
        }

    //function insert_data can insert all the data into my sqlite3 database.
    function insert_data($array){
        Global $db;
        $dbh=new PDO("sqlite:{$db}");
        $dbh->beginTransaction();
        $sql = "INSERT INTO gmail(message_id,uid,subject) VALUES (?,?,?)";
        $query = $dbh->prepare($sql);
        foreach($array as $item){
            $query->execute($item);
            }
        $dbh->commit();
        $dbh->beginTransaction();
        $dbh=null;
    }

    $data=get_gmail();
    insert_data($data);
?>

问题仍然存在,例如,电子邮件的主题为'=?GB2312?B?zbO8xtGnu/m0ocq10bXP7sS/?=',它已作为以下形式插入到sqlite3中 '=?GB2312?B?zbO8xtGnu/m0ocq10bXP7sS/?=',我使用以下代码将其以utf-8的形式更改为中文字符.

A problem remains ,for example a email's subject is '=?GB2312?B?zbO8xtGnu/m0ocq10bXP7sS/?=' ,it was inserted into sqlite3 as the form '=?GB2312?B?zbO8xtGnu/m0ocq10bXP7sS/?=' ,i changed it into chinese characters in the form of utf-8 with the following code.

<?php
    $db='/home/email.db';

    function get_gmail(){
        mb_internal_encoding('UTF-8'); 
        $email_data=array();
        $hostname = '{imap.gmail.com:993/imap/ssl}';
        $username = 'xxxx@gmail.com';
        $password = 'yyyy';
        $inbox = imap_open($hostname,$username,$password);
        $nums=imap_num_msg($inbox);
        for ($i=1;$i<=$nums;$i++){
            $overview = imap_fetch_overview($inbox, $i, 0);
            $x1 = $overview[0]->message_id;
            $x2 = $overview[0]->uid;
            $x3 = $overview[0]->subject;
            $x3 = mb_decode_mimeheader($x3);
            $email_data[]=array($x1,$x2,$x3);
            }
        imap_close($inbox);
        return  $email_data;
        }


    function insert_data($array){
        Global $db;
        $dbh=new PDO("sqlite:{$db}");
        $dbh->beginTransaction();
        $sql = "INSERT INTO gmail(message_id,uid,subject) VALUES (?,?,?)";
        $query = $dbh->prepare($sql);
        foreach($array as $item){
            $query->execute($item);
            }
        $dbh->commit();
        $dbh->beginTransaction();
        $dbh=null;
    }

    $re=get_gmail();
    insert_data($re);
?>

有两个问题需要解决.

1.mb_decode_mimeheader
并非所有MIME RFC 2047格式的主题都需要更改,以=?GB2312开头的字符串将被更改.
如何添加if-else结构来完成这项工作?

1.mb_decode_mimeheader
Not all the subject in MIME RFC 2047 format need to be changed , strings begins with =?GB2312 will be changed.
How to add a if-else structure to do the job?

2.无法运行insert_data函数.
PHP致命错误:在第49行上的非对象上调用成员函数execute() $query->execute($item);中的一些错误,为什么在不使用mb_decode_mimeheader函数更改字符串时可以运行该语句?

2.The insert_data function can't be run .
PHP Fatal error: Call to a member function execute() on a non-object in on line 49
Some bug in $query->execute($item); ,Why the statement can be run when not to change the string with mb_decode_mimeheader function?

推荐答案

尽管我不会完全回答为什么"(抱歉,在这里很晚了),但是您可以尝试使用

although I won't answer exactly "why" (sorry too late at night here) you can try using imap_mime_header_decode to make it possible to have it stored in the db. Make sure you treat it with iconv later as it s usually stored not in utf8. (in my case at least)

$x3 = $overview[0]->subject;
$subject =  imap_mime_header_decode(x3);
$subject = iconv('ISO-8859-2','utf-8',$subject[0]->text);

这篇关于为什么无法将我的gmail标头信息主题插入sqlite3数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆