将简历作为附件发送到电子邮件

时间:2013-04-26 14:04:16

标签: php html file

我有以下表格:

 <form method="POST" name="email" action="sendemail.php" enctype="multipart/form-data">
    <table border=0 cellPadding=5 cellSpacing=10 width=100%>
        <tr>
            <td><label for='position'>Position:</label></td>
            <td><input type=text name=position class=position value="" READONLY size=35 /></td>
        </tr>
        <tr>
            <td><label for='uploaded_file'>Resume:</label></td>
            <td><input type="file" name="uploaded_file"></td>
        </tr>
        <tr>
            <td colSpan=2 align=center><input class=subbtn type="submit" value="Submit" name='submit' /></td>
        </tr>
        <tr>
            <td colSpan=2><hr /></td>
        </tr>
        <tr>
            <td colSpan=2 align=center style="padding-top: 5px;"><input type=button value="Close Window" class="close" /></td>
        </tr>
    </table>
</form>

以及以下的sendemail.php:

<?php

$uf = $_POST['uploaded_file'];

  $to = "myemail@yahoo.com";
  $subject = "This is subject";
  $message = "This is test message.";
  # Open a file
  $file = fopen( $uf, "r" );
 if( $file == false )
  {
     echo "Error in opening file";
     exit();
  }
  # Read the file into a variable
  $size = filesize($file);
  $content = fread( $file, $size);

  # encode the data for safe transit
  # and insert \r\n after every 76 chars.
  $encoded_content = chunk_split( base64_encode($content));

  # Get a random 32 bit number using time() as seed.
  $num = md5( time() );

  # Define the main headers.
  $header = "From:myemail@yahoo.com\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num\r\n";
  $header .= "--$num\r\n";

  # Define the message section
  $header .= "Content-Type: text/plain\r\n";
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";
  $header .= "$message\r\n";
  $header .= "--$num\r\n";

  # Define the attachment section
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name=\"test.txt\"\r\n";
  $header .= "Content-Transfer-Encoding:base64\r\n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename=\"test.txt\"\r\n\n";
  $header .= "$encoded_content\r\n";
  $header .= "--$num--";

  # Send email now
  $retval = mail ( $to, $subject, "", $header );
  if( $retval == true )
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>

当我尝试使用表单发送简历时,出现以下错误:

  

打开文件时出错

有人可以告诉我问题在哪里以及如何解决。

3 个答案:

答案 0 :(得分:2)

上传的文件不在$ _POST数组中,它们位于$ _FILES数组中。

所以使用$ _FILES ['uploaded_file'] ['tmp_name']而不是$ _POST ['uploaded_file']

有关$ _FILES的进一步实施,请参阅http://www.php.net/manual/en/features.file-upload.post-method.php

上传文件时请注意安全问题,并使用PHP中的is_uploaded_file()函数验证文件。

答案 1 :(得分:1)

<?php

$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$subject = $_POST['fieldSubject'];

/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];

/* Start of headers */
$headers = "From: $fromName";

if (file($tmpName)) {
  /* Reading file ('rb' = read binary)  */
  $file = fopen($tmpName,'rb');
  $data = fread($file,filesize($tmpName));
  fclose($file);

  /* a boundary string */
  $randomVal = md5(time());
  $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

  /* Header for File Attachment */
  $headers .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n" ;
  $headers .= " boundary=\"{$mimeBoundary}\"";

  /* Multipart Boundary above message */
  $message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mimeBoundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
  $message . "\n\n";

  /* Encoding file data */
  $data = chunk_split(base64_encode($data));

  /* Adding attchment-file to message*/
  $message .= "--{$mimeBoundary}\n" .
  "Content-Type: {$fileType};\n" .
  " name=\"{$fileName}\"\n" .
  "Content-Transfer-Encoding: base64\n\n" .
  $data . "\n\n" .
  "--{$mimeBoundary}--\n";
}

$flgchk = mail ("$to", "$subject", "$message", "$headers");

if($flgchk){
  echo "<script language='javascript' type='text/javascript'>
        alert('Success');
        window.location = 'index.php';
    </script>";
 }
else{
  echo "<script language='javascript' type='text/javascript'>
        alert('Message failed');
        window.location = 'index.php';
    </script>";
}
?>

答案 2 :(得分:-1)

使用===代替==

if( $file === false )

 if( !$file )