1

我正在尝试使用 PHP mail() 函数发送带有附件的电子邮件。电子邮件已送达,但在网络邮件中,文件名显示为“无标题”。使用 Outlook Avast 启发式完全阻止电子邮件。

请指导我克服这个问题。

这是我正在使用的代码

 $recieverAdd = 'r@gamil.com';
 $subject = 'Attachment';
 $attDescription="emailing lessons";
 $filename ='example.pdf';
 $file = '/home/user/example.pdf';
 $file_size = filesize($file);
 $handle = fopen($file, "r");
 $content = fread($handle, $file_size);
 fclose($handle);
 $content = chunk_split(base64_encode($content));
 $uid = md5(uniqid(time()));
 $name = basename($file);

 $from_name = $uname;
 $from_mail = 'admin@gmail.com';
 $replyto  ='noreply@gmail.com';
 $header = "From: $from_name";      
 $header .= "\nMIME-Version: 1.0\n" . 
     "Content-Type: multipart/mixed;\n" . 
     " boundary=\"{$uid}\"";

 $message = "<p><b>Hello </b></p>";
 $messageBody =  "--{$uid}\n" . "Content-Type: text/html; charset=\"iso-8859-1              \"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
 $messageBody .= "--{$uid}\n". "Content-Type: application/octet-stream;\n".
        "name=\"{$filename1}\"\n".
        "Content-Description: {$filename}\n" .
        "Content-Disposition: attachment;\n".
        "filename=\"{$filename}\"; size={$file_size};\n" .
        "Content-Transfer-Encoding: base64\n\n". $content."\n\n". "--{$uid}--\n";

 mail($recieverAdd,$subject,$messageBody,$header)
4

2 回答 2

2
"Content-Type: application/octet-stream;\n".
"name=\"{$filename1}\"\n".

"Content-Disposition: attachment;\n".
"filename=\"{$filename}\"; size={$file_size};\n" .

这些标头的name=...andfilename=...部分被认为是单独的(无效的)标头行,因为您在Content-Type: application/octet-stream;and之后输出换行符Content-Disposition: attachment;,但在这些换行符之后没有空格。为了将标题拆分为多行,每个续行必须以空格开头。

于 2012-10-08T14:01:10.483 回答
1

我想这可能对你有帮助。,

请找到以下示例:

<?php    

    $my_file = "somefile.zip";
    $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
    $my_name = "Olaf Lederer";
    $my_mail = "my@mail.com";
    $my_replyto = "my_reply_to@mail.net";
    $my_subject = "This is a mail with attachment.";
    $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
    mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

?>

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $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";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}
?>
于 2012-10-08T14:29:20.917 回答