Results 1 to 2 of 2

Thread: chunk_split(base64_encode()) issue in email

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default 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 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:
    Code:
    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
    Code:
    		 	$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.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not really sure, I would try adding a chunk size and the eol:

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

    PHP Code:
    <?php
    $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.
    Last edited by jscheuer1; 05-12-2016 at 01:22 AM. Reason: add bit about the test, later - advice about seeking libraries
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Simple send email issue
    By sniperman in forum ASP
    Replies: 1
    Last Post: 02-02-2011, 09:08 PM
  2. HTML Email Issue
    By tomyknoker in forum HTML
    Replies: 0
    Last Post: 07-02-2007, 12:40 AM
  3. Email issue -- Carbon Copy Issue
    By Humper in forum HTML
    Replies: 0
    Last Post: 09-01-2006, 09:01 PM
  4. Email issue -- Carbon Copy Issue
    By Humper in forum PHP
    Replies: 1
    Last Post: 09-01-2006, 08:47 PM
  5. Email issue -- Carbon Copy Issue
    By Humper in forum JavaScript
    Replies: 0
    Last Post: 09-01-2006, 07:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •