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

时间:2015-02-06 16:29:09

标签: php zip ziparchive

我正在尝试创建图片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";

1 个答案:

答案 0 :(得分:2)

/r/n需要阅读为\r\n

然后

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

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

后者可能不那么重要,但您可以尝试看它是否有帮助。

  • /r/n绝对是一个主要问题。
相关问题