【问题标题】:PHP mail attachment is not workingPHP 邮件附件不起作用
【发布时间】:2017-07-10 06:28:17
【问题描述】:

我正在使用 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);

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

【问题讨论】:

  • 我想推荐 PhpMailer (github.com/PHPMailer/PHPMailer) 来完成这个任务。
  • 阅读PHPMailer链接它可能对你有帮助。
  • 我不想使用 PHPMailer() 。我想知道我的代码有什么问题?
  • 您在代码中的哪个位置将文件内容放入要发送的邮件中?你只给出一个文件名(或者实际上是一个完整的路径)。该路径在服务器上有意义,但在接收器的平板电脑上没有。只有像“contract.pdf”这样的文件名才有意义
  • 我建议您查看邮箱中包含附件的电子邮件正文。检查“边界”和“十六进制编码”部分,它们实际上是附件

标签: php email


【解决方案1】:

我推荐你使用 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();   

【讨论】:

【解决方案2】:

你的内容类型应该是

"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";

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

【讨论】:

  • 你认为php.ini的任何设置都会导致这个问题,你能发送非附件邮件吗?
【解决方案3】:

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

$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);

【讨论】:

    【解决方案4】:
    $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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      相关资源
      最近更新 更多