2009-10-16 68 views
0

我在窗体上有一个输入,允许用户浏览到文件位置。这个想法是,他们可以将简历附加到他们准备提交的应用程序中。使用php附加文件到电子邮件

<label class="description" for="element_5">Upload a File </label> 
     <div> 
      <input id="element_5" name="element_5" class="element file" type="file"/> 

对于我的文字精密组件和下拉列表我一直在使用的线沿线的东西:

$experince = $_POST["experince"]; 

但我不想路径字符串我想实际的文件。我如何获取文件本身以及如何将其附加到电子邮件中?

此外,有没有简单的方法来限制附件到.DOC/.PDF?

回答

3

我最近做了类似的事情。这不是微不足道的。

您的电子邮件必须是多部分MIME消息。您将读取您的文件,对其进行编码(使用base64),然后将其放入您的电子邮件字符串中的正确位置。

这看起来像一个体面的教程(我希望我之前就找到): http://www.texelate.co.uk/blog/send-email-attachment-with-php/

但是请注意,本教程有一些逃避的问题:

$message . "“nn"; 

应该是:

$message . "\n\n"; 

您还可以查看到Mail_Mime PEAR包帮助: http://pear.php.net/package/Mail_Mime

-1

这是我到目前为止,但它不是工作

<input id="resumeup" name="resumeup" type="file"/> 

<?php $_FILES = $_POST["resumeup"]; 

      if ((($_FILES["file"]["type"] == "image/gif") 
      || ($_FILES["file"]["type"] == "image/jpeg") 
      || ($_FILES["file"]["type"] == "image/pjpeg")) 
      && ($_FILES["file"]["size"] < 20000)) 
      { 
       if ($_FILES["file"]["error"] > 0) 
       { 
        echo "Error: " . $_FILES["file"]["error"] . "<br />"; 
       } 
       else 
       { 
        echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
        echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
        echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
        echo "Stored in: " . $_FILES["file"]["tmp_name"]; 
       } 
      } ?> 
0

这是我做了什么发送邮件附加换货。有些代码是我的,其中一些代码来自http://php.net/manual/en/function.mail.php。此代码已经过尝试和测试,因此它应该可以工作。还看看是否安装了sendmail。如果在linux的ubuntu系统上试试sudo apt-get install sendmail来安装它。 有关此代码的最佳部分是它适用于多个文件上载。

文件名:index.php文件

<?php 
    if($_POST || $_FILES) 
    { 
      // email fields: to, from, subject, and so on 
      // Here 
      $from = "[email protected]"; 
      $to = "[email protected]_else.com"; 
      $subject = "Mail with Attachment"; 
      $message = "This is the message body and to it I will append the attachments."; 
      $headers = "From: $from"; 

      // boundary 
      $semi_rand = md5(time()); 
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

      // headers for attachment 
      $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

      // multipart boundary 
      $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
      fixFilesArray($_FILES['attachment']); 
      foreach ($_FILES['attachment'] as $position => $file) 
      { 
        // should output array with indices name, type, tmp_name, error, size 
        $message .= "--{$mime_boundary}\n"; 
        $fp  = @fopen($file['tmp_name'],"rb"); 
        $data = @fread($fp,filesize($file['tmp_name'])); 
        @fclose($fp); 
       $data = chunk_split(base64_encode($data)); 
       $message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      } 
      $message .= "--{$mime_boundary}--"; 
      $returnpath = "-f" . $from; 
      $ok = @mail($to, $subject, $message, $headers, $returnpath); 
      if($ok){ return 1; } else { return 0; } 
    } 
    //This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important 

    function fixFilesArray(&$files) 
    { 
      $names = array('name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1); 

      foreach ($files as $key => $part) { 
        // only deal with valid keys and multiple files 
        $key = (string) $key; 
        if (isset($names[$key]) && is_array($part)) { 
          foreach ($part as $position => $value) { 
            $files[$position][$key] = $value; 
          } 
          // remove old key reference 
          unset($files[$key]); 
        } 
      } 
    } 
    ?> 
    <html> 
     <body> 
      <form method="POST" action="index.php" enctype="multipart/form-data"> 
       <input type="file" name="attachment[]"><br/> 
       <input type="submit"> 
      </form> 
     </body> 
    </html> 

通过我愿意为使用sendmail,因为它是慢了一点道歉的方式。我会尝试发布更好的解决方案。