Php mail breaks in pop and imap client (for eg outlook)

When i execute this code it works perfectly fine if mail is viewed in webmail. but when its viewed using imap or pop client.
 it breaks it shows mime headers.
below is my code . please help

		$myfile = "filepath";
		$file_size = filesize($myfile);
		$handle = fopen($myfile, "r");
		$content = fread($handle, $file_size);
		$user ="abc@test.com";
		$content = chunk_split(base64_encode($content));
		$total =0;
		$subject = 'Bulk deactivation report';
		$message = "<html>
		<body>
		<p>Dear admin,</p><br />
		<p>The bulk deactivation request has been successfully processed. The summary of the activity is as bellow</p>
		<p>IDs failed to get deactivate: ".intval($failval)."</p>
		<p>IDs deactivated successfully: ".intval($success)."</p>    <br />

		<p>Warm regards,</p><br />
		<p>Treo infotech,</p>
		<p>lovetomarry.com .</p>
		</body>
		</html>";
		
		$uid = md5(uniqid(time()));
		$user = $login;
		$myfile1 ='bulk_cvs.csv';
		$header = "From: <info@treo.biz>\r\n";
		$header .= "To: <".$user.">\r\n";
		$header .= "Cc: <info@treo.biz>\r\n";
		$header .= "Reply-To: ".$user."\r\n";
		$header .= "MIME-Version: 1.0\r\n";
		$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
		$header .= "This is a multi-part message in MIME format.\r\n";
		$header .= "--".$uid."\r\n";
		$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
		$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
		$header .= $message."\r\n\r\n";
		$header .= "--".$uid."\r\n";
		$header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n"; // use diff. tyoes here
		$header .= "Content-Transfer-Encoding: base64\r\n";
		$header .= "Content-Disposition: attachment; filename=\"".$myfile1."\"\r\n\r\n";
		$header .= $content."\r\n\r\n";
		$header .= "--".$uid."--";

		mail($login, $subject, $message, $header);

Open in new window

LVL 15
InsoftserviceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
1. You never close your file handle. A quicker way to get $content is just:

$content = file_get_contents($myfile);

...it will open the handle, read the entire file into the variable, and close it for you in one command.

2. Can you post the full source of an example message that was received?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InsoftserviceAuthor Commented:
Content-Type: multipart/mixed; boundary="6a7a8e345b4431dfca83a17298894efd"


Even if i write             fclose($handle);
o/p is same



This is a multi-part message in MIME format.

--6a7a8e345b4431dfca83a17298894efd

Content-type:text/html; charset=iso-8859-1

Content-Transfer-Encoding: 7bit



<html>
            <body>
            <p>Dear admin,</p><br />
            <p>The bulk deactivation request has been successfully processed. The summary of the activity is as bellow</p>
            <p>IDs failed to get deactivate: 9</p>
            <p>IDs deactivated successfully: 0</p>    <br />

            <p>Warm regards,</p><br />
0
gr8gonzoConsultantCommented:
That is not the full message source. That is the content. In Outlook, if you open up the message in its own window, there should be a "View Source" action in one of the toolbars at the top (depending on what version you're using). For example, Outlook 2013 (the ribbon style) has a "Move" ribbon group, and it's under "Actions" >> "Other Actions"

The full source should show you the headers and everything. My guess is that you have an extra line break in your headers somewhere and Outlook is ending the headers section prematurely, before it can process the MIME boundaries.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

InsoftserviceAuthor Commented:
I found the issue i had missed few boundaries , wrote new code for the same.

              		$attachment = chunk_split(base64_encode(file_get_contents($myfile)));
		$attachment = str_ireplace("\x0D", "", $attachment); 
	      $fileatt_type   = "application/octet-stream"; // File Type
	
	
	    $email_from     = "info@treo.biz"; // Who the email is from
		
		$headers                = "From: ".$email_from;

		$semi_rand              = md5(time());
		$mime_boundary          = "==Multipart_Boundary_x{$semi_rand}x";

		$random_hash = md5(date('r', time()));
		$headers               .= "\nMIME-Version: 1.0\n" .

		//add boundary string and mime type specification
		$headers .= "\nContent-Type: multipart/mixed;\n		boundary=\"PHP-mixed-".$random_hash."\"";		
				//define the body of the message.
		$message = "--PHP-mixed-$random_hash\n"
		."Content-Type: multipart/alternative;\n		boundary=\"PHP-alt-$random_hash\"\n\n";
		$message .= "--PHP-alt-$random_hash\n"
		."Content-Type: text/plain;\n		charset=\"iso-8859-1\"\n"
		."Content-Transfer-Encoding: 7bit\n\n";

		//Insert the plain text message.
		$message .= strip_tags($email_message);
		$message .= "\n\n--PHP-alt-$random_hash\n"
		."Content-Type: text/html;\n		charset=\"iso-8859-1\n"
		."Content-Transfer-Encoding: 7bit\n\n";

		//Insert the html message.
		$message .= $email_message;
		$message .="\n\n--PHP-alt-$random_hash--\n\n";

		//include attachment
		$message .= "--PHP-mixed-$random_hash\n"
		."Content-Type: $fileatt_type;\n		name=\"$myfile1\"\n"
		."Content-Transfer-Encoding: base64\n"
		."Content-Disposition: attachment\n\n";
		$message .= $attachment;
		$message .= "\n--PHP-mixed-$random_hash--";

		
		unset($data);
		unset($file);
		unset($fileatt_type);
		$ok = @mail($user, $email_subject, $message, $headers);

Open in new window

0
InsoftserviceAuthor Commented:
Thanx for your help.
0
gr8gonzoConsultantCommented:
Glad you got it fixed, but is there a reason for the "B" grade? Anything less than an "A" means that an expert failed in some way.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.