chunk_split(base64_encode()) issue in email

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 is 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 using is this: 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. 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 email 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.” What gives with chunk_split(base64_encode())?

Did you solve this one, Strangeplant? It appears not. If so, please let us know…

First, I question why you want to send an encrypted file by email. I mean, the reader would have to take
the results and decrypt it before they could read it. base_64 encoding is useful for transferring data back
and forth, but, not in emails unless the receiving end can decode it. Just wanted to point that out.

Here is a site that shows an example of a function set up to send attachments. Look at answer number 6.
It might help you. http://stackoverflow.com/questions/3092821/php-send-e-mail-with-attachment

No, I have not solved this problem. I now send these messages in plain text, and that works for a few that are short system generated information. (I have six instances of similar scripts in various class libraries) The base64_encode is standard to emails, btw, and is not specially encrypted - just an ordinary message. In the past I always sent both plain text and chunk_split(base64_encoded()), so that the email client could choose which to display and satisfy old ones that only understood plain text. I think now all clients understand chunked base64.

This script has worked fine for a number of years, until a server crash forced the use of PHP 5.4.45 instead of 5.2.5 A security fix, I think, changed the way email headers are treated, and it looks like more since the chunk_split(base64_encode($message_body)) now drops the first 57 characters if the message is generated by variables returned from the script, but not if the message is read from a text file. Maybe encoding issue? But I can’t find it.

Sorry, phone ringing off the hook this Monday and my flying fingers… I did not mean not to use the base64
encoding…

In line #14, you use just the \n (New-Line) without the carraige return? Might be the issue.

Also, at the end of the function, you suppress the error reporting. ( @mail() ) … Therefore, you can not see
the errors when the mail is sent. Normally, you would set all error message to be active and then any of them
would show up on the page.

Sponsor our Newsletter | Privacy Policy | Terms of Service