Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

php imap

Posted on 2013-10-31
6
273 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

839 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