Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单未将图像文件发送到电子邮件_Php_Html_Forms_Email - Fatal编程技术网

Php 表单未将图像文件发送到电子邮件

Php 表单未将图像文件发送到电子邮件,php,html,forms,email,Php,Html,Forms,Email,我的表单未将图像发送到我的电子邮件。在表单上,用户可以上传图像和所有需要的信息,但图像没有收到,我只得到上传的文件名 以下是我在电子邮件中接收邮件的方式: 表格详情如下 评论名称:大卫·麦克斯韦 电子邮件:greatken。richkid@gmail.com 网站:08036240369 照片:David.jpg 评论:荣誉 这是表单的php代码: <?php if(isset($_POST['email'])) { // CHANGE THE TWO LINES BELOW

我的表单未将图像发送到我的电子邮件。在表单上,用户可以上传图像和所有需要的信息,但图像没有收到,我只得到上传的文件名

以下是我在电子邮件中接收邮件的方式:

表格详情如下

评论名称:大卫·麦克斯韦
电子邮件:greatken。richkid@gmail.com
网站:08036240369
照片:David.jpg
评论:荣誉

这是表单的php代码:

<?php
if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to = "kennis_16@yahoo.com";        

    $email_subject = "form submission";

    function died($error) {
        // your error code can go here
        echo "We are sorry, Your comment couldn't be submitted. Please provide solution to error(s) below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['author']) ||
        !isset($_POST['email']) ||
        !isset($_POST['url']) ||
        !isset($_POST['photo']) ||
        !isset($_POST['comment'])
        ) 
        {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $commentName = $_POST['author']; // required
    $email_from = $_POST['email']; // required
    $website = $_POST['url']; // required
    $photo = $_POST['photo'];
    $comments= $_POST['comment']; // required

    $error_message = "";
        $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$commentName)) {
    $error_message .= 'The Name is empty, or does not appear to be valid.<br />';
  }
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'Email Address is missing, or does not appear to be valid.<br />';
  }

        $string_exp = "/^[0-9+().-]+$/";
  if(!preg_match($string_exp,$website)) {
    $error_message .= 'Phone No is omitted, or does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'Comment field cannot be left blank. Please enter your comment.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "commentName: ".clean_string($commentName)."\n";
    $email_message .= "email: ".clean_string($email_from)."\n";
    $email_message .= "website: ".clean_string($website)."\n";
    $email_message .= "photo: ".clean_string($photo)."\n";
    $email_message .= "comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- place your own success html below -->

Your comment will be reviewed before posting within 48hours. <a href='blog_post.html' style='text-decoration:none;color:#ff0099;'> Click Here </a> to return back.
<?php
}
die();
?>

您不能将图像分配给变量并随邮件发送。您需要执行几个步骤

  • 检查图像是否为有效格式(jpg、png…)
  • 然后上传到服务器
  • 然后随信寄去
  • 中,您应该使用
    enctype=“多部分/表单数据”


    这是完整的解决方案。我只是简单地保留了它。以后可以添加其他代码。我已经测试了代码,它工作得非常好

    PHP部分

    <?php
    $error_message = "";
    if(isset($_POST['send'])) {
    
    
    // CHANGE THE TWO LINES BELOW
    $email_to = "kennis_16@yahoo.com";   
    
    
    $email_subject = "form submission";
    
    $commentName = $_POST['author']; // required
    $email_from = $_POST['email']; // required
    $website = $_POST['url']; // required
    //$photo = $_POST['photo'];
    
    $photoname=$_FILES['photo']['name'];
    $tmp_name=$_FILES['photo']['tmp_name'];
    $comments= $_POST['comment']; // required
    
    if($commentName==""){
        echo "Empty author name";
    }else if($email_from==""){
        echo "Empty email";
    }
    else{
     //create a folder name called img
     $destinationpath="img/".$photoname;
     $moveimage=move_uploaded_file($tmp_name,$destinationpath);
    
    
     if(!$moveimage){
      die("Error in uploading");
     }
    
      $message = "Form details below\n\n";
    
    
      $message .= "commentName: ".$commentName."\n";
      $message .= "email: ".$email_from."\n";
      $message .= "website: ".$website."\n";
    
      $message .= "comments: ".$comments."\n";
    
    
        // boundary 
        $semi_rand = md5(time()); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    
    
         // preparing attachments            
            $file = fopen($destinationpath,"rb");
            $f_contents = fread($file,filesize($destinationpath));
            $f_contents = chunk_split(base64_encode($f_contents));
           fclose($file);
            # Get a random 32 bit number using time() as seed.
         $num = md5( time() );
    
        # Define the main headers.
        $header = "From:xyz@somedomain.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 .= "$message\r\n";
       $header .= "--$num\r\n";
    
       # Define the attachment section
       $header .= "Content-Type:  multipart/mixed; ";
       $header .= "name=\"$photoname\"\r\n";
       $header .= "Content-Transfer-Encoding:base64\r\n";
       $header .= "Content-Disposition:attachment; ";
       $header .= "filename=\"$photoname\"\r\n\n";
       $header .= "$f_contents \r\n";
       $header .= "--$num--";
    
          mail($email_to, $email_subject, $message, $header); 
               }
             ?>
    
          <!-- place your own success html below -->
    
       <h2>Your comment will be reviewed before posting within 48hours.
      </h2>
      <?php
       }
    
      ?>
    
    
    您的评论将在48小时内发布前进行审核。
    
    和主体标记内的html部分

      <!-- Comment Form -->
      <div id="respond" class="eleven columns row alpha">
      <h4>Leave a comment</h4>
      <form id="replyform" method="post" action="" enctype="multipart/form-data">
      <div class="input_cm_wrapper">
      <h6 for="commentName">Name *</h6>
      <input type="text" name="author" id="reply_name" class="required"/>
    </div>
    <div class="input_cm_wrapper">
      <h6 for="email">Email Address *</h6>
      <input type="email" name="email" id="reply_email" class="required"/>
    </div>
    <div class="input_cm_wrapper last">
      <h6 for="website">Phone No. *</h6>
      <input type="text" name="url" id="reply_website" class="last"/>
    </div>
    <span class="clear"></span>
      <h6 for="commentsText">
      Message *   <span>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
      Upload Photo. * &nbsp&nbsp&nbsp
      <input type="file" name="photo" id="reply_photo" class="requiredfield"  required />
      </span> </h6>
    <textarea name="comment" id="reply_message" class="requiredfield"></textarea>
    <span class="clear"></span><br>
    <button type="submit"  name="send">Send</button>
    </form>
    </div>
    
    
    留言
    名字*
    电子邮件地址*
    电话号码*
    消息*              
    上传照片。* 
    
    发送

    干杯

    非常感谢@Abdulla。请您花一分钟解释一下,我可以如何将这个新代码添加到我现有的代码中,或者我的位置。提前感谢,我正在等待您的回复。因此,我尝试按照您的指示添加enctype=“multipart/form data”>这是用户提交表单时的响应:很抱歉,您的评论无法提交。请提供以下错误的解决方案。很抱歉,您提交的表单似乎有问题。请返回并修复这些错误。您好,我应该对stackoverflow投票以提高您对所回答问题的排名吗
    <?php
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["photo"]["name"]);
        $status = 1;
        $FileType = pathinfo($target_file,PATHINFO_EXTENSION);
    
    // Allow certain file formats
        if($FileType != "jpg" && $FileType != "png" && $FileType != "jpeg")
        {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $status = 0;
        }
    
    
    // Check if $status is set to 1
        if ($status == 1) 
        {
            // if everything is ok, try to upload file
            if (move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file)) 
            {
                echo "The file has been uploaded.";
            } else 
            {
                echo "Sorry, there was an error uploading your file.";
            }    
    
        } 
        else 
        {
            echo "Sorry, your file was not uploaded.";
        }
    ?>
    
    <?php
    
    $path = "uploads/".$_FILES["photo"]["tmp_name"];
    
    ?>
    
    <img src="<?php echo $path ;?>" alt="">
    
    $header = "From:".$email_from."\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";
    
    <?php
    $error_message = "";
    if(isset($_POST['send'])) {
    
    
    // CHANGE THE TWO LINES BELOW
    $email_to = "kennis_16@yahoo.com";   
    
    
    $email_subject = "form submission";
    
    $commentName = $_POST['author']; // required
    $email_from = $_POST['email']; // required
    $website = $_POST['url']; // required
    //$photo = $_POST['photo'];
    
    $photoname=$_FILES['photo']['name'];
    $tmp_name=$_FILES['photo']['tmp_name'];
    $comments= $_POST['comment']; // required
    
    if($commentName==""){
        echo "Empty author name";
    }else if($email_from==""){
        echo "Empty email";
    }
    else{
     //create a folder name called img
     $destinationpath="img/".$photoname;
     $moveimage=move_uploaded_file($tmp_name,$destinationpath);
    
    
     if(!$moveimage){
      die("Error in uploading");
     }
    
      $message = "Form details below\n\n";
    
    
      $message .= "commentName: ".$commentName."\n";
      $message .= "email: ".$email_from."\n";
      $message .= "website: ".$website."\n";
    
      $message .= "comments: ".$comments."\n";
    
    
        // boundary 
        $semi_rand = md5(time()); 
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    
    
         // preparing attachments            
            $file = fopen($destinationpath,"rb");
            $f_contents = fread($file,filesize($destinationpath));
            $f_contents = chunk_split(base64_encode($f_contents));
           fclose($file);
            # Get a random 32 bit number using time() as seed.
         $num = md5( time() );
    
        # Define the main headers.
        $header = "From:xyz@somedomain.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 .= "$message\r\n";
       $header .= "--$num\r\n";
    
       # Define the attachment section
       $header .= "Content-Type:  multipart/mixed; ";
       $header .= "name=\"$photoname\"\r\n";
       $header .= "Content-Transfer-Encoding:base64\r\n";
       $header .= "Content-Disposition:attachment; ";
       $header .= "filename=\"$photoname\"\r\n\n";
       $header .= "$f_contents \r\n";
       $header .= "--$num--";
    
          mail($email_to, $email_subject, $message, $header); 
               }
             ?>
    
          <!-- place your own success html below -->
    
       <h2>Your comment will be reviewed before posting within 48hours.
      </h2>
      <?php
       }
    
      ?>
    
      <!-- Comment Form -->
      <div id="respond" class="eleven columns row alpha">
      <h4>Leave a comment</h4>
      <form id="replyform" method="post" action="" enctype="multipart/form-data">
      <div class="input_cm_wrapper">
      <h6 for="commentName">Name *</h6>
      <input type="text" name="author" id="reply_name" class="required"/>
    </div>
    <div class="input_cm_wrapper">
      <h6 for="email">Email Address *</h6>
      <input type="email" name="email" id="reply_email" class="required"/>
    </div>
    <div class="input_cm_wrapper last">
      <h6 for="website">Phone No. *</h6>
      <input type="text" name="url" id="reply_website" class="last"/>
    </div>
    <span class="clear"></span>
      <h6 for="commentsText">
      Message *   <span>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
      Upload Photo. * &nbsp&nbsp&nbsp
      <input type="file" name="photo" id="reply_photo" class="requiredfield"  required />
      </span> </h6>
    <textarea name="comment" id="reply_message" class="requiredfield"></textarea>
    <span class="clear"></span><br>
    <button type="submit"  name="send">Send</button>
    </form>
    </div>