2015-10-02 96 views
0

我有以下代碼,它可以正確地發送附件的正確大小的電子郵件,但附件以非'noname'形式出現,沒有擴展名。如果我在下載後手動重命名文件,它不起作用。該文件是一個mp4視頻。帶有附件的PHP郵件,附件文件是noname

<?php 
$htmlbody = " Your Mail Contant Here.... You can use html tags here..."; 
$to = "[email protected]"; //Recipient Email Address 
$subject = "Test email with attachment"; //Email Subject 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
$random_hash = md5(date('r', time())); 
$headers .= "\r\nContent-Type: multipart/mixed; 
boundary=\"PHP-mixed-".$random_hash."\""; 
// Set your file path here 
$attachment = chunk_split(base64_encode(file_get_contents('test.mp4'))); 


//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: video/vnd.uvvu.mp4 
name=\"testing.mp4\"\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"; 
?> 
+0

回去PHPMailer的,它不是這樣 –

+0

每PHPMailer的演示一個更好的主意行不通 – prismspecs

+1

最好還是調試在mail()中內置的php是非常有限的 –

回答

0

只需修改此行,其他的一切保持不變:

$filename= "myvideo.mp4"; 

//include attachment 
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: video/vnd.uvvu.mp4 
name=\"testing.mp4\"\r\n"."Content-Transfer-Encoding: 
base64\r\n"."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";