Solved

php read all emails and save attachments

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

Expert Comment

by:Loganathan Natarajan
ID: 22710492
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

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