PHP邮件功能发送的图像用0x20替换0x00

时间:2017-03-21 04:03:50

标签: php email jpeg

我正在尝试编写一个PHP脚本,将图像通过电子邮件发送给自己。我有更多的功能,如发送json文件等,但这些工作。

当我使用XAMPP运行脚本时,我使用哈希来比较发送前的图像和发送的图像。哈希不匹配。我在十六进制编辑器中打开了这两个图像(注意我为我正在测试和代码更改的许多图像做了这个)并且我注意到,对于每个0x00,它被替换为0x20。现在我希望这是解决这个问题的唯一问题。

提前致谢!

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";
    }

编辑0:我试过7bit,8bit,base64

编辑1:当我点击我的Gmail帐户上的show original时,我也想添加

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"

该文件包含数据,除了0x20问题,以及相同的大小(以MB为单位),但它没有像我在添加代码发送文本文件时那样显示在这里。不确定这是否有用

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

编辑2:在0x22问题之后,文件实际上开始有错误的数据(二进制和十六进制)。这是一个样本

已发送文件(粗体显示它开始用0x20替换0x00,但不久之后数据只是坏数据不再是0x200问题)

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

原始文件

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

0 个答案:

没有答案