Pernahkah anda mencoba Thunderbird? Salah satu Mail Client Program yang menggunakan teknologi POP3/SMTP dan digunakan untuk manajemen email. Aplikasi ini dapat membuka email tanpa harus masuk ke layanan penyedia email semacam gmail. Kali ini, kita akan membahas bagaimana cara membaca email Gmail menggunakan PHP dan IMAP.
Requirement
- Extensi PHP IMAP harus aktif.
- Pengaturan IMAP pada Gmail harus diaktifkan
Cara Mengaktifkan extensi IMAP
Extensi IMAP secara default tidak terinstall. Untuk menginstall extensi IMAP, jalankan perintah berikut ini di console.
- Install IMAP
sudo apt-get install php5-imap
- Tambahkan extensi IMAP pada konfigurasi php.ini
extension=imap.so
atau jalankan perintah berikut ini di console.php5enmod imap
- Restart Apache
sudo /etc/init.d/apache2 restart
Mengaktifkan IMAP pada Gmail
Untuk mengaktifkan IMAP pada konfigurasi Gmail, masuk ke menu setting. Kemudian pilih tab Forwarding and POP/IMAP, lalu aktifkan IMAP Access.

Kode
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'username@gmail.com';
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
ALL merupakan kriteria pencarian yang akan dilakukan untuk mengambil data email. Berikut ini beberapa pilihan kriteria pencarian yang bisa digunakan.
ALL | return all messages matching the rest of the criteria |
ANSWERED | match messages with the \ANSWERED flag set |
BCC “string” | match messages with “string” in the Bcc: field |
BEFORE “date” | match messages with Date: before “date” |
BODY “string” | match messages with “string” in the body of the message |
CC “string” | match messages with “string” in the Cc: field |
DELETED | match deleted messages |
FLAGGED | match messages with the \FLAGGED (sometimes referred to as Important or Urgent) flag set |
FROM “string” | match messages with “string” in the From: field |
KEYWORD “string” | match messages with “string” as a keyword |
NEW | match new messages |
OLD | match old messages |
ON “date” | match messages with Date: matching “date” |
RECENT | match messages with the \RECENT flag set |
SEEN | match messages that have been read (the \SEEN flag is set) |
SINCE “date” | match messages with Date: after “date” |
SUBJECT “string” | match messages with “string” in the Subject: |
TEXT “string” | match messages with text “string” |
TO “string” | match messages with “string” in the To: |
UNANSWERED | match messages that have not been answered |
UNDELETED | match messages that are not deleted |
UNFLAGGED | match messages that are not flagged |
UNKEYWORD “string” | match messages that do not have the keyword “string” |
UNSEEN | match messages which have not been read yet |
Sampai disini kita sudah bisa mendapatkan daftar email dalam bentuk array yang disimpan dalam variable $emails.
// ambil 10 email
$emails = array_slice($emails, -10);
rsort($emails);
$content = array();
foreach($emails as $email) {
$overview = imap_fetch_overview($inbox, $email, 0);
$message = imap_fetchbody($inbox, $email, 2);
$content[] = array(
'seen' => $overview[0]->seen,
'flagged' => $overview[0]->flagged,
'from' => $overview[0]->from,
'subject' => $overview[0]->subject,
'date' => $overview[0]->date
);
}
Nilai dari variable $content kurang lebih akan seperti ini:
Array
(
[0] => Array
(
[seen] => 1
[flagged] => 0
[from] => Google
[subject] => Akun Google: upaya masuk diblokir
[date] => Wed, 3 Jun 2015 03:18:10 +0000 (UTC)
)
[1] => Array
(
[seen] => 1
[flagged] => 1
[from] => "jobsDB.com"
[subject] => 1 new jobs for Job Alert
[date] => 3 Jun 2015 07:45:04 +0800
)
)
Selanjutnya tinggal kita olah variable $content untuk menampilkan datanya dalam bentuk HTML.
No Comments
Leave a comment Cancel