PHP邮件附件无法正常工作

时间:2017-07-10 06:28:18

标签: php email

我使用php邮件功能发送附件,但它不起作用。这是我的代码:

     $headers = array(
        "Mime-Version: 1.0",
        "Content-Type: text/html; charset=charset=UTF-8",
        "From: Test <myemail>",
        "Content-Disposition: attachment; filename=\"" . $_SERVER['DOCUMENT_ROOT']."/project_name/test.txt" . "\"\r\n"; // For Attachment
    );    
    $headers = join("\r\n", $headers);

    mail($to, $subject, $body, $headers);

电子邮件已成功发送且文件已附加,但内容已隐藏。

4 个答案:

答案 0 :(得分:0)

我建议你使用PHPMailer。

使用PHPMailer:

  • 从此处下载PHPMailer脚本:http://github.com/PHPMailer/PHPMailer
  • 提取存档并将脚本的文件夹复制到项目中方便的位置。
  • 包含主脚本文件 - require_once('path / to / file / class.phpmailer.php');

现在用作:

$email = new PHPMailer();
$email->From      = 'you@example.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->Body      = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'nameOfFile.pdf' );
return $email->Send();   

答案 1 :(得分:0)

您的内容类型应为

"Content-Type:  multipart/mixed; charset=charset=UTF-8",

请参阅this answer,这可以指导您解决问题。

我也有一个很大的疑问,如果它是正确的

 "Content-Disposition: attachment; filename=\"" . $_SERVER['DOCUMENT_ROOT']."/project_name/test.txt" . "\"\r\n"; // For Attachment

单引号和双引号的组合似乎不太好。你能把它改成

吗?
 "Content-Disposition: attachment; filename=\" . $_SERVER['DOCUMENT_ROOT'] . "/project_name/test.txt" . "\"\r\n";

或尝试使用单引号,因为您没有解析任何变量。

答案 2 :(得分:0)

如果您想编写自己的代码而不想实现邮件库。请这样做

$handle = fopen($file_name, "r");
$content = fread($handle, filesize($file_name));
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

$boundary = md5("foobar");
$message = "I have some message";
//header

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

//plain text 
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= chunk_split(base64_encode($message)); 

//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
$body .= $encoded_content;

mail($to, $subject, $body, $headers);

答案 3 :(得分:0)

$to = "YOUR EMAIL ID";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}