2

我有一个简单的 PHP 联系表 ( Seen Here ),我想向其中添加文件上传选项,以便客户可以附加重要文档并使用 PHP 的邮件功能将其邮寄给我。

该表单本身可以正常工作,但我似乎无法获得正确的代码来上传附件,将其临时存储在服务器上并将其作为电子邮件的一部分发送给我。这是我正在使用的代码:

<?php

if ($_POST['test'] != '') {
    echo 'Unfortunately, by filling out the hidden field, you have been identified as a potential spambot and your message has been terminated.';
} else {

//Validate the name:
if (!empty($_POST['name'])) {
    $name = $_POST['name'];
} else {
    echo "You forgot to enter your name.<br>";
}

//Validate the phone:
if (!empty($_POST['phone'])) {
    $phone = $_POST['phone'];
} else {
    echo "You forgot to enter your phone number.<br>";
}

//Validate the e-mail:
if (!empty($_POST['email'])) {
    $email = $_POST['email'];
} else {
    echo "You forgot to enter your e-mail.<br>";
}

//Validate the message:
if (!empty($_POST['message'])) {
    $message = $_POST['message'];
} else {
    echo "You forgot to enter a message.";
}

if (!empty($_POST['name']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['message'])) {

    // Obtain file upload variables:
    $attachment = $_FILES['attachment']['tmp_name']; 
    $attachment_type = $_FILES['attachment']['type']; 
    $attachment_name = $_FILES['attachment']['name'];

    if (file($attachment)) { 
    // Read the file to be attached ('rb' = read binary):
    $file = fopen($attachment,'rb'); 
    $data = fread($file,filesize($attachment)); 
    fclose($file);

    // Generate a boundary string:
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // Add the headers for a file attachment:
    $headers = "\nMIME-Version: 1.0\n" . 
    "Content-Type: multipart/mixed;\n" . 
    " boundary=\"{$mime_boundary}\"";

    // Add a multipart boundary above the plain message:
    $message = "This is a multi-part message in MIME format.\n\n" . 
    "--{$mime_boundary}\n" . 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $message . "\n\n";

    // Base64 encode the file data:
    $data = chunk_split(base64_encode($data));  

    // Add file attachment to the message:
    $message .= "--{$mime_boundary}\n" . 
    "Content-Type: {$attachment_type};\n" . 
    " name=\"{$attachment_name}\"\n" . 
    //"Content-Disposition: attachment;\n" . 
    //" filename=\"{$attachment_name}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" . 
    "--{$mime_boundary}--\n"; 
    }  

    $body = "$name\n$phone\n$email\n\n$message";
    mail("*@*.com", "Starcrest Escrow, Inc. Website - Real Property Sale", $body, $headers);
    header("Location: confirm.html");
}

}

?>

当我目前运行此脚本时,它会将我转发到确认页面,但似乎根本没有生成任何电子邮件。我究竟做错了什么?

4

1 回答 1

0
<?php

if (isset($_POST['txtEmail'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "xyz@abc.com";
    $email_subject = "Subject";
    $email_from = "abc@xyz.com";
    function died($error)
    {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.";
        echo $error . "<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists

    if (!isset($_POST['txtName']) || !isset($_POST['txtEmail']) || !isset($_POST['txtAddress']) || !isset($_POST['txtContact']) || !isset($_POST['txtUpload'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $name = $_POST['txtName']; // required
    $email = $_POST['txtEmail']; // required
    $address = $_POST['txtAddress']; // required
    $contact = $_POST['txtContact']; // not required
    $upload = $_POST['txtUpload']; // required
    $email_message = "Form Details are below.\n\n";
    function clean_string($string)
    {
        $bad = array(
            "content-type",
            "bcc:",
            "to:",
            "cc:",
            "href"
        );
        return str_replace($bad, "", $string);
    }

    $email_message.= "Full Name: " . clean_string($name) . "\n\n";
    $email_message.= "Address: " . clean_string($address) . "\n\n";
    $email_message.= "Email ID: " . clean_string($email) . "\n\n";
    $email_message.= "Contact No.: " . clean_string($contact) . "\n\n";
    $email_message.= "File: " . clean_string($upload) . "\n\n";

    // create email headers

    $headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>
    <!-- include your own success html here -->

    Thank you for contacting us. We will be in touch with you very soon.

    <?php
}

?>
于 2014-01-31T05:04:22.583 回答