Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<?php
header('Content-type: text/html; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');
header('Pragma: no-cache');
function extract_attachments($connection, $message_number) {
$attachments = array();
$structure = imap_fetchstructure($connection, $message_number);
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if ($structure->parts[$i]->ifdparameters) {
foreach ($structure->parts[$i]->dparameters as $object) {
if (strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if ($structure->parts[$i]->ifparameters) {
foreach ($structure->parts[$i]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if ($attachments[$i]['is_attachment']) {
$file = getcwd() .DIRECTORY_SEPARATOR. $attachments[$i]['filename'];
$attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i + 1);
file_put_contents($file, $attachments[$i]['attachment']);
}
}
}
return $attachments;
}
$servidor = 'mbox.domain.com';
$usuario = 'user=soulcomunicacao.com.br';
$senha = "pass1234";
$mbox = imap_open('{' . $servidor . '/pop3}INBOX', $usuario, $senha);
$erro[] = imap_last_error();
if (!empty($erro[0])) {
echo $erro[0];
exit;
} else {
$n_messages = imap_num_msg($mbox);
echo "<p>You got $n_messages message(s)</p>";
echo "<br /><br />";
echo '<table border="1" width="100%">';
echo '<tr>';
echo '<td align="center" width="150"><strong>Date</strong></td>';
echo '<td><strong>Sender</strong></td>';
echo '<td><strong>Subject</strong></td>';
echo '</tr>';
for ($i = 1; $i <= imap_num_msg($mbox); $i++) {
$headers = imap_header($mbox, $i);
$subject = imap_utf8($headers->subject);
$message_id = $headers->message_id;
$toaddress = $headers->toaddress;
$from = $headers->from;
$sender = $from[0]->personal;
$email_sender = $from[0]->mailbox;
$server_sender = $from[0]->host;
$date = $headers->date;
$date = strtotime($date);
$date = date("d/m/Y H:i:s", $date);
echo '<tr>';
echo '<td align="center">' . $date . '</td>';
echo '<td>' . $email_sender . '@' . $server_sender . '</td>';
echo '<td><a href="index.php?id=' . $i . '">' . $subject . '</a></td><td>';
echo '</tr>';
}
echo '</table>';
echo '<br />';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$message = imap_fetchbody($mbox, $id, 1);
echo nl2br(imap_utf8($message));
$struct = imap_fetchstructure($mbox, $id, FT_UID);
extract_attachments($mbox, $id);
}
imap_close($mbox);
}
?>