-
php script - display e-mails
Hello 
I am looking for some kind of solution, within I could display my gmail e-mails on a website. I have found a php script, which seems to be the one I am looking for, but I simply can't get it work and almost get frustrated with it 
My 2 questions:
1. If this php script is correct, where do I have to enter the e-mail, server and password to get it work for gmail?
2. If it would be an all-cach mail, is it possible to give the user a possibility to display just the e-mails, with the prefix he is looking for? (For example if the user enters "x", than he will be shown all "x@example.com" e-mails.
Thank You in advance for Your help.
Max 
<?php
function show_mails($server, $account, $password)
{
$mailbox = imap_open("{".$server.":995/pop3}INBOX", $account, $password);
$mails = imap_fetch_overview($mailbox,"1:*", FT_UID); // This is fetching an overview of all emails
// Output as a table:
$return = '<table width="100%">
<tr>
<td><b>#</b></td>
<td><b>From</b></td>
<td><b>Date / Time</b></td>
<td><b>Subject</b></td>
</tr>';
$size = count($mails); // Number of messages
$cmsg = 0; // This is used to have a continously number
for($i=$size-1;$i>=0;$i--)
{
$cmsg++;
$value = $mails[$i];
$return .= '<tr><td>'.$cmsg.'</td><td>'.$value->from.'</td><td>'.$value->date.'</td><td><a href="'.$_SERVER['PHP_SELF'].'?id='.$value->msgno.'">'.$value->subject.'</a></td></tr>';
}
$return .= '</table>';
imap_close($mailbox);
return $return;
}
function show_mail($id, $server, $account, $password)
{
$mailbox = imap_open("{".$server.":995/pop3}INBOX", $account, $password);
$mail = imap_body($mailbox,$id, FT_UID);
// This is fetching the email..
$mail = htmlentities(stripslashes($mail));
/* stripslashes is stripping the slashes, htmlentities transforms all of the non-regular symbols to their equal html code expression. */
$return = '<pre>'.$mail.'</pre>';
imap_close($mailbox);
return $return;
}
if(isset($_GET['id']))
if(is_numeric($_GET['id']))
echo show_mail($_GET['id'], "YourServerAddress", "Account", "Password");
else
echo 'wrong parameter';
else
echo show_mails("YourServerAddress", "Account", "Password");
?>
-
-
You put the server, username, and password in your function call (these:
echo show_mail($_GET['id'], "YourServerAddress", "Account", "Password");
). Replace them with your actual details.
-
The Following User Says Thank You to traq For This Useful Post:
-
I've tried it already, but I always get this error message:
Fatal error: Call to undefined function imap_open() in /users/test/www/mail/index.php on line 5
---
line 5:
$mailbox = imap_open("{".$server.":995/pop3}INBOX", $account, $password);
PS.: Is it perhaps, because I have entered it twice, once in:
echo show_mail($_GET['id'], "YourServerAddress", "Account", "Password");
and once in:
echo show_mails("YourServerAddress", "Account", "Password");
?
-
-
No, those are calls for separate functions (show_mails() shows the emails in your inbox, while show_mail() displays the contents of a particular email).
The "undefined function" message probably means that your server doesn't have the imap extension installed (or maybe your host doesn't make it available to the plan you are using). You would have to contact them to find out and/or ask (nicely!
) if they would install it for you.
Alternatively, you might be able to check yourself: create an empty php page and write <?php phpinfo(); ?>
, then upload that page to your server and open it in your browser. It will show info about your server's php configuration. Scroll down the list (it's a big list) and see if there's a box labeled "IMAP." If not, you'll have to ask your host about it.
-
The Following User Says Thank You to traq For This Useful Post:
-
Firstly I would like to thank you for your help, Traq 
I did what you told me to do and You were of course right. At bplaced.net, a free host, where I tried to do it, the imap extension is not installed.
So I tried to get it work on a different server with a following php configuration:
mencel.net/mail/php.php
And although a table can be seen now, the e-mail can't be downloaded from gmail:
mencel.net/mail
Here the source code of mencel.net/mail/index.php :
<?php
function show_mails($server, $account, $password)
{
$mailbox = imap_open("{".$server.":993/imap/notls}INBOX", $account, $password);
$mails = imap_fetch_overview($mailbox,"1:*", FT_UID); // This is fetching an overview of all emails
// Output as a table:
$return = '<table width="100%">
<tr>
<td><b>#</b></td>
<td><b>From</b></td>
<td><b>Date / Time</b></td>
<td><b>Subject</b></td>
</tr>';
$size = count($mails); // Number of messages
$cmsg = 0; // This is used to have a continously number
for($i=$size-1;$i>=0;$i--)
{
$cmsg++;
$value = $mails[$i];
$return .= '<tr><td>'.$cmsg.'</td><td>'.$value->from.'</td><td>'.$value->date.'</td><td><a href="'.$_SERVER['PHP_SELF'].'?id='.$value->msgno.'">'.$value->subject.'</a></td></tr>';
}
$return .= '</table>';
imap_close($mailbox);
return $return;
}
function show_mail($id, $server, $account, $password)
{
$mailbox = imap_open("{".$server.":993/imap/notls}INBOX", $account, $password);
$mail = imap_body($mailbox,$id, FT_UID);
// This is fetching the email..
$mail = htmlentities(stripslashes($mail));
/* stripslashes is stripping the slashes, htmlentities transforms all of the non-regular symbols to their equal html code expression. */
$return = '<pre>'.$mail.'</pre>';
imap_close($mailbox);
return $return;
}
if(isset($_GET['id']))
if(is_numeric($_GET['id']))
echo show_mail($_GET['id'], "imap.gmail.com", "all-cach@trash.name", "temp@password");
else
echo 'wrong parameter';
else
echo show_mails("imap.gmail.com", "all-cach@trash.name", "temp@password");
?>
So back to my questions:
1. How to get this script work, so that it shows up the e-mails.
2. As You can see it is an all-cach mail. So is it possible to give the user a possibility to display just the e-mails, with the prefix he is looking for? (For example if the user enters "x", than he will be shown all "x@example.com" e-mails.
PS.: In the script I have made a small change and to be able to use imap I have to replaced "/pop3" with "/imap/notls" and changed the ports, but it isn't still working.
... more about the project I am working at can be seen at trash.name
-
-
Well, I have no idea why it isn't retrieving your emails. I've never used this class, so I'm learning as I go here.
Are there any error messages returned when you try to fetch the emails?
---
As for sorting the emails, from looking at the documentation about imap_fetch_overview(), it seems you ought to be able to get the sent-to address using (in the "for" loop in your script above) $values->to
. You could then use those values as the basis of a sorting function.
-
-
I am really not good at php at all, but somehow I feel the solution coming closer and closer. Thousand thank for Your valuable informations, Traq 
I googled for "php show errors" and have paste this php sequence right after "<?php":
error_reporting(E_ALL);
ini_set('display_errors', '1');
and following errors has been displayed:
Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/notls}INBOX in /customers/mencel.net/mencel.net/httpd.www/mail/index.php on line 6
Warning: imap_fetch_overview() expects parameter 1 to be resource, boolean given in /customers/mencel.net/mencel.net/httpd.www/mail/index.php on line 7
Warning: imap_close() expects parameter 1 to be resource, boolean given in /customers/mencel.net/mencel.net/httpd.www/mail/index.php on line 26
# From Date / Time Subject
Notice: Unknown: Connection failed to gmail-imap.l.google.com,993: Connection timed out (errflg=2) in Unknown on line 0
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks