How i can get IMAP Inbox and save it to db in codeigniter?

608 Views Asked by At

I have the following code working in PHP 7, i use it to save my mail inbox into my db.

$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}'; 
$username = '[email protected]'; $password = 'Mypassword'; 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
print_r(imap_errors());

$MC = imap_check($inbox);
$num = imap_num_msg($inbox);
if($num > 0) {
    //open MySQL connection
    $mysqli = new mysqli("localhost","root","password","mydb");
    //get from, date and subject
    $result = imap_fetch_overview($inbox,"1:{$MC->Nmsgs}",0);
    foreach ($result as $overview) {
        $message = imap_fetchbody($inbox, $overview->msgno, 1);
        echo "Next message:";
        echo "<br>";
        echo "#{$overview->msgno} - Date: ({$overview->date}) - From: {$overview->from} Subject: {$overview->subject}\n";
        echo "<br><br>";
        echo "Body: ".$message;
        $mysqli->query("INSERT INTO email VALUES ($overview->msgno, '$overview->date', '$overview->from', '$overview->subject',  '$message')");
    }
}

I want to know how i can make this in Codeigniter2, with PHP 5, using the controllers...

My intention is to save the inbox of my mail on a db, using IMAP. Then i want show it on a table in HTML, using one view in codeigniter.

Im newbie in codeigniter by the way, but i know how the system model-view-controller work.

Someone know how to do that?

Thanks you for your help, I appreciate it.

0

There are 0 best solutions below