PHP 带有附件的电子邮件 – fopen() 期望参数 1 是有效路径

PHP email with ATTACHMENT – fopen() expects parameter 1 to be a valid path

我很难附加任何文件并通过电子邮件发送 PHP 表格。

我正在尝试实现一个简单的文件附加功能,将附件通过电子邮件发送到指定的电子邮件地址。我不想使用 PHPmailer,因为我发现这么小的功能真的很复杂。

我从这里得到了表格 – http://forums.phpfreaks.com/topic/280373-uploading-file-and-sending-as-attachment-in-e-mail/

请帮忙!

我遇到了这些错误:

**Warning: fopen() expects parameter 1 to be a valid path, resource given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 36
Warning: filesize() expects parameter 1 to be a valid path, resource given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 37
Warning: fread() expects parameter 1 to be resource, boolean given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 37
Warning: fclose() expects parameter 1 to be resource, boolean given in /homepages/34/d565332578/htdocs/email-test/send2.php on line 38** 

HTML:

<!DOCTYPE html>
<html>
<body>

    <form name="contactform" method="post" action="send2.php" enctype="multipart/form-data">
        <table width="100%" border="0" align="center">
        <tr>
         <td valign="middle" id="ta">
         <label for="title">Title *</label>
         </td>
         <td valign="middle" id="ta">
         <select name="title">
         <option value="0">Title</option>
         <option value="1">Mr.</option>
         <option value="2">Ms.</option>
         <option value="3">Mrs.</option>
         </select></td></tr><tr><td id="ta">
          <label for="first_name">First Name *</label>
         </td>
         <td valign="middle" id="ta">
          <input  type="text" name="first_name" maxlength="50" size="30" required="required">
         </td>
        </tr>
        <tr>
         <td valign="middle" id="ta">
          <label for="last_name">Last Name *</label>
         </td>
         <td valign="middle" id="ta">
          <input  type="text" name="last_name" maxlength="50" size="30" required="required">
         </td>
        </tr>
        <tr>
         <td valign="middle" id="ta">
          <label for="email">Email Address *</label>
         </td>
         <td valign="middle" id="ta">
          <input  type="text" name="email" maxlength="80" size="30" required="required">
         </td>
        </tr>
        <tr>
         <td valign="middle" id="ta">
          <label for="telephone">Telephone Number *</label>
         </td>
         <td valign="middle" id="ta">
          <input  type="text" name="telephone" maxlength="30" size="30" required="required">
         </td>
        </tr>
        <tr>
         <td valign="middle" id="ta">
          <label for="comments">Details</label>
         </td>
         <td valign="middle" id="ta">
          <textarea  name="comments" maxlength="100000" cols="25" rows="6"></textarea>
         </td>
        </tr>
        <tr>
        <td valign="middle" id="ta">
         <label for="file">Or upload a file (only word, excel or pdf)</label>
        </td>
        <td valign="middle" id="ta">
        <input type="file" name="file">
        </td>
        </tr>
        <tr>
         <td colspan="2" style="text-align:center" id="ta">
          <input type="submit" value="Submit">
         </td>
        </tr>
        </table>
        </form>

    </body>
    </html>

PHP 代码:

<?php
            error_reporting(E_ALL); ini_set('display_errors', 1);
            $title = array('Title', 'Mr.', 'Ms.', 'Mrs.');
             $selected_key = $_POST['title'];
             $selected_val = $title[$_POST['title']]; 

                $first_name = $_POST['first_name']; // required
                $last_name = $_POST['last_name']; // required
                $email_from = $_POST['email']; // required
                $telephone = $_POST['telephone']; // not required
                $comments = $_POST['comments']; // required

                if(($selected_key==0))
               echo "<script> alert('Please enter your title')</script>";
             function clean_string($string) {
                  $bad = array("content-type","bcc:","to:","cc:","href");
                  return str_replace($bad,"",$string);
                }
              $email_message = "";
             $email_message .="Title: ".$selected_val."\n";
             $email_message .= "First Name: ".clean_string($first_name)."\n";
                $email_message .= "Last Name: ".clean_string($last_name)."\n";
                $email_message .= "Email: ".clean_string($email_from)."\n";
                $email_message .= "Telephone: ".clean_string($telephone)."\n";
                $email_message .= "Comments: ".clean_string($comments)."\n";

            $email_to = "bhavesh.k.2007@gmail.com"; // The email you are sending to (example)
            //$email_from = "sendfrom@email.com"; // The email you are sending from (example)
            $email_subject = "subject line"; // The Subject of the email
            $email_txt = "text body of message"; // Message that the email has in it
            $destination=$_FILES["file"]["name"];


            /**here you open the file, the $fileatt variable become a resource**/
            $fileatt = fopen($_FILES["file"]["tmp_name"],'r'); // Path to the file (example)
            move_uploaded_file($_FILES["file"]["name"], $destination);

            $fileatt_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; // File Type
            $fileatt_name = "Details.docx"; // Filename that will be used for the file as the attachment

            /**here you are just read the $fileatt opened**/
            $data = fread($fileatt ,filesize($fileatt));

            fclose($file);

            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            $headers="From: $email_from"; // Who the email is from (example)
            $headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";
            $email_message .= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
            $email_message .= "\n\n";
            $data = chunk_split(base64_encode($data));
            $email_message .= "--{$mime_boundary}\n" .
            "Content-Type: {$fileatt_type};\n" .
            " name=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $data . "\n\n" .
            "--{$mime_boundary}--\n";

            mail($email_to,$email_subject,$email_message,$headers);
?>
 move_uploaded_file($_FILES["file"]["name"], $destination);
 $fileatt = fopen($_FILES["file"]["name"],'r');

必须是:

move_uploaded_file($_FILES["file"]["tmp_name"], $destination);
$fileatt = fopen($destination,'r');

因为 $_FILES["file"]["name"] 只是文件的 "basename" 您需要上传的文件(在您的 /tmp 中)

试试这个:

$fileatt = fopen($_FILES['file']['tmp_name'], 'r');

因为它是临时目录中的原始文件

错误在这里:

/**
  here you open the file, the $fileatt variable become a resource
**/
$fileatt = fopen($_FILES["file"]["name"],'r'); // Path to the file (example)

$fileatt_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; // File Type
$fileatt_name = "Details.docx"; // Filename that will be used for the file as the attachment

/**
  here you are trying open a resource but not a file,
  cuz $fileatt is a resource from $_FILES['file']['name']
**/
$file = fopen($fileatt,'r');
$data = fread($file,filesize($fileatt));
fclose($file);

试试这个:

/**
  here you open the file, the $fileatt variable become a resource
**/
$fileatt = fopen($_FILES["file"]["name"],'r'); // Path to the file (example)

$fileatt_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; // File Type
$fileatt_name = "Details.docx"; // Filename that will be used for the file as the attachment

/**
  here you are just read the $fileatt opened
**/
$data = fread($fileatt ,filesize($fileatt));

fclose($fileatt);

我建议您打开 $_FILES['file']['tmp_name'] 而不是 $_FILES['file']['name'],并在调用 move_uploaded_file 函数之前打开它。

试着告诉我什么