通过php邮件函数发送pdf会导致打开错误
我附上了一个pdf文件,但附件没有在邮件中打开。 邮件服务器上出现以下错误 预期:application/pdf.pdf;找到:应用程序/x-empty 我有以下代码片段通过php邮件函数发送pdf会导致打开错误,php,pdf,Php,Pdf,我附上了一个pdf文件,但附件没有在邮件中打开。 邮件服务器上出现以下错误 预期:application/pdf.pdf;找到:应用程序/x-empty 我有以下代码片段 请帮我解决问题检查内容类型是否与应用程序/pdfAlready相同,但相同的错误通过附加纯文本来测试代码file@MangeshSatheIND我尝试了文本文件,但得到了相同的错误我发现的是,有2个内容类型在您的代码。$filename是空的吗? <?php $from='test1@domain.com'
请帮我解决问题检查内容类型是否与应用程序/pdfAlready相同,但相同的错误通过附加纯文本来测试代码file@MangeshSatheIND我尝试了文本文件,但得到了相同的错误我发现的是,有2个内容类型在您的代码。$filename是空的吗?
<?php
$from='test1@domain.com';
$to = 'test@domain.com';
//define the subject of the email
$subject = 'Test email with attachment';
$message='Startendday reports' ;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$file = file_get_contents('file.pdf');
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time())); //unique identifier
$file_name='file.pdf';
//standard mail headers
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$to. "\r\n";
$header .= "MIME-Version: 1.0\r\n";
//declare multiple kinds of email (plain text + attch)
$header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .="This is a multi-part message in MIME format.\r\n";
//plain txt part
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message. "\r\n\r\n";
//attch part
$header .= "--".$uid."\r\n";
$header .= "Content-Type:application/octet-stream; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n\r\n"; //chucked up 64 encoded attch
//sending the mail - message is not here, but in the header in a multi part
if(mail($to, $subject, "", $header)) {
echo "success";
}else {
echo "fail";
}
?>