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

Posted on 2014-08-28
Last Modified: 2014-09-05
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 ="";
		$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> .</p>
		$uid = md5(uniqid(time()));
		$user = $login;
		$myfile1 ='bulk_cvs.csv';
		$header = "From: <>\r\n";
		$header .= "To: <".$user.">\r\n";
		$header .= "Cc: <>\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

Question by:Insoftservice
    LVL 34

    Accepted Solution

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

    $content = file_get_contents($myfile); 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?
    LVL 15

    Author Comment

    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 />
    LVL 34

    Assisted Solution

    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.
    LVL 15

    Author Comment

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

    LVL 15

    Author Closing Comment

    Thanx for your help.
    LVL 34

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now