Skip to content

Instantly share code, notes, and snippets.

@bachors
Last active January 22, 2020 14:51
Show Gist options
  • Select an option

  • Save bachors/8d8a1aa482bb3eabdff1 to your computer and use it in GitHub Desktop.

Select an option

Save bachors/8d8a1aa482bb3eabdff1 to your computer and use it in GitHub Desktop.
Reading emails from a IMAP mailbox - PHP example http://ibacor.com/demo/reading-emails-php/
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>IMAP mailbox - iBacor</title>
<!-- Custom style -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.min.css">
<style>
pre {
white-space: pre;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="alert alert-info alert-dismissable">Tutorial: <a style="color:#333;text-decoration:underline" href="http://ibacor.com/blog/reading-emails-from-a-imap-mailbox-php-example/" target="_BLANK">Reading emails from a IMAP mailbox - PHP example</a></div>
<?php
error_reporting(0);
// Multiple email account
$emails = array(
array(
'no' => '1',
'label' => 'Inbox Email 1',
'host' => '{mail.domain.com:143/notls}INBOX',
'username' => 'mail1@domain.com',
'password' => 'xxxxxxxxxx'
),
array(
'no' => '2',
'label' => 'Inbox Email 2',
'host' => '{mail.domain.net:143/notls}INBOX',
'username' => 'mail2@domain.net',
'password' => 'xxxxxxxxxx'
)
// bla bla bla ...
);
foreach ($emails as $email) {
$read = imap_open($email['host'],$email['username'],$email['password']) or die('<div class="alert alert-danger alert-dismissable">Cannot connect to yourdomain.com: ' . imap_last_error().'</div>');
$array = imap_search($read,'ALL');
if($array) {
$html = '';
rsort($array);
$html.= '<div class="panel panel-default">
<div class="panel-heading">
'.$email['label'].'
</div>
<div class="panel-body">
<div class="panel-group" id="accordion">';
foreach($array as $result) {
$overview = imap_fetch_overview($read,$result,0);
$message = imap_body($read,$result,0);
$reply = imap_headerinfo($read,$result,0);
$html.= ' <div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#'.$email['no'].$result.'">
<span class="subject">'.substr(strip_tags($overview[0]->subject),0,50).'.. </span>
<span class="from">'.$overview[0]->from.'</span>
<span class="date">on '.$overview[0]->date.'</span>
</a>
</h4>
</div>
<div id="'.$email['no'].$result.'" class="panel-collapse collapse">
<div class="panel-body">
<pre>'.$message.'<hr>From: '.$reply->from[0]->mailbox.'@'.$reply->from[0]->host.'</pre>
</div>
</div>
</div>';
}
$html.= '</div>
</div>
</div>';
echo $html;
}
imap_close($read);
}
?>
<!-- Javascript -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
@enofre
Copy link
Copy Markdown

enofre commented Oct 12, 2016

not working on me

@duongp131994
Copy link
Copy Markdown

code too very bad

@nbabariya
Copy link
Copy Markdown

its working for me.

Good Coding and working perfectly....

Copy link
Copy Markdown

ghost commented Dec 11, 2017

Works perfectly :)

@brayan13sep
Copy link
Copy Markdown

I had found solution, Error is with this line

$message = imap_body($read,$result,0);

Now, I am using

$message = imap_body($read,$result,1.2);
To receive body content in text/html format

Below i had given datails of available options. This might some one

()Root Message Part (multipart/related)
(1) The text parts of the message (multipart/alternative)
(1.1) Plain text version (text/plain)
(1.2) HTML version (text/html)
(2) The background stationary (image/gif)

@tghilardi
Copy link
Copy Markdown

Code runs, but message body and subject are not from the same message. Any ideas?

Copy link
Copy Markdown

ghost commented Apr 18, 2019

Great article,
I have a issue cannot display message body in accordion section. Any suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment