Solved

php imap

Posted on 2013-10-31
6
264 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 108

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 108

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scandir() not reads all files 4 26
Wordpress syntax error 1 29
Adding Extra Information box 4 26
Modify PHP Code on the Fly? 8 42
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now