Solved

php read all emails and save attachments

Posted on 2008-10-13
6
2,248 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:Squinky
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:RQuadling
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 25

Accepted Solution

by:
Squinky 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:Squinky
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

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

Join & Write a Comment

Suggested Solutions

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now