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

Posted

技术标签:

【中文标题】带有文件上传附件的 PHP 联系表【英文标题】:PHP Contact Form w/ File Upload Attachment 【发布时间】:2013-11-11 00:06:40 【问题描述】:

我有一个简单的 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_randx"; 

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




?>

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

【问题讨论】:

您是否收到任何服务器错误? $data 是否填充了附件的内容? 我意识到我没有在我的 HTML 表单中指向正确的脚本 - 哎呀!现在已经解决了,它会发送包含所有信息的电子邮件。但是,附件在邮件底部显示为文本数据。例如,一个 PDF 文件会变成很多行“JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k”。知道发生了什么吗?谢谢! 是的!这正在发生:$file = fopen($attachment,'rb'); & $data = chunk_split(base64_encode($data)); 我很抱歉如此密集,但为什么这些行导致它显示为这样,而不是 PDF 附件?我错过了代码中的重要一步吗? 不要尝试自己构建 MIME 附件...这很复杂:***.com/questions/10606558/… 【参考方案1】:
<?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


?>

【讨论】:

...您是在问如何将此代码添加到 OP 的问题中吗?? batsticks用于短代码,四个空格用于多行,见***.com/editing-help。但是请阅读指南How to Answer too ;) (基本上:解释你的代码,如果你从某个地方复制它,请提及源代码)

以上是关于带有文件上传附件的 PHP 联系表的主要内容,如果未能解决你的问题,请参考以下文章

PHP不发送带有附件的邮件

多上传附件联系表单

php 表单自定义表单文件上传附件多附件表单

PHP PHP电子邮件附带上传文件的附件

表单不会使用 PHP 将带有文件上传的文本输入发布到 SQL 表

php 文件上传附件邮件