带附件的PHP邮件,附件文件是noname

时间:2015-10-02 01:42:53

标签: php email smtp

我有以下代码,它正确地发送了一封包含正确大小的附件的电子邮件,但附件以“noname”形式出现,没有扩展名。如果我在下载后手动重命名文件不起作用。该文件是mp4视频。

<?php
$htmlbody = " Your Mail Contant Here.... You can use html tags here...";
$to = "blah@gmail.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: test@mysite.com\r\nReply-To: test@mysite.com";
$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";
?>

1 个答案:

答案 0 :(得分: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";