Solved

php imap

Posted on 2013-10-31
6
274 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
[X]
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
  • 3
  • 2
6 Comments
 
LVL 110

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 110

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 110

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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…

730 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