douzhi3779 2019-06-23 02:52
浏览 124

如何从imap_open()函数输出json数据?

This is my php code

    <?php
    error_reporting(0);

    // Multiple email account
    $emails = array(
        array(
            'no' => '5',
            'label' => 'inbox 1',
            'host' => '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX',
            'username' => 'xxx@gmail.com',
            'password' => 'xxx'
        ),
        array(
            'no' => '2',
            'label' => 'Inbox Email 2',
            'host' => '{mail.domain.net:143/notls}INBOX',
            'username' => 'mail2@domain.net',
            'password' => 'xxxxxxxxxx'
        )
    );

    foreach ($emails as $email) {

        $read = imap_open($email['host'], $email['username'], $email['password']) or die('Cannot connect to yourdomain.com: ' . imap_last_error() . '</div>');

        $array = imap_search($read, 'SUBJECT "123"');

        if ($array) {

            $html = '';

            rsort($array);

            foreach ($array as $result) {


                $overview = imap_fetch_overview($read, $result, 0);
                $message  = imap_body($read, $result, 0);
                $reply    = imap_headerinfo($read, $result, 0);

                //Now I want a json output like bellow structure
                /*
                "email no":"1",//from emails array
                "email lebel":"label",//from emails arrey
                "subject" :"mail_subject",
                "date"    :"full date time",
                "message" :"messege",
                "sent_from":"sender email"
                */


            }

        }

        imap_close($read);

    }

    ?>

I want json output from fetched emails like the following structure:

"email no":"email no",
"email_label":"label",
"subject" :"mail_subject",
"date"    :"full date time",
"message" :"messege",
"sent_from":"sender email"

I don't know how to do that, if anybody write a example code that will very helpful for me

I tested it to print as normal using this code:

echo $overview[0]->from;
echo $overview[0]->date;
echo $overview[0]->subject;
echo $reply->from[0]->mailbox.'@'.$reply->from[0]->host;
echo $message;

and this is the output:

sahidul islam Sun, 23 Jun 2019 12:54:32 +0800123sahid4745@gmail.com--0000000000008222fd058bf67e3a Content-Type: text/plain; charset="UTF-8" Thi is test message --0000000000008222fd058bf67e3a Content-Type: text/html; charset="UTF-8"
    Thi is test message

this part of the message --0000000000008222fd058bf67e3a Content-Type: text/plain; charset="UTF-8" Thi is test message --0000000000008222fd058bf67e3a Content-Type: text/html; charset="UTF-8" Thi is test message may be be conflicting when output as json_encode, How do I extract Thi is test message from $message?

  • 写回答

1条回答 默认 最新

  • doumou5109 2019-06-23 04:11
    关注

    My guess is that here we can just assemble an array we desire to output, then json_encode:

    $json_output = []; // output array to be JSONed
    
    foreach ($emails as $email) {
        $read = imap_open($email['host'], $email['username'], $email['password']) or die('Cannot connect to yourdomain.com: ' . imap_last_error() . '</div>');
        $array = imap_search($read, 'SUBJECT "123"');
    
        if ($array) {
            $html = '';
            rsort($array);
    
            foreach ($array as $result) {
    
                $overview = imap_fetch_overview($read, $result, 0);
                $message = imap_body($read, $result, 0);
                $reply = imap_headerinfo($read, $result, 0);
    
                // data from email array
                $json_output["email no"] = $email["no"];
                $json_output["email label"] = $email["label"];
    
                // data from other variables
                $json_output["subject"] = $result["mail_subject"];
                $json_output["date"] = $result["full date time"];
                $json_output["message"] = $result["messege"];
                $json_output["sent_from"] = $result["sender email"];
    
                $json_output = json_encode($json_output, true);
    
            }
    
        }
    
        imap_close($read);
    }
    

    I was not sure, where the data comes from for these vars, which I'm sure you would know that:

    $json_output["subject"] = $result["mail_subject"];
    $json_output["date"] = $result["full date time"];
    $json_output["message"] = $result["messege"];
    $json_output["sent_from"] = $result["sender email"];
    

    Extracting the Message

    There should be better ways to extract the message, one of which would be:

    $re = '/charset="UTF-8"\s*(.+?)\s+-/s';
    $str = '--0000000000008222fd058bf67e3a Content-Type: text/plain; charset="UTF-8" Thi is test message --0000000000008222fd058bf67e3a Content-Type: text/html; charset="UTF-8"
        Thi is test message';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    var_dump($matches);
    

    Demo

    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失