联系表格上传文件但不附加到电子邮件php

时间:2014-12-09 16:21:12

标签: php html email

我正在尝试上传我的联系表单,我已经按照W3schools http://www.w3schools.com/php/php_file_upload.asp上的代码进行了操作,但我是php的新手,我不确定如何将其添加到电子邮件中。我相信我错过了简单的事情。电子邮件发送发现电子邮件没有附件,文件上传到服务器正常。


HTML


<form action="send_config.php" method="post" novalidate="novalidate" id="register-form" enctype="multipart/form-data">

                    <label>Company Name :<span>*</span></label><br />
                    <input name="company_name" type="text" placeholder="Joes Cleaner">
                    <br />

                    <label>Ref :<span>*</span></label><br />
                    <input name="ref" type="text" placeholder="H123">
                    <br />

                    <label>Website :<span>*</span></label><br />
                    <input name="website" type="text" placeholder="www.joescleaners.com">
                    <br />

                    <label>Email :<span>*</span></label><br />
                    <input name="email" type="email" placeholder="Joescleaners@gmail.com">
                    <br />

                    <label>Telephone :<span>*</span></label><br />
                    <input name="tel" type="text" placeholder="07123456789">
                    <br />

                    <label>Message :<span>*</span></label><br />
                    <input name="message" id="message" type="text" size="500">
                    <br />

                    <label>Upload :</label>
                    <input type="file" name="fileToUpload" id="fileToUpload">
                    <input type="submit" value="Upload Image" name="submit">
                    <br />

                    <input  type="reset" value="Reset" />
                    <input name="submit" type="submit" value="Submit" />

                </form>

PHP


<?php
// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);


$to = 'test@gmail.com';
$subject = 'Website Submission';
$company_name = $_POST['company_name'];
$ref = $_POST['ref'];
$website = $_POST['website'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}


$body = <<<EMAIL

<html>

<p><h3>Email from website.</h3></p>

<p><strong>Company Name:</strong> $company_name</p>
<p><strong>Ref:</strong> $ref</p>
<p><strong>Website:</strong> $website</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Tel:</strong> $tel</p>
<p><strong>Message:</strong> $message</p>

</html>

EMAIL;

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: test' . "\r\n";
$headers .= 'From: <noreply@email.co.uk>' . "\r\n";
//$headers .= 'Cc: noreply@example.com' . "\r\n";
//$headers .= 'Bcc: noreply@example.com' . "\r\n";


if ($_POST['submit']){
mail($to, $subject, $body, $headers);
echo 'Message Successfully Sent.';
} else {

    die('Error Email Not Sent');
} 
?>

0 个答案:

没有答案