Reading Email with PHP
Results 1 to 3 of 3

Thread: Reading Email with PHP

  1. #1
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447

    Reading Email with PHP

    Hey am working on a script and have hit a bit of a wall. I want to be able to update pages simply by sending an email to a specified POP enabled account (like gmail)

    But have been unable to find any tutorials on how to accomplish this - the only thing that seems to come close is :

    http://www.phpbuilder.com/columns/musone19990207.php3

    but as this seems to require the installation of an imap server on the machine you want to run the script on (and I am writing the script for use on my remotely hosted account not my local dev server) so this solution is not suitable.

    Has anyone came across any tutorials on this or can they point me at least in the right direction....I've tried searching but I guess am not using the correct terms as nothing is coming up

    thanks

    v_Ln

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    I simply love php with its boundless possibilities
    They way to accomplish your request is by using
    the php-imap facility[1], the same as mentioned
    in the article you linked. It does allow you to talk
    POP3 as well! Note, there is no need for an IMAP server,
    the library simply has to be loaded. The php manual[2] is
    quite descriptive. If you cannot use this (see below
    section configuration), check out this POP3-class based
    on sockets[3] (in german, but the script is self-explaining.
    I have not tested it, however!).

    But since I already do, what you want to do, here a
    short excerpt of some script of mine.

    Code:
    <?php
    
    if (!extension_loaded('imap'))
    	exit("php_imap.dll not loaded!");
    else 
    	echo "php_imap.dll loaded!<br><br>\n";
    
    
    
    $mbox=@imap_open("{pop3server/pop3:110}INBOX","username","password");
    if ($mbox==NULL)
    	exit("Could not connect!");
    
    
    	echo "Number of Mails: ".@imap_num_msg($mbox)."<br><br><hr><br><br>\n";
    
    	for ($i = 1; $i <= @imap_num_msg($mbox); $i++)
    	{
     	 	$header = @imap_headerinfo($mbox, $i,80,80);
    
     		echo "From:    ".$header->from[0]->mailbox."@".$header->from[0]->host."<br>\n";
      		echo "Subject: ".$header->fetchsubject."<br>\n";
    	
      		$body = @imap_body($mbox, $i);
      		echo $body."<br><br>\n<hr width=\"50%\"><br><br>\n";
    
      		@imap_delete($mbox,$i);
    	}
    
    
    
    	if (@imap_close($mbox,CL_EXPUNGE)==TRUE) 
    		echo "Connection successfully terminated!";
    	else 
    		echo "Error in closing connection!";
    ?>
    A few thoughts, nevertheless:

    1. Configuration
    Either use 'dl("php_imap.dll")'[4] (here: windows, if
    not multithreaded, and in case you cannot alter the php.ini file (remote server)),
    or (let) untag in the php.ini file the line
    Code:
    extension=php_imap.dll
    or corresponding to your OS.

    2. Security
    Since you plan to perform maintenance via email, be careful what you
    allow to do. My best suggestion is to actually sign the emails you send
    to that pop-account with your private key, in order to let php verify the
    integrity and authenticity of the message (ie the body of the email). Make
    sure to add some "salt" to the message In addition, I would also limit
    the size of allowed emails.

    3.Deleting mails
    Please note, that I do delete downloaded mails by closing the connection
    using the CL_EXPUNGE flag.


    Cheers


    [1] ftp://ftp.cac.washington.edu/imap/
    [2] http://www.php.net/imap
    [3] http://www.it-development.de/forum/s...5&pagenumber=2
    [4] http://www.php.net/dl


    /edit: Of course, parts of the code are copied one-to-one from php.net-comments
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Flash M0nkey
    Join Date
    Sep 2001
    Posts
    3,447
    ah sec you've come up with the goods again
    thanks matey - will take a look at the info you've provided (And the script) see what I come up with


    oh and if i could i would

    You must spread your AntiPoints around before giving it to sec_ware again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides