1

I am trying to write a php script that will email an image to myself. I had a more functions like sending json files and etc but those work.

When I ran the script using XAMPP, I used a hash to compare the image before sending, and the image that was sent. Hashes did not match. I opened both images in a hex editor (note I did this for a number of images I was testing and code changes) and I noticed that for every 0x00 there was, it was being replaced by a 0x20. Now I am hoping this is the only issue along with a solution to this issue.

Thanks in advance!

function generateBoundary()
    {
        $boundary = md5(time());
        return "=============\" . $boundary \"=============";
    }

    function attachImageFile($path, $boundary, $content_type, $encoding_type)
    {
        $start = strrpos($path, '/') == -1 ? strrpos($path, '//') : strrpos($path, '/')+1;
        $file_name = substr($path, $start, strlen($path));
        $file = fopen($path,'r');
        $data = fread($file, filesize($path));
        fclose($file);

        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $ftype = finfo_file($finfo, $path);

        $message =
            "--{$boundary}\r\n" .
            "Content-Type: {$ftype}; " .
            "name=\"{$file_name}\"\r\n" .
            "Content-Transfer-Encoding: {$encoding_type}\r\n" .
            "Content-Disposition: attachment;" . " filename=\"{$file_name}\"\r\n" .
            $data . "\r\n";

        return $message;
    }

    function generateMessage($body, $boundary)
    {
        // at this point all data will be seperated by boundary, text, files, etc, so ow we will use it to seperate the email body
        //$message = "TEST!!!!!!!!!!!!!!!!: \r\n" .
        $message =
            // the -- is needed before the boundary
            "--{$boundary}\r\n" .
            // charset=ISO-8859-1 is choosen since it covers a good amount of chars: https://www.w3schools.com/html/html_charset.asp
            "Content-Type:text/html; charset = \"iso-8859-1\"\r\n" .
            // https://stackoverflow.com/questions/25710599/content-transfer-encoding-7bit-or-8-bit
            "Content-Transfer-Encoding: 7bit\r\n" .
            // body text
            $body . "\r\n";
        return $message;
    }

    $to = "myemail@gmail.com";
    // email
    $subject = "subject";
    $header = "From:donotreply@fake.com \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $boundary = generateBoundary();
    $header .=
        "Content-Type: multipart/mixed; " .
        "boundary=\"{$boundary}\"\r\n";

    $message = "";
    $message .= generateMessage("THIS IS THE BODY", $boundary);

    $message .= attachImageFile("./test2/wallpaper.jpeg", $boundary, "image/jpeg", "binary");

    // last message needs to end the body
    $message .= "--{$boundary}\r\n";

    $result= mail ($to, $subject, $message, $header);

    if( $result == true ) {
        echo "Message sent";
    }else {
        echo "Message not sent";
    }

EDIT 0: I tried 7bit, 8bit, base64

EDIT 1: I also wanted to add when I click on the show original on my gmail account I see this

To: fake@gmail.com
Subject: Report Number
X-PHP-Originating-Script: 505:index.php
From: donotreply@fake.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=============" . f14134bdf626542e17344e7377f1f281 "============="
Message-Id: <20170321015658.8DC30D3471A@MacBook.localdomain>
Date: Mon, 20 Mar 2017 18:56:58 -0700 (PDT)

Content-Type: text/html; charset = "iso-8859-1"
Content-Transfer-Encoding: 7bit

THIS IS THE BODY
Content-Type: image/jpeg; name="wallpaper.jpeg"
Content-Transfer-Encoding: binary
Content-Disposition: attachment; filename="wallpaper.jpeg"

the file has the data, except for the 0x20 problem, and same size in MB but it does not show up here like it does when I add my code to send over text files. not sure if this is helpful or not

Lamp / Cakephp: Streaming an image : Binary 0x00 replaced by 0x20

EDIT 2: The file actually starts to have the wrong data (in binary and hex) after the 0x22 problem. here is a sample

Sent File (The bold shows where it starts to replace 0x00 with 0x20, but shortly after that the data is just bad data no longer the 0x200 problem)

FFD8FFE0 20104A46 49462001 01012060 20602020 FFDB2043 20050304 04040305 04040405 
05050607 0C080707 07070F0B 0B090C11 0F121211 0F111113 161C1713 141A1511 11182118 
1A1D1D1F 1F1F1317 2224221E 241C1E1F 1EFFDB20 43010505 05070607 0E08080E 1E141114 
1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 
1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1EFFC2 20110817 E02D8A03 01222002 11010311 01FFC420 
1C200101 01010101 010101**20 20202020 2020**2001 02030405 060708FF C4201A01 01010101 
01010120 20202020 20202020 20010203 040506FF DA200C03 01200210 03102020 01FE7AAF 
ADF58020 20205820 A420A094 25202094 2020200B 0B289651 2C160B2A A02CA008 A0202020 
20202020 1502C282 59402020 20202020 2020200B 200D0A10 0D0A2280 04282020 20202020 
20202020 2C0B200B 20142594

original file

FFD8FFE0 00104A46 49460001 01010060 00600000 FFDB0043 00050304 04040305 04040405
05050607 0C080707 07070F0B 0B090C11 0F121211 0F111113 161C1713 141A1511 11182118 
1A1D1D1F 1F1F1317 2224221E 241C1E1F 1EFFDB00 43010505 05070607 0E08080E 1E141114
1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1E1E1E 
1E1E1E1E 1E1E1E1E 1E1E1E1E 1E1EFFC2 00110817 E02D8A03 01220002 11010311 01FFC400
1C000101 01010101 01010100 00000000 00000001 02030405 060708FF C4001A01 01010101 
01010100 00000000 00000000 00010203 040506FF DA000C03 01000210 03100000 01FE7AAF
ADF58000 00005800 A400A094 25000094 0000000B 0B289651 2C160B2A A02CA008 A0000000 
00000000 1502C282 59400000 00000000 0000000B 000A100A 22800428 00000000 00000000
00002C0B 000B0014 25940000
Community
  • 1
  • 1
Tanner Summers
  • 689
  • 1
  • 8
  • 26

0 Answers0