
02-19-2005, 08:50 AM
|
|
New Member
|
|
Join Date: Feb 2005
Posts: 4
|
|
How To: Display Emails (PHP)
With this PHP code snippet you can show an email overview and display emails. This example has been written for POP3 accounts. If you're using an IMAP account just replace /pop3 with /imap/notls
PHP Code:
<?php
function show_mails($server, $account, $password)
{
$mailbox = imap_open("{".$server.":110/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.":110/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");
?>
Used functions:
Can be found at this web address (I'm technically not allowed to post links yet...): php.de/ftopic31562.html
|

03-30-2005, 09:28 PM
|
|
WHT Addict
|
|
Join Date: Feb 2005
Posts: 114
|
|
WOW!! Thank you, this will help me alot!
|

06-10-2005, 01:40 AM
|
|
Newbie
|
|
Join Date: Jun 2005
Posts: 7
|
|
Thanks for this, it'll be of use to me because I'm trying to make my own simple webmail client. Thanks again.
|

06-12-2005, 10:09 PM
|
|
WHT Addict
|
|
Join Date: Jul 2001
Location: Glasgow, Scotland
Posts: 129
|
|
You seem to be lacking curly brackets for your last if ... else ... clause?
|

06-24-2005, 12:24 AM
|
|
Web Hosting Master
|
|
Join Date: Dec 2004
Location: Canada
Posts: 1,076
|
|
Quote:
Originally posted by the--dud
You seem to be lacking curly brackets for your last if ... else ... clause?
|
They're not required. If there are no curly braces, the if construct only applies to the following expression (in fact, curly braces just turn a bunch of expressions into a single one) see http://www.php.net/manual/en/languag...-structures.if
Good simple snippet.
|

07-10-2005, 03:13 AM
|
|
Who am I?
|
|
Join Date: Sep 2002
Location: Among the corn
Posts: 9,637
|
|
They MAY not be required if you're doing a simple check, such as
however, any time you have multiple lines, such as this
Code:
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 need to properly set things up. The above line should, indeed be:
Code:
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");
}
Another thing to keep in mind is that you really should avoid using nasty and messy if() statements like this. A better approach would be using a switch() statement and covering things with a default call, rather than messy if() statements like that.
|

07-10-2005, 07:40 AM
|
|
Web Hosting Master
|
|
Join Date: Dec 2004
Location: Canada
Posts: 1,076
|
|
No. The language grammar doesn't require the braces at all, ever. The problem is that it becomes ambiguous (to the programmer; the language knows what to do), especially when some else clauses are missing. In the case above, the code will do exactly what is intended. In other cases though, it can get confusing (and error-prone) as the else keyword *always* applies to the most recently opened if clause, while often it is another one the programmer wishes or expects it to apply to.
I'd probably do:
PHP Code:
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");
Just to make things clear. It's generally good practice to always include the braces to make sure your code is totally clear to human eyes. If it compiles without an error, the code will do exactly what you told it to do...but with features like this, that's often not what you intend. Use the braces for everything but the simplest of cases, but understand what's happening otherwise.
As for switch statements; they're good when you have a large number of possible input conditions for a single variable (and C forces this to be the only case), but become difficult to follow when you're dealing with multiple variables. Nested ifs are often much more clear in these sorts of cases (and probably faster as it's a tree-like structure rather than a sequential list, requiring less comparisons). Use whichever makes more logical sense. Have a bunch of possible input cases? Use a switch. Want to check a handful of conditions and do something different depending on the combination? Use nested ifs.
As always, try to write code that's clear to humans. The computer doesn't care, so make your life easier. Don't waste your time trying to 'optimize' things or any other such nonsense. As personal experience, I recently wrote an entire balanced tree class in PHP to try to optimize searching a large database of IPs (on the order of 100,000). Of course, not heeding my own advice, I went ahead coding this before I actually considered the performance of the 'easy way out' (in this case, something like in_array(), a sequential search, or even better, using the associative array (hash table)). Even stored the IPs as binary data. Moral of the story? Searching the tree was about an order of magnitude faster than the seq. search (which still only took 1/10th sec). Loading the tree took almost 30 seconds, and consumed over twice the RAM. Loading the list? Just under 1 second. Overall execution time: tree - 30.01s, list - 1.1s. tree - 2 hours coding, list 5 minutes coding. If the process were persistent and the data didn't have to be loaded every time it might be worth using the tree. With PHP though, the 'optimal' data structure was so slow it was unusable. I'm getting too used to C, where these things actually work as intended :p.
Anyway, there's a rant that's off topic.
Last edited by error404; 07-10-2005 at 07:46 AM.
|

08-30-2005, 12:12 PM
|
|
Newbie
|
|
Join Date: Aug 2005
Posts: 14
|
|
thank you so much coders!
|

09-05-2005, 09:10 AM
|
|
New Member
|
|
Join Date: Jul 2005
Location: Southampton, England
Posts: 4
|
|
Thanks very much for the code, will prove very useful 
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|