简体   繁体   中英

PHP Mail() Failed to send email with attachment

I'm attempting to create a zip of images, then send the email and attach the .zip file to the user. However, the email is not sending. I'm using mail() elsewhere on the server so I know the server isn't blocking this function.

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 that needs to read as \\r\\n

Then

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

to

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

the latter may not be as important, but you can try it to see if it helps.

  • The /r/n is definitely a major issue though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM