[英]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
絕對是主要問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.