
|
View Full Version : How To: Display Emails (PHP)
toweter 02-19-2005, 08:50 AM 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
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
Atomic Haven 03-30-2005, 09:28 PM WOW!! Thank you, this will help me alot!
davidsoj 06-10-2005, 01:40 AM Thanks for this, it'll be of use to me because I'm trying to make my own simple webmail client. Thanks again.
the--dud 06-12-2005, 10:09 PM You seem to be lacking curly brackets for your last if ... else ... clause?
tiamak 06-23-2005, 08:19 PM you may also check my really LAME and ugly implementation on NET POP3 and MAIL MIME packages from pear.php.net :)
http://www.goscinnawies.pl/~tiamak/mail/
it is quite lame but works pretty nice
and allows you to send emails and receive emails with attachments
you will need:
http://pear.php.net/package/Mail_Mime
http://pear.php.net/package/Net_POP3
and
http://www.dynarch.com/projects/htmlarea/ (this is ofcourse not neccesary)
btw dont blame me its lame - i know that and i dont use it anywhere :)
error404 06-24-2005, 12:24 AM 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/language.control-structures.php#control-structures.if
Good simple snippet.
linux-tech 07-10-2005, 03:13 AM They MAY not be required if you're doing a simple check, such as
if ($a)
echo $a;
however, any time you have multiple lines, such as this
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:
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.
error404 07-10-2005, 07:40 AM 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:
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.
D3 Design 08-30-2005, 12:12 PM thank you so much coders!
lemonadeX 09-05-2005, 09:10 AM Thanks very much for the code, will prove very useful :)
|