Jump to content

IMAP readout errors


Dima_2005

Recommended Posts

Hi,

I want to have a PHP e-mail forwarding script, but I still have some problems.

First: this is my code (some parts come from PHP.net to help me out):

$emails = array("test","test2");
foreach($emails as &$addr){
$mbox = imap_open("{imapserverofmydomain/imap}INBOX","[email protected]","$pass");
$m_head = imap_headers($mbox);
$count = imap_num_msg($mbox);
echo "<p>$addr: $count</p>"; //Echo
$i = 0;
for($i = 1; $i < $count+1; $i++){
	$mailHeader = imap_headerinfo($mbox, $i);
	$from = $mailHeader->fromaddress;
	$subject = $mailHeader->subject;
	$structure = imap_fetchstructure($mbox,$i);
	if($structure->type == 1){
		$files = getdata("tmp/",$i,$mbox); // ==>array(); - filenames
		$text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
				<html>
				<head><title>$subject -resent</title>
				<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
				</head>
				<body style=\"margin: 10px;\"><div>";
		$text .= imap_fetchbody($mbox, $i, "1.2");
		$text = str_replace("=\r\n","",$text);
		$text = str_replace("=3D","=",$text);
		$text .= "</div></body></html>";
		$fp = fopen("message.html",'w');
		$data = $text;	
		fputs($fp,$data);
		fclose($fp);
	}
	else{
		$text = str_replace("=\r\n","",imap_body($mbox, $i));
		$text = str_replace("=3D","=",$text);
	}
	sendEmail(1, $subject, $text,$files, $addr, $pass);
	imap_delete($mbox,$i);
}
imap_expunge($mbox);
imap_close($mbox);
}
/*----------------------------------Find decoder--------------------*/
function getdecodevalue($message,$coding){
switch($coding) {
		case 0:
		case 1:
			$message = imap_8bit($message);
			break;
		case 2:
			$message = imap_binary($message);
			break;
		case 3:
		case 5:
			$message=imap_base64($message);
			break;
		case 4:
			$message = imap_qprint($message);
			break;
	}
	return $message;
}
/*---------------------------------Get attachment-----------------------------*/
function getdata($savedirpath,$msgID,$mbox){
$files = array();
//Message-array -> type
$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";

//Fetch structure
$structure = imap_fetchstructure($mbox, $msgID);    
$parts = $structure->parts;
$fpos=2;
//For parts
for($i = 1; $i < count($parts); $i++){
	$message["pid"][$i] = ($i);
	$part = $parts[$i];
	//Check if attachment
	if($part->disposition == "attachment") {
		$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
		$message["subtype"][$i] = strtolower($part->subtype);
		$ext=$part->subtype;
		$params = $part->dparameters;
		$filename=$part->dparameters[0]->value;

		$mege="";
		$data="";
		$mege = imap_fetchbody($mbox,$msgID,$fpos);  
		$filename="$filename";
		$fp=fopen($savedirpath.$filename,'w');
		$data= getdecodevalue($mege,$part->type);	
		fputs($fp,$data);
		fclose($fp);
		array_push($files,$filename);
		$fpos+=1;
	}
}
return $files;
}
//Here is the sendmail function, but it works good!

Now, my problems:

1) Text attachments fail to work, they are all messed up

2) In a lot of emails (HTML) I have characters like =3D (in stead of =), and so on, there are a lot, the most disturbing I removed with str_replace

3) Not all emails have the "1.2" of MIME, why? (it did work yesterday tho, so now it's empty, unless I attach attachments)

4) In-text images don't work, only as other attachments, but if I send an image (like a smiley) in the text, then it gives a lot of errors.

 

Can someone help me out? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/192804-imap-readout-errors/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.