?
Solved

php read all emails and save attachments

Posted on 2008-10-13
6
Medium Priority
?
2,327 Views
Last Modified: 2012-05-05
Im trying to write a script that will open a stream to my POP3 mailbox, cycle through all emails, and save any attachments that are in the mails into a directory.

Problem is every time I run it, I get the message 'This application has requested the Runtime to terminate it in an unusual way.' and fails, however seems to be able to read the first attachments name, even creates a file, but there is nothing in it.

Any ideas whats going on, Im really confused here!

<?php
	$mail = imap_open ("{localhost:995/pop3/ssl/novalidate-cert}INBOX", "myMailBox", "myPassword");
	print_r(imap_errors());
	$headers = imap_headers($mail);
	$last = imap_num_msg($mail);
	$header = imap_header($mail,$last);
	
	for ($counter = 0; $counter <= $last; $counter++) 	
		{
		$info = imap_fetchstructure($mail, $last);
		$numparts = count($info->parts);
		$i=0;
 
		if ($numparts >1)
			{
			foreach ($info->parts as $part)
				{
				if ($part->disposition == "attachment")
					{
					$i++;
					echo $i." Attachment/s found!\n\n";
					echo "\t C:\\out\\" . $part->dparameters[0]->value . "\n";
					imap_savebody ($mail , "c:\\out\\" . $part->dparameters[0]->value , $counter, $i); 
					exit();
					}
				}
			}	
		}
	imap_close($mail);
?>

Open in new window

0
Comment
Question by:tonelm54
[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
6 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22710213
Are you sure the user that it's running as has sufficient permissions to write into that location? It seems to be a common problem on Windows that users are allowed to create files but not write anything into them, but it can be fixed with correct permissions.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 22710243
Add this to the top of your code ...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);

// Your code from here...


This will show ALL errors/warnings/notices/etc. and allow for the script to run for more than 30 seconds.

Do you get any other output?

Can you also remove the exit in the innermost loop.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 2000 total points
ID: 22710794
There's nothing particular wrong with the MIME parser in the IMAP extension. There's another one in the PHP mailparse extension (in PECL): http://www.php.net/manual/en/book.mailparse.php that will be way faster than any PHP class, and don't forget the one in PEAR's Mail_MIMEdecode package: http://pear.php.net/package/Mail_mimeDecode There's also Zend's Zend_Mail: http://framework.zend.com/manual/en/zend.mail.read.html and ezComponents ezcMail: http://ezcomponents.org/docs/tutorials/Mail
All of these packages are also much more up to date and actively maintained - that sourceforge project has not been touched for 7 years!
0
 

Author Comment

by:tonelm54
ID: 22817054
Anyone got any examples, Ive been playing around with PEAR, but finding it difficult to find examples on dealing with POP3 accounts and attachments.
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22829326
That it's POP3 shouldn't make much difference. However you get hold of a message (with attachments or not), you'll just have a big mime structure to decode, so treat them as separate things:

1) get a message
2) take it apart and examine its structure for attachments

Bear in mind that MIME doesn't really treat attachments any differently to any other part of a message such as a regular message body. It's just a matter of identifying MIME parts that have a 'content-disposition: attachment' header.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 dynamically set the form action using jQuery.
Suggested Courses

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