Solved

php read all emails and save attachments

Posted on 2008-10-13
6
2,268 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
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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 22710492
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

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