View Full Version : chunk_split(base64_encode()) issue in email

05-11-2016, 04:55 PM
For whatever reason, the string that I pass to chunk_split(base64_encode()) tosses the first part of the string, and only part comes through. The plaintext version reads fine if I leave out the base_64 encoded section. While I suppose that I can just leave things alone and it will work, I want to find out about this problem. The code that I'm writing is this, part of a larger class library:

public function adminEmail($subject,$body) {
$eol = PHP_EOL;
$plaintext = strip_tags($body);
$boundary = uniqid(rand(), true);
$headers = "From: <" . $this->fromemail . ">".$eol;
$headers .= "Reply-To: <" . $this->replyto . ">".$eol;
$headers .= "Cc: ".$this->admincc.$eol;;
$headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"; type=\"multipart/alternative\"";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "--$boundary".$eol;
$headers .= "Content-Type: text/plain; charset=ISO-8859-1".$eol;
$message = chunk_split($plaintext);
// $message .= "--$boundary".$eol;
// $message .= "Content-Type: text/html; charset=ISO-8859-1\n" . "Content-Transfer-Encoding: base64".$eol;
// $message .= chunk_split(base64_encode($body)); // chunk_split adds the ending "\r\n"
$message .= "--$boundary--".$eol;
return @mail($this->adminemail, $subject, $message, $headers); // true if email sent
The part commented out is giving me trouble. The three lines above marked //} For instance, if

$time_execution = microtime(true) - $time_start;
$done = "$pronum interpolations generated in $time_execution seconds.";
$body = date('Ymd H:i:s'.substr(microtime(), 1, 8))." ".$done;
Then the output in the encoded email part is "1.9073486328125E-6 seconds." while the plain text in the email is "20160509 11:15:40.7827080 123 interpolations generated in 1.9073486328125E-6
seconds." I can test the contents of the base64 encoding using an on-line tester to verify that they are entirely different, or missing the first part. I'm using PHP 5.4.45 (I'm told that this makes it essentially v 5.5) What gives with chunk_split(base64_encode())? And in another instance of a similar function, html lines are split leaving pieces that don't mean anything by themselves, ie the metadata lines. Am I overlooking something? This never happened before when used on a php 5.2 system and I haven't found any documentation changes between these versions.

05-11-2016, 07:53 PM
I'm not really sure, I would try adding a chunk size and the eol:

$message .= chunk_split(base64_encode($body), 72) . $eol; // chunk_split adds the ending "\r\n"

Also, just to make sure these functions are working properly, you can try testing them on an image:

$file = 'coconut.jpg'; // must be a real image in the path/current working directory
echo "<img src=\"data:image/jpeg;base64," . chunk_split(base64_encode(fread(fopen($file,"rb"),filesize($file))),72) . "\">\r\n";

So, if that checks out and you see the image, but you're still having a problem with your homemade function, I would suggest Googling PHP mail functions/classes and resorting to an up to date one. I know from personal experience that complex operations like mail are often best carried out by libraries constructed by folks more familiar with all of the ins and outs of that particular objective.