Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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>
		<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>
		$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

  • 3
  • 3
2 Solutions
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?
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.


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

Content-Transfer-Encoding: 7bit

            <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 />
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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--";

		$ok = @mail($user, $email_subject, $message, $headers);

Open in new window

InsoftserviceAuthor Commented:
Thanx for your help.
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now