Skip to content

Instantly share code, notes, and snippets.

@marlenesco
Last active September 27, 2017 16:15
Show Gist options
  • Save marlenesco/07f7568a6afe06b3a61a8c7e56fc1d0c to your computer and use it in GitHub Desktop.
Save marlenesco/07f7568a6afe06b3a61a8c7e56fc1d0c to your computer and use it in GitHub Desktop.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ERROR);
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL', SE_FREE, 'utf-8');
?><!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Gmail Imap</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<style>
.panel-group .panel {
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
box-shadow: none;
}
.panel-group .panel+.panel {
margin-top: 0;
border-top: 1px solid #fff;
}
.panel-title {
font-size: 13px;
}
.panel-title > a,
.panel-title > span {
display: block;
overflow: hidden;
}
.panel-title strong {
font-size: 15px;
}
.panel-title > a > span,
.panel-title strong {
display: block;
float: left;
overflow: hidden;
}
.seen {
width: 30px;
}
.subject {
padding-left: 15px;
border-left: 1px solid rgba(0, 0, 0, .2);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.from {
padding-left: 15px;
border-left: 1px solid rgba(0, 0, 0, .2);
width: 20%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.date {
float: none !important;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: right;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Gmail Imap</a>
</div>
</div>
</div>
<div class="container-fluid">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading-<?= $overview[0]->uid ?>">
<div class="panel-title">
<span>
<strong class="seen"><i class="fa fa-fw fa-eye"></i></strong>
<strong class="from">from</strong>
<strong class="subject">subject</strong>
<strong class="date">date</strong>
</span>
</div>
</div>
</div>
<?php
if($emails) {
rsort($emails);
$index = 0;
foreach($emails as $email_number) {
//if ($index > 20) {
// break;
//}
$overview = imap_fetch_overview($inbox, $email_number,0);
$structure = imap_fetchstructure($inbox, $email_number);
if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
$part = $structure->parts[1];
$message = imap_fetchbody($inbox, $email_number,2);
if($part->encoding == 3) {
$message = imap_base64($message);
} else if($part->encoding == 1) {
$message = imap_8bit($message);
} else {
$message = imap_qprint($message);
}
}
$date = date("d M H:i", strtotime(utf8_decode(imap_utf8($overview[0]->date))));
?>
<div class="panel panel-warning">
<div class="panel-heading" role="tab" id="heading-<?= $overview[0]->uid ?>">
<div class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-<?= $overview[0]->uid ?>" aria-expanded="false" aria-controls="collapse-<?= $overview[0]->uid ?>">
<span class="seen"><i class="fa fa-fw fa-envelope<?=($overview[0]->seen ? '-open text-muted' : '')?>"></i></span>
<span class="from"><?=($overview[0]->seen ? '' : '<b>')?><?= utf8_decode(imap_utf8($overview[0]->from)) ?><?=($overview[0]->seen ? '' : '</b>')?></span>
<span class="subject"><?=($overview[0]->seen ? '' : '<b>')?><?php //echo '[' .$part->encoding.']'; ?> <?= utf8_decode(utf8_encode(imap_utf8($overview[0]->subject))) ?><?=($overview[0]->seen ? '' : '</b>')?></span>
<span class="date"><?=($overview[0]->seen ? '' : '<b>')?><?= $date ?><?=($overview[0]->seen ? '' : '</b>')?></span>
</a>
</div>
</div>
<div id="collapse-<?= $overview[0]->uid ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<?= utf8_decode(utf8_encode($message)) ?>
</div>
</div>
</div>
<?php
$index +=1;
}
}
imap_close($inbox);
?>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment