表单未将图像文件发送到电子邮件 [英] form not sending image file to email

查看:68
本文介绍了表单未将图像文件发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My Form is not sending image to my email. On the form the user can upload image and all the required information, but the image is not received, i only get the file name that was uploaded.

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

This is how the mail is received in my email:

下面的表格详细信息

注释名称:David Maxwell
电子邮件:greatken.richkid@gmail.com
网站:08036240369
照片:David.jpg
评论:荣誉

commentName: David Maxwell
email: greatken.richkid@gmail.com
website: 08036240369
photo: David.jpg
comments: Kudos

这是表单的php代码:

This is the php code for the form:

<?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();
?>

这是HTML代码:

<html>
<body>

<!-- Comment Form -->
    <div id="respond" class="eleven columns row alpha">
        <h4>Leave a comment</h4>
        <form id="replyform" method="post" action="commentformprocessor.php">
        <div class="input_cm_wrapper">
            <h6 for="commentName">Name *</h6>
            <input type="text" name="author" id="reply_name" class="requiredfield"/>
        </div>
        <div class="input_cm_wrapper">  
            <h6 for="email">Email Address *</h6>            
            <input type="email" name="email" id="reply_email" class="requiredfield"/>
        </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 style=""/>
            </span> </h6>
            <textarea name="comment" id="reply_message" class="requiredfield"></textarea>

            <span class="clear"></span><br>
            <button type="submit" onclick="alert ('Thank you for your comment. Please  make sure all required fields are completed.')" name="send">Send</button>

        </form>
    </div> 
</body>
</html>

推荐答案

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

You cant assign image to variable and send it with mail. You need to do several steps

  1. 检查图像是否为有效格式(jpg,png ...)
  2. 然后将其上传到服务器
  3. 然后发送带有附件的邮件

,在<form>中,您应该使用enctype="multipart/form-data"

<form id="" method="post" action="" enctype="multipart/form-data">

检查图片是否为有效格式

Check image is valid format

<?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="">

将此内容包含在电子邮件标题中

Include this in email header

$header = "From:".$email_from."\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";

这篇关于表单未将图像文件发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆