简体   繁体   English

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

[英]Form not sending images to email

I have a form that does not send the images to my email. 我的表格无法将图像发送到我的电子邮件中。 On the form the user can upload two images and all the information submits to my email except the images do not. 用户可以在表格上上传两张图片,所有信息都提交给我的电子邮件,除了图片没有。

This is the code for the form: 这是表格的代码:

<div id="stylized" class="myform">
<form id="myform" name="myform" action="bookingform.php" method="post" >
<h1></h1>
<p></p>

<div id="firstname">
<label>Name
<span class="small">Add your name</span>
</label>
<input type="text" name="FirstName" id="FirstName"  />
</div>

<div id="email">
<label>Email
<span class="small">Add a valid email address</span>
</label>
<input type="text" name="Email" id="Email" />
</div>

<div id="phone">
<label>Tel Number
<span class="small">Add phone number</span>
</label>
<input type="text" name="Phone" id="Phone" />
</div>


<label>Address
<span class="small">Add your address</span>
</label>
<input type="text" name="Address" id="Address" />


<h2 style="margin-top:140px;">Event Venue Information</h2>

<div id="dates">
<label>Date of Event
<span class="small">Date of your occasion</span>
</label>
<input type="text" name="datex" id="datex" />
</div>
<div>
<label>Occasion
<span class="small">Type of occasion</span>
</label>
<input type="text" name="type" id="type" />
</div>

<div style="margin-left:315px;">
<label>Venue
<span class="small">Location of event</span>
</label>
<input type="text" name="location" id="location" />
</div>
<div style="float:right; margin-right:25px;">
<label>Services Required
<span class="small">Service needed</span>
</label>
<textarea cols="40" rows="5" name="services">
</textarea>
</div>


<div style="margin-top:-20px;">
<label>Please photo's that you find are useful:</label>
<input type="file" name="image" size="40"
onchange="return validateFileExtension(this)">
<input type="file" name="image" size="40"
onchange="return validateFileExtension(this)">
</div>


<h2 style="margin-top:260px;">Additional Information</h2>

<div style="margin-left:-40px; margin-top:30px;">
<label>How you found us?
<span class="small">Website/DJ's/Friend</span>
</label>
<input type="text" name="found" id="found" />
</div>

<div style="float:right; margin-right:10px;">
<label>Your comments/questions
<span class="small">Any other queries</span>
</label>
<textarea cols="40" rows="5" name="questions">
</textarea>
</div>
<div>
<input class="buttons" style="margin-top:10px; margin-left:110px;" type="submit" 
value="Submit" 
></input>
</div>

<div class="spacer"></div>

</form>
<script language="JavaScript" type="text/javascript"
    xml:space="preserve">//<![CDATA[
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("myform");
  frmvalidator.addValidation("FirstName","req","Please enter your First Name");
  frmvalidator.addValidation("FirstName","maxlen=20",   "Max length for FirstName is 20");
  frmvalidator.addValidation("FirstName","alpha","Alphabetic chars only");



  frmvalidator.addValidation("Email","maxlen=50");
  frmvalidator.addValidation("Email","req");
  frmvalidator.addValidation("Email","email");

  frmvalidator.addValidation("Phone","maxlen=50");
  frmvalidator.addValidation("Phone","numeric");

  frmvalidator.addValidation("Address","maxlen=50");


//]]></script>
</div>

This is the php code: 这是PHP代码:

<?php
$boundary = uniqid('np');

$field_name = $_POST['FirstName'];
$field_email = $_POST['Email'];
$field_phone = $_POST['Phone'];
$field_address = $_POST['Address'];
$field_date = $_POST['datex'];
$field_type = $_POST['type'];
$field_location = $_POST['location'];
$field_services = $_POST['services'];
$field_found = $_POST['found'];
$field_comments = $_POST['questions'];

$mail_to = 'user1@hotmail.co.uk';


$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Address: '.$field_address."\n";
$body_message .= 'Date: '.$field_date."\n";
$body_message .= 'Type of Occasion: '.$field_type."\n";
$body_message .= 'Location: '.$field_location."\n";
$body_message .= 'Services required: '.$field_services."\n";
$body_message .= 'How they found us: '.$field_found."\n";
$body_message .= 'Comments - Questions: '.$field_comments;

$headers = "MIME-Version: 1.0\r\n";
$headers = 'From: '.$Email."\r\n";

 $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";


$message = "This is a MIME encoded message."; 

 $message .= "\r\n\r\n--" . $boundary . "\r\n";
 $message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
 $message .= "This is the text/plain version.";

 $message .= "\r\n\r\n--" . $boundary . "\r\n";
 $message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
 $message .= "This is the <b>text/html</b> version.";

 $message .= "\r\n\r\n--" . $boundary . "--";



$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'http://www.google.co.uk';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to user1@hotmail.co.uk');
        window.location = 'url';
    </script>
<?php
}
?>

I would be very grateful if someone could show me how to allow the images to send with the form. 如果有人可以向我展示如何允许图像随表格一起发送,我将不胜感激。

Thanks 谢谢

Edit: 编辑:

I have tried to add this as a seperate script but it gives an error which keeps saying unexpected } this is the code: 我试图将其添加为单独的脚本,但它给出了一个错误,该错误不断说出意外}这是代码:

//reads the name of the file the user submitted for uploading
    $image=$_FILES['image']['name'];
    //if it is not empty
    if ($image) 
    {
    //get the original name of the file from the clients machine
        $filename = stripslashes($_FILES['image']['name']);
    //get the extension of the file in a lower case format
        $extension = getExtension($filename);
        $extension = strtolower($extension);
    //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
    //otherwise we will do more tests
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
        {
        //print error message
            echo '<h1>Unknown extension!</h1>';
            $errors=1;
        }
        else
        {
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server
 $size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
    echo '<h1>You have exceeded the size limit!</h1>';
    $errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
    echo '<h1>Copy unsuccessfull!</h1>';
    $errors=1;
}}}}

//If no errors registred, print the success message
 if(isset($_POST['Submit']) && !$errors) 
 {
    echo "<h1>File Uploaded Successfully! Try again!</h1>";
 }

 ?>

You have not added enctype for form: 您尚未为表单添加enctype:

<form enctype="multipart/form-data" id="myform" name="myform" action="bookingform.php" method="post" >

This might be a problem, hope it helps 这可能是个问题,希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM