hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : How To: Display Emails (PHP)
Reply

Programming Tutorials How-Tos related to programming, databases, and the like.
Forum Jump

How To: Display Emails (PHP)

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 02-19-2005, 08:50 AM
toweter toweter is offline
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,$idFT_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

Reply With Quote


Sponsored Links
  #2  
Old 03-30-2005, 09:28 PM
Atomic Haven Atomic Haven is offline
WHT Addict
 
Join Date: Feb 2005
Posts: 114
WOW!! Thank you, this will help me alot!

Reply With Quote
  #3  
Old 06-10-2005, 01:40 AM
davidsoj davidsoj is offline
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.

Reply With Quote
Sponsored Links
  #4  
Old 06-12-2005, 10:09 PM
the--dud the--dud is offline
WHT Addict
 
Join Date: Jul 2001
Location: Glasgow, Scotland
Posts: 129
You seem to be lacking curly brackets for your last if ... else ... clause?

Reply With Quote
  #5  
Old 06-23-2005, 08:19 PM
tiamak tiamak is offline
Junior Guru
 
Join Date: Oct 2004
Location: Złocieniec, Poland
Posts: 190
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

Reply With Quote
  #6  
Old 06-24-2005, 12:24 AM
error404 error404 is offline
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.

Reply With Quote
  #7  
Old 07-10-2005, 03:13 AM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
They MAY not be required if you're doing a simple check, such as
Code:
if ($a)
echo $a;
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.

Reply With Quote
  #8  
Old 07-10-2005, 07:40 AM
error404 error404 is offline
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.
Reply With Quote
  #9  
Old 08-30-2005, 12:12 PM
D3 Design D3 Design is offline
Newbie
 
Join Date: Aug 2005
Posts: 14
thank you so much coders!

Reply With Quote
  #10  
Old 09-05-2005, 09:10 AM
lemonadeX lemonadeX is offline
New Member
 
Join Date: Jul 2005
Location: Southampton, England
Posts: 4
Thanks very much for the code, will prove very useful

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
eleven Report for June Sees 927.4 Percent Rise in Virus-Infected Emails Web Hosting News 2012-06-07 15:33:50
Security Firm eleven Reports Phishing Email Increase in December and January Web Hosting News 2012-02-08 14:20:06
eleven Report Finds Online Casinos Most Popular Spam Topic in November Web Hosting News 2011-12-12 21:58:22
Security Firm eleven Report Finds 89 Percent Spam Increase Since July Web Hosting News 2011-10-12 19:04:26
eleven Email Security Report Finds Decrease in US Spam Web Hosting News 2011-06-16 19:17:06


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?