Hi, first am a noob at PHP and IMAP.. So please bear with me...

I am trying to create a PHP page that will check an IMAP server for any unseen messages and download an attachment (the attachment will always be the same audio file format), then attach the file to another email that will allow the user to click on a link to play the attachment.. Whew... Pretty much sums it up...

Now, I have script to login and see messages, or unseen messages, but have searched for two weeks on exactly how to get to the attachment.....

Thanks in advance!


Recommended Answers

All 2 Replies

     $host = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}inbox';
     $login = 'username';
     $password = 'password';
     $savedirpath = "/home/Mine/Public/";
     $type = 'ReadAttachment';
$obj = new $type;
class ReadAttachment
	function getdecodevalue($message,$coding) {
		switch($coding) {
			case 0:
			case 1:
				$message = imap_8bit($message);
			case 2:
				$message = imap_binary($message);
			case 3:
			case 5:
			case 4:
				$message = imap_qprint($message);
		return $message;

	function getdata($host,$login,$password,$savedirpath,$delete_emails=false) {
		// make sure save path has trailing slash (/)
		$savedirpath = str_replace('\\', '/', $savedirpath);
		if (substr($savedirpath, strlen($savedirpath) - 1) != '/') {
			$savedirpath .= '/';
		$mbox = imap_open ($host, $login, $password) or die("can't connect: " . imap_last_error());
		$message = array();
		$message["attachment"]["type"][0] = "text";
		$message["attachment"]["type"][1] = "multipart";
		$message["attachment"]["type"][2] = "message";
		$message["attachment"]["type"][3] = "application";
		$message["attachment"]["type"][4] = "audio";
		$message["attachment"]["type"][5] = "image";
		$message["attachment"]["type"][6] = "video";
		$message["attachment"]["type"][7] = "other";
		$emails = imap_search($mbox,'ALL');
		foreach($emails as $email_number) {
			$structure = imap_fetchstructure($mbox, $email_number , FT_UID);    
			$parts = $structure->parts;
			for($i = 1; $i < count($parts); $i++) {
				$message["pid"][$i] = ($i);
				$part = $parts[$i];
				if($part->disposition == "ATTACHMENT") {
					$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
					$message["subtype"][$i] = strtolower($part->subtype);
					$params = $part->dparameters;
					$mege = imap_fetchbody($mbox,$email_number,$fpos);  
		// imap_expunge deletes all tagged messages


Here is my code while i run this code part of the attachment only rest of them couldn't...Eg If i am having 5 PDF resources in this 2 or 3 of the attachments is downloaded rest of the resources not able to download any one is having idea about this...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.