PHP电子邮件格式问题

时间:2010-08-24 23:14:46

标签: php email

我通过.swf文件将表单数据发送到这个PHP页面。(见下文)当发送电子邮件时,如果有附件,邮件正文将不会出现,但如果没有附件,则看起来很好。我知道所有变量名都是正确的,因为所有数据都是从swf文件传递的。我设置电子邮件的方式是否有某种方式使其以这种方式工作?请告诉我。

<?php

   $to=$_POST['toEmail'];
   $subject=$_POST['subject'];
   $body = $_POST['messaggio'];
   $nome = $_POST['nome'];
   $email = $_POST['email'];
   $attach = $_POST['attach'];
   $headers = "From: $nome<" . $email . ">\n";

if ($attach == 1) {
   $tmp_name = $_FILES['Filedata']['tmp_name'];
   $type = $_FILES['Filedata']['type'];
   $name = $_FILES['Filedata']['name'];

      if(is_uploaded_file($tmp_name)){
         $file = fopen($tmp_name,'rb');
         $data = fread($file,filesize($tmp_name));
         fclose($file);
         $data = chunk_split(base64_encode($data));

         $headers .= "Reply-To: <" . $email . ">\n";
         $headers .= "MIME-Version: 1.0\n";
         $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDARY_main_message\"\n";
         $headers .= "X-Sender: $to <" . $to . ">\n";
         $headers .= "Return-Path: <" . $email . ">\n";
         $headers .= "This is a multi-part message in MIME format.\n";
         $headers .= "------=MIME_BOUNDARY_main_message \n";
         $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDARY_message_parts\"\n";

         $message  = "------=MIME_BOUNDARY_message_parts\n";
         $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
         $message .= "Content-Transfer-Encoding: quoted-printable\n";
         $message .= "\n";       
         $message .= $body . "\n";
         $message .= "\n";
         $message .= "------=MIME_BOUNDARY_message_parts--\n";
         $message .= "\n";       
         $message .= "------=MIME_BOUNDARY_main_message\n";
         $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $name . "\"\n";
         $message .= "Content-Transfer-Encoding: base64\n";
         $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $name . "\"\n\n";
         $message .= $data;
         $message .= "\n";
         $message .= "------=MIME_BOUNDARY_main_message--\n";


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

      }
   } else {
          if(mail($to, $subject, $body, $headers)) {
             echo "ok=1";
      }
}
?>

1 个答案:

答案 0 :(得分:0)

构建MIME消息,特别是附件,很痛苦。你最好使用像PHPMailer这样的东西,它会自动为你处理整个业务......你只需要提供内容。

除此之外,你正在将附件啜饮到记忆中。他们多大?你超过了脚本的memory_limit吗?