Solved

php imap

Posted on 2013-10-31
6
269 Views
Last Modified: 2013-10-31
I having trouble inputting emails content  into a mysql database it only put one in.

<?php 
$imap = imap_open("{".$host.":143/novalidate-cert}INBOX", $user, $pass)or die(imap_last_error()); 
?><?php do{?><?php

   $counter++;
     
    $num = imap_num_msg($imap); 

	
	 
           
		  $partno = "1";

 $content_of_email= (imap_fetchbody($imap, $num, $partno));
          
		 
 $insertSQL = "INSERT INTO  `import`.`emails_in` (
`id` ,
`sContent` ,
`time`
)
VALUES (
NULL ,  '".$content_of_email."', 
CURRENT_TIMESTAMP
);";
                       

  mysql_select_db($database_con, $_con);
  $Result1 = mysql_query($insertSQL, $_con) or die(mysql_error());
     
	


    //move the email to our saved folder
    //$imapresult=imap_mail_move($imap,'1:'.$num,'INBOX.done');
    
    

	 

     
	 

 



} while ($num==0); 
  //close the stream 
     imap_close($imap);
	 echo $counter;
	 
	  ?>

Open in new window

0
Comment
Question by:beridius
  • 3
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39615639
What is the value of $num?  The way this is written it will only work once, unless $num is zero.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39615642
And in semi-related news, you will need to get off MySQL, because PHP is doing away with the MySQL extension.  This article explains why and shows you what you must do to keep your scripts running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 2

Author Comment

by:beridius
ID: 39615727
it goes round and round but only collects the first email and input that lots of times in the DB
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39615739
Let me try this simple question again.
What is the value of $num?
0
 
LVL 2

Author Comment

by:beridius
ID: 39615750
$num is the number of  emails I have changed it to counter =0  
<?php 
$counter=0;
$imap = imap_open("{".$host.":143/novalidate-cert}INBOX", $user, $pass)or die(imap_last_error()); 
?><?php do{?><?php

   $counter++;
     
    $num = imap_num_msg($imap); 

	
	 
           
		  $partno = "1";

 $content_of_email= (imap_fetchbody($imap, $num, $partno));
          
		 
 $insertSQL = "INSERT INTO  `import`.`emails_in` (
`id` ,
`sContent` ,
`time`
)
VALUES (
NULL ,  '".$content_of_email."', 
CURRENT_TIMESTAMP
);";
                       

  mysql_select_db($database_con, $_con);
  $Result1 = mysql_query($insertSQL, $_con) or die(mysql_error());
     
	


    //move the email to our saved folder
    //$imapresult=imap_mail_move($imap,'1:'.$num,'INBOX.done');
    
    

	 

     
	 

 



} while ( $counter==0); 
  //close the stream 
     imap_close($imap);
	 echo;
	 
	  ?>

Open in new window


but still only get the first email
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39615758
Try this

<?php 
$imap = imap_open("{".$host.":143/novalidate-cert}INBOX", $user, $pass)or die(imap_last_error()); 
$msgcount = imap_num_msg($imap); 

for($num = 1; $num <= $msgcount; $num++) {

 $partno = "1";

 $content_of_email= (imap_fetchbody($imap, $num, $partno));
	 
 $insertSQL = "INSERT INTO  `import`.`emails_in` (`id` ,`sContent` ,`time`)VALUES (NULL ,  '".$content_of_email."', CURRENT_TIMESTAMP);";

  mysql_select_db($database_con, $_con);
  $Result1 = mysql_query($insertSQL, $_con) or die(mysql_error());
   
} 

imap_close($imap);
?>

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question