PHP memiliki fungsi untuk mengambil dan membaca email melalui internet protokol POP3 dan IMAP. Port yang kita gunakan untuk itu adalah 993 (IMAP SSL)
Fungsi-fungsi yang digunakan adalah:
imap_open($hostname,$username,$password);
imap_fetch_overview($mbox,”1:$MN”,0);
imap_header($mbox, $msg);
Coding berikut ini menampilkan semua email dari GMail Anda, seperti pada gambar di atas.
<!DOCTYPE html> <html> <head> <title>Baca Email</title> <style type="text/css"> #mytable {width:800px;border:1px #a9c6c9 solid;font:12px verdana,arial,sans-serif;color:#333333;} #mytable td {padding:8px;} #mytable tr:hover td {background:#a0a0a0;color:#ffffff;} #mytable th {background-color:#000099;color:#ffffff;padding:8px; } #mytable tr:nth-child(odd) {background-color:#c0c0c0;} #mytable tr:nth-child(even) {background-color:#f0f0f0;} </style> <script type="text/javaScript"> function buka(url) {newwindow = window.open(url, '_blank', "status=yes, height=300, width=400, resizeable=yes");} </script> </head> <body> <?php $hostname = '{imap.gmail.com:993/ssl/novalidate-cert}[Gmail]/All Mail'; $username = 'your_email@gmail.com'; $password = 'your_gmail_password'; $mbox = imap_open($hostname,$username,$password) or die('Cannot connect to mail server: ' . imap_last_error()); $MC=imap_check($mbox); $MN=$MC->Nmsgs; $overview=imap_fetch_overview($mbox,"1:$MN",0); $size=sizeof($overview); echo "<h1>Baca Email</h1> <table id='mytable'> <tr><th>Msg_Id</th><th>From</th><th>Email</th><th>Date</th><th>Subject</th><th>Size</th></tr>"; for($i=$size-1;$i>=0;$i--) { $val=$overview[$i]; $msg=$val->msgno; $date=date('Y-m-d H:i:s', strtotime($val->date)); $subj=isset($val->subject)?$val->subject:"(no subject)"; $header = imap_header($mbox, $msg); $from = $header->from; $email_size = $val->size; foreach ($from as $id => $object) { $fromname = isset($object->personal)?$object->personal:$object->mailbox; $fromaddress = $object->mailbox . "@" . $object->host; } echo "<tr onclick=\"buka('read_email.php?msgno=$msg&msgdate=$date&msgfrom=$fromname&msgemail=$fromaddress&msgsubj=$subj');\"> <td>$msg</td> <td>$fromname</td> <td>$fromaddress</td> <td>$date</td> <td>$subj</td><td>$email_size</td></tr>"; } echo "</table>"; imap_close($mbox); ?> </body> </html>
Anda dapat click baris pada table-nya untuk membaca isi email Anda pada window browser baru.
Berikut ini coding untuk membaca isi email Anda
<?php function get_mime_type(&$structure) { $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); if($structure->subtype) {return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;} return "TEXT/PLAIN"; } function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) { if (!$structure) {$structure = imap_fetchstructure($stream, $msg_number);} if($structure) { if($mime_type == get_mime_type($structure)) { if(!$part_number) {$part_number = "1";} $text = imap_fetchbody($stream, $msg_number, $part_number); if($structure->encoding == 3) {return imap_base64($text);} else if ($structure->encoding == 4) {return imap_qprint($text);} else {return $text;} } if ($structure->type == 1) /* multipart */ { while (list($index, $sub_structure) = each($structure->parts)) { if ($part_number) {$prefix = $part_number . '.';} $data = get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); if ($data) {return $data;} } } } return false; } $msg_number = $_GET['msgno']; $msg_from = $_GET['msgfrom']; $msg_email = $_GET['msgemail']; $msg_date = $_GET['msgdate']; $msg_subject = $_GET['msgsubj']; $hostname = '{imap.gmail.com:993/ssl/novalidate-cert}[Gmail]/All Mail'; $username = 'your_email@gmail.com'; $password = 'your_gmail_password'; $stream = imap_open($hostname,$username,$password) or die('Cannot connect to mail server: ' . imap_last_error()); $isiemail = get_part($stream, $msg_number, "TEXT/HTML"); echo "<h2>$msg_subject</h2> From: $msg_from ($msg_email) <br /> Date: $msg_date<br/> <hr /> $isiemail"; ?>
Selamat mencoba, semoga sukses.