troubleshooting Question

Imap attachments

Avatar of tonelm54
tonelm54 asked on
2 Comments1 Solution450 ViewsLast Modified:
Good morning,
Im trying to write a script to save all attachments from an IMAP account, currently Im just working on trying to save the data to the local folder, but I cannot seem to get my code to save.

I can see the email details, and even the attachment name, but it will not save.

The lines that should be saving the file are:-
				$tFileHandle = fopen($part->description, "w");
				fwrite($tFileHandle, imap_fetchbody($inbox, $iCnt, $pCnt));
				fclose ($tFileHandle);

My entire code is:-
$hostname = '{}INBOX';
$username = '';
$password = 'myPAssword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
	/* put the newest emails on top */
	$iCnt = 0;
	foreach($emails as $email_number) 
		$overview = imap_fetch_overview($inbox,$email_number,0);
		$structure = imap_fetchstructure($inbox,$email_number);
		$pCnt = 0;
		foreach ($structure->parts as $part) 
			if ($part->disposition == "ATTACHMENT") 
      			echo "Attachment found!<BR>";
				echo "Filename: ", $part->description . "<BR>";
				$tFileHandle = fopen($part->description, "w");
				fwrite($tFileHandle, imap_fetchbody($inbox, $iCnt, $pCnt));
				fclose ($tFileHandle);

		echo $overview[0]->from . "<BR>";
		echo $overview[0]->date . "<BR>";
		echo $overview[0]->subject . "<BR>";
		echo "<HR>";

		//Just exit after 60 emails.
		if ($iCnt > 60)
	echo $output;

/* close the connection */
Loganathan Natarajan
LAMP Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros