Solved

php read all emails and save attachments

Posted on 2008-10-13
6
2,307 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 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…

717 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