带有文件上传附件的 PHP 联系表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19698822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:53:01  来源:igfitidea点击:

PHP Contact Form w/ File Upload Attachment

phpfile-uploadmime

提问by 712Jefferson

I have a simple PHP contact form (Seen Here) that I want to add a file upload option to, so clients can attach an important document and mail it to me using PHP's mail function.

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

The form works fine on its own, but I can't seem to get the code right for uploading the attachment, storing it temporarily on the server and sending it to me as part of the e-mail. Here is the code I'm using:

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

<?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");
}

}

?>

When I run this script presently, it forwards me to the confirmation page, but no e-mail appears to be generated at all. What am I doing wrong?

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

回答by Tezzo

<?php

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

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "[email protected]";
    $email_subject = "Subject";
    $email_from = "[email protected]";
    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
}

?>