PHP Mail() 无法发送带附件的电子邮件

PHP Mail() Failed to send email with attachment

我正在尝试创建图像的 zip,然后发送电子邮件并将 .zip 文件附加给用户。但是,电子邮件未发送。我在服务器的其他地方使用 mail() 所以我知道服务器没有阻止这个功能。

if( (mysqli_num_rows($gallery_query) >= 1) || ($_SESSION['permission'] == 1) ){

        $photos = array();

        while( $photo = mysqli_fetch_assoc($gallery_query) ){

            $file_headers = @get_headers($photo[path]);
            if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.1 403 Not Found' || $file_headers[0] == 'HTTP/1.0 404 Not Found' || $file_headers[0] == 'HTTP/1.0 403 Forbidden') {

                $file_headers = @get_headers($photo[image]); 
                if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.1 403 Not Found' || $file_headers[0] == 'HTTP/1.0 404 Not Found' || $file_headers[0] == 'HTTP/1.0 403 Forbidden') {
                    //If we made it here we dont' have a copy of the image
                } else {
                    $photos[] = $photo[image];
                }
            } else {
                $photos[] = $photo[path];
            }   

        }

        //Begin zipping
        $zip = new ZipArchive();

        $tmp_file = tempnam('.','');
        $zip->open($tmp_file, ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE);

        foreach($photos as $file){
            try{
                    // download file
                    $download_file = file_get_contents($file);

                    //add it to the zip
                    $zip->addFromString(basename($file),$download_file);

            } catch(Exception $e){
            }

        }
        $zip->close();      

    } //End if mysqli_num_rows >= 1

    $htmlbody = " Your Mail Contant Here.... You can use html tags here...";
    $to = $email; //Recipient Email Address
    $subject = "Images"; //Email Subject

    $random_hash = md5(date('r', time()));

    $headers = "From: no-reply@DOMAIN.com\r\nReply-To: no-reply@DOMAIN.com";
    $headers .= "MIME-Version: 1.0\n\r";
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

    $attachment = chunk_split(base64_encode(file_get_contents($tmp_file))); 


    //define the body of the message.
    $message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
    $message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";


    //Insert the html message.
    $message .= $htmlbody;
    $message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";


    //include attachment
    $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; name=\"images.zip\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";

    $message .= $attachment;
    $message .= "/r/n--PHP-mixed-$random_hash--";


    //send the email
    $mail = mail( $to, $subject , $message, $headers );

    echo $mail ? "Mail sent" : "Mail failed";

/r/n 需要读作 \r\n

然后

$headers .= "MIME-Version: 1.0\n\r";

$headers .= "MIME-Version: 1.0\r\n";

后者可能没那么重要,但你可以试试看是否有帮助。

  • /r/n 绝对是个大问题。