表格不发送图像到电子邮件

时间:2011-11-29 00:43:37

标签: php javascript html forms

我的表单不会将图片发送到我的电子邮箱。在表单上,​​用户可以上传两个图像,并且所有信息都提交到我的电子邮件,但图像不会。

这是表格的代码:

<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>

这是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
}
?>

如果有人能告诉我如何允许图片与表单一起发送,我将非常感激。

由于

编辑:

我曾尝试将其添加为单独的脚本,但它会出现一个错误,该错误一直在说意外}这是代码:

//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>";
 }

 ?>

1 个答案:

答案 0 :(得分:0)

您尚未为表单添加enctype:

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

这可能是个问题,希望有所帮助