Php 向访问者发送一份联系人表单的副本

Php 向访问者发送一份联系人表单的副本,php,contact-form,Php,Contact Form,我想让表单将一份副本发送到访问者输入的电子邮件地址'label'=>'email' 3个php文件正在处理整个事件,并且作为休耕: 这是处理HTML输入的PHP表单 <?php require_once('form_process.php'); $form = array( 'subject' => 'Contact Form', 'heading' => 'Submission', 'success_redirect' => '',

我想让表单将一份副本发送到访问者输入的电子邮件地址
'label'=>'email'

3个php文件正在处理整个事件,并且作为休耕:

这是处理HTML输入的PHP表单

<?php 

require_once('form_process.php');

$form = array(
    'subject' => 'Contact Form',
    'heading' => 'Submission',
    'success_redirect' => '',
    'resources' => array(
        'checkbox_checked' => 'Checked',
        'checkbox_unchecked' => 'Unchecked',
        'submitted_from' => 'Form submitted from website: %s',
        'submitted_by' => 'Visitor IP address: %s',
        'too_many_submissions' => 'Too many recent submissions from this IP',
        'failed_to_send_email' => 'Failed to send email',
        'invalid_reCAPTCHA_private_key' => 'Invalid reCAPTCHA private key.',
        'invalid_field_type' => 'Unknown field type \'%s\'.',
        'invalid_form_config' => 'Field \'%s\' has an invalid configuration.',
        'unknown_method' => 'Unknown server request method'
    ),
    'email' => array(
        'from' => 'info@myurl.com',
        'to' => 'info@myurl.com'
    ),
    'fields' => array(
        'custom_U8149' => array(
            'order' => 1,
            'type' => 'string',
            'label' => 'Name',
            'required' => true,
            'errors' => array(
                'required' => 'Field \'Name\' is required.'
            )
        ),
        'Email' => array(
            'order' => 2,
            'type' => 'email',
            'label' => 'Email',
            'required' => true,
            'errors' => array(
                'required' => 'Field \'Email\' is required.',
                'format' => 'Field \'Email\' has an invalid email.'
            )
        ),
        'custom_U8139' => array(
            'order' => 3,
            'type' => 'string',
            'label' => 'Message',
            'required' => false,
            'errors' => array(
            )
        )
    )
);

process_form($form);
?>

这是表单_process.php

<?php 

require_once('form_throttle.php');

function process_form($form) {
    if ($_SERVER['REQUEST_METHOD'] != 'POST')
        die(get_form_error_response($form['resources']['unknown_method']));

    if (formthrottle_too_many_submissions($_SERVER['REMOTE_ADDR']))
        die(get_form_error_response($form['resources']['too_many_submissions']));

    // will die() if there are any errors
    check_required_fields($form);

    // will die() if there is a send email problem
    email_form_submission($form);
}

function get_form_error_response($error) {
    return get_form_response(false, array('error' => $error));
}

function get_form_response($success, $data) {
    if (!is_array($data))
        die('data must be array');

    $status = array();
    $status[$success ? 'FormResponse' : 'MusePHPFormResponse'] = array_merge(array('success' => $success), $data);

    return json_serialize($status);
}

function check_required_fields($form) {
    $errors = array();

    foreach ($form['fields'] as $field => $properties) {
        if (!$properties['required'])
            continue;

        if (!array_key_exists($field, $_REQUEST) || empty($_REQUEST[$field]))
            array_push($errors, array('field' => $field, 'message' => $properties['errors']['required']));
        else if (!check_field_value_format($form, $field, $properties))
            array_push($errors, array('field' => $field, 'message' => $properties['errors']['format']));
    }

    if (!empty($errors))
        die(get_form_error_response(array('fields' => $errors)));
}

function check_field_value_format($form, $field, $properties) {
    $value = get_form_field_value($field, $properties, $form['resources'], false);

    switch($properties['type']) {
        case 'checkbox':
        case 'string':
        case 'captcha':
            // no format to validate for those fields
            return true;

        case 'checkboxgroup':
            if (!array_key_exists('optionItems', $properties))
                die(get_form_error_response(sprintf($form['resources']['invalid_form_config'], $properties['label'])));

            // If the value received is not an array, treat it as invalid format
            if (!isset($value))
                return false;

            // Check each option to see if it is a valid value
            foreach($value as $checkboxValue) {
                if (!in_array($checkboxValue, $properties['optionItems']))
                    return false;
            }

            return true;

        case 'radiogroup':
            if (!array_key_exists('optionItems', $properties))
                die(get_form_error_response(sprintf($form['resources']['invalid_form_config'], $properties['label'])));

            //check list of real radio values
            return in_array($value, $properties['optionItems']);

        case 'recaptcha':
            if (!array_key_exists('recaptcha', $form) || !array_key_exists('private_key', $form['recaptcha']) || empty($form['recaptcha']['private_key']))
                die(get_form_error_response($form['resources']['invalid_reCAPTCHA_private_key']));
            $resp = recaptcha_check_answer($form['recaptcha']['private_key'], $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
            return $resp->is_valid;

        case 'email':
            return 1 == preg_match('/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i', $value);

        case 'radio': // never validate the format of a single radio element; only the group gets validated
        default:
            die(get_form_error_response(sprintf($form['resources']['invalid_field_type'], $properties['type'])));
    }
}

function email_form_submission($form) {
    if(!defined('PHP_EOL'))
        define('PHP_EOL', '\r\n');

    $form_email = ((array_key_exists('Email', $_REQUEST) && !empty($_REQUEST['Email'])) ? cleanup_email($_REQUEST['Email']) : '');

    $to = $form['email']['to'];
    $subject = $form['subject'];
    $message = get_email_body($subject, $form['heading'], $form['fields'], $form['resources']);
    $headers = get_email_headers($to, $form_email); 

    $sent = @mail($to, $subject, $message, $headers);

    if(!$sent)
        die(get_form_error_response($form['resources']['failed_to_send_email']));

    $success_data = array(
        'redirect' => $form['success_redirect']
    );

    echo get_form_response(true, $success_data);
}

function get_email_headers($to_email, $form_email) {
    $headers = 'From: ' . $to_email . PHP_EOL;
    $headers .= 'Reply-To: ' . $form_email . PHP_EOL;
    $headers .= 'X-Mailer: Adobe Muse CC 2015.0.2.310 with PHP' . PHP_EOL;
    $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;

    return $headers;
}

function get_email_body($subject, $heading, $fields, $resources) {
    $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    $message .= '<html xmlns="http://www.w3.org/1999/xhtml">';
    $message .= '<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><title>' . encode_for_form($subject) . '</title></head>';
    $message .= '<body style="background-color: #ffffff; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: 18px; font-family: helvetica, arial, verdana, sans-serif;">';
    $message .= '<h2 style="background-color: #eeeeee;">' . $heading . '</h2>';
    $message .= '<table cellspacing="0" cellpadding="0" width="100%" style="background-color: #ffffff;">'; 

    $sorted_fields = array();

    foreach ($fields as $field => $properties) {
        // Skip reCAPTCHA from email submission
        if ('recaptcha' == $properties['type'])
            continue;

        array_push($sorted_fields, array('field' => $field, 'properties' => $properties));
    }

    // sort fields
    usort($sorted_fields, 'field_comparer');

    foreach ($sorted_fields as $field_wrapper)
        $message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>' . encode_for_form($field_wrapper['properties']['label']) . ':</b></td><td>' . get_form_field_value($field_wrapper['field'], $field_wrapper['properties'], $resources, true) . '</td></tr>';

    $message .= '</table>';
    $message .= '<br/><br/>';
    $message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME'])) . '</div>';
    $message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_by'], encode_for_form($_SERVER['REMOTE_ADDR'])) . '</div>';
    $message .= '</body></html>';

    return cleanup_message($message);
}

function field_comparer($field1, $field2) {
    if ($field1['properties']['order'] == $field2['properties']['order'])
        return 0;

    return (($field1['properties']['order'] < $field2['properties']['order']) ? -1 : 1);
}

function is_assoc_array($arr) {
    if (!is_array($arr))
        return false;

    $keys = array_keys($arr);
    foreach (array_keys($arr) as $key)
        if (is_string($key)) return true;

    return false;
}

function json_serialize($data) {

    if (is_assoc_array($data)) {
        $json = array();

        foreach ($data as $key => $value)
            array_push($json, '"' . $key . '": ' . json_serialize($value));

        return '{' . implode(', ', $json) . '}';
    }

    if (is_array($data)) {
        $json = array();

        foreach ($data as $value)
            array_push($json, json_serialize($value));

        return '[' . implode(', ', $json) . ']';
    }

    if (is_int($data) || is_float($data))
        return $data;

    if (is_bool($data))
        return $data ? 'true' : 'false';

    return '"' . encode_for_json($data) . '"';
}

function encode_for_json($value) {
    return preg_replace(array('/([\'"\\t\\\\])/i', '/\\r/i', '/\\n/i'), array('\\\\$1', '\\r', '\\n'), $value);
}

function encode_for_form($text) {
    $text = stripslashes($text);
    return htmlentities($text, ENT_QUOTES, 'UTF-8');// need ENT_QUOTES or webpro.js jQuery.parseJSON fails
}

function get_form_field_value($field, $properties, $resources, $forOutput) {
    $value = $_REQUEST[$field];

    switch($properties['type']) {
        case 'checkbox':
            return (($value == '1' || $value == 'true') ? $resources['checkbox_checked'] : $resources['checkbox_unchecked']);

        case 'checkboxgroup':
            if (!is_array($value))
                return NULL;

            $outputValue = array();

            foreach ($value as $checkboxValue)
                array_push($outputValue, $forOutput ? encode_for_form($checkboxValue) : stripslashes($checkboxValue));

            if ($forOutput)
                $outputValue = implode(', ', $outputValue);

            return $outputValue;

        case 'radiogroup':
            return ($forOutput ? encode_for_form($value) : stripslashes($value));

        case 'string':
        case 'captcha':
        case 'recaptcha':
        case 'email':
            return encode_for_form($value);

        case 'radio': // never validate the format of a single radio element; only the group gets validated
        default:
            die(get_form_error_response(sprintf($resources['invalid_field_type'], $properties['type'])));
    }
}

function cleanup_email($email) {
    $email = encode_for_form($email);
    $email = preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $email);
    return $email;
}

function cleanup_message($message) {
    $message = wordwrap($message, 70, "\r\n");
    return $message;
}
?>
$properties){
//从电子邮件提交中跳过reCAPTCHA
if('recaptcha'=$properties['type']))
继续;
数组推送($sorted_fields,array('field'=>$field,'properties'=>$properties));
}
//排序字段
usort($sorted_fields,'field_comparer');
foreach($field\u包装的已排序字段)
$message.=''。为表单编码($field\u wrapper['properties']['label']):'。获取表单字段值($field\u wrapper['field'],$field\u wrapper['properties'],$resources,true)。“”;
$message.='';
$message.='

'; $message.=''。sprintf($resources['submitted_from'],将_编码为_表单($_SERVER['SERVER_NAME']))。'; $message.=''。sprintf($resources['submitted\u by'],对表单进行编码($\u SERVER['REMOTE\u ADDR']))。'; $message.=''; 返回清除消息($message); } 函数字段\比较器($field1,$field2){ 如果($field1['properties']['order']==$field2['properties']['order'])) 返回0; 返回($field1['properties']['order']<$field2['properties']['order'])?-1:1; } 函数是数组($arr){ 如果(!is_数组($arr)) 返回false; $keys=数组_键($arr); foreach(数组_键($arr)作为$key) if(is_string($key))返回true; 返回false; } 函数json_serialize($data){ 如果(是数组($data)){ $json=array(); foreach($key=>$value形式的数据) 数组_push($json,“.”$key.“:”.json_serialize($value)); 返回“{”。内爆(“,”,$json)。“}”; } if(is_数组($data)){ $json=array(); foreach(数据为$value) array_push($json,json_serialize($value)); 返回“[”。内爆(“,”,$json)。”]; } if(is_int($data)| is_float($data)) 返回$data; 如果(数据) 返回$data?'true':'false'; 返回''.encode''u for''u json($data.'')'; } 函数encode_for_json($value){ 返回preg\u replace(数组('/([\''“\\t\\\\])/i','/\\r/i','/\\n/i'),数组('\\\$1','\\r','\\n'),$value); } 函数对表单进行编码($text){ $text=条带斜杠($text); 返回htmlentities($text,ENT_引号,'UTF-8');//需要ENT_引号或webpro.js jQuery.parseJSON失败 } 函数get\u form\u field\u value($field、$properties、$resources、$forOutput){ $value=$_请求[$field]; 开关($properties['type'])){ 案例“复选框”: 返回(($value='1'| |$value=='true')?$resources['checkbox_checked']:$resources['checkbox_checked']); 案例“checkboxgroup”: 如果(!是_数组($value)) 返回NULL; $outputValue=array(); foreach($checkboxValue的值) 数组_push($outputValue,$forOutput?对_表单进行编码($checkboxValue):stripslashes($checkboxValue)); 如果($forOutput) $outputValue=内爆(“,”,$outputValue); 返回$outputValue; 病例“放射组”: return($forOutput?对表单编码($value):stripslashes($value)); 大小写“string”: “验证码”案例: “recaptcha”案例: “电子邮件”案例: 返回编码形式的形式($value); case'radio'://从不验证单个radio元素的格式;只验证组 违约: die(获取表单错误响应(sprintf($resources['invalid\u field\u type'],$properties['type'])); } } 功能清理电子邮件($email){ $email=对表格进行编码($email); $email=preg|u replace('=((| 0x0A/%0A | 0x0D/%0D |\\n |\\r)\S)。*=i',null,$email); 返回$email; } 函数清除消息($message){ $message=wordwrap($message,70,“\r\n”); 返回$message; } ?>
这是表单_throttle.php

<?php 

function formthrottle_check()
{
    if (!is_writable('.'))
    {
        return '8';
    }

    try
    {
        if (in_array("sqlite",PDO::getAvailableDrivers(),TRUE))
        {
            $db = new PDO('sqlite:muse-throttle-db.sqlite3');
            if ( file_exists('muse-throttle-db') )
            {
                unlink('muse-throttle-db');
            }
        }
        else if (function_exists("sqlite_open")) 
        {
            $db = new PDO('sqlite2:muse-throttle-db');
            if ( file_exists('muse-throttle-db.sqlite3') )
            {
                unlink('muse-throttle-db.sqlite3');
            }
        }
    }
    catch( PDOException $Exception ) {
        return '9';
    }

    $retCode ='5';
    if ($db) 
    {
        $res = $db->query("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Submission_History';");
        if (!$res or $res->fetchColumn() == 0)
        {
            $created = $db->exec("CREATE TABLE Submission_History (IP VARCHAR(39), Submission_Date TIMESTAMP)");

            if($created == 0)
            {
                $created = $db->exec("INSERT INTO Submission_History (IP,Submission_Date) VALUES ('256.256.256.256', DATETIME('now'))");
            }

            if ($created != 1)
            {
                $retCode = '2';
            }
        }
        if($retCode == '5')
        {
            $res = $db->query("SELECT COUNT(1) FROM Submission_History;");
            if ($res && $res->fetchColumn() > 0)
            {
                $retCode = '0';
            }
            else
                $retCode = '3';
        }

        // Close file db connection
        $db = null;
    } 
    else
        $retCode = '4';

    return $retCode;
}   

function formthrottle_too_many_submissions($ip)
{
    $tooManySubmissions = false;

    try
    {
        if (in_array("sqlite",PDO::getAvailableDrivers(),TRUE))
        {
            $db = new PDO('sqlite:muse-throttle-db.sqlite3');
        }
        else if (function_exists("sqlite_open")) 
        {
            $db = new PDO('sqlite2:muse-throttle-db');
        }
    }
    catch( PDOException $Exception ) {
        return $tooManySubmissions;
    }

    if ($db) 
    {
        $res = $db->query("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Submission_History';");
        if (!$res or $res->fetchColumn() == 0)
        {
            $db->exec("CREATE TABLE Submission_History (IP VARCHAR(39), Submission_Date TIMESTAMP)");
        }
        $db->exec("DELETE FROM Submission_History WHERE Submission_Date < DATETIME('now','-2 hours')");

        $stmt = $db->prepare("INSERT INTO Submission_History (IP,Submission_Date) VALUES (:ip, DATETIME('now'))");
        $stmt->bindParam(':ip', $ip);
        $stmt->execute();
        $stmt->closeCursor();

        $stmt = $db->prepare("SELECT COUNT(1) FROM Submission_History WHERE IP = :ip;");
        $stmt->bindParam(':ip', $ip);
        $stmt->execute();
        if ($stmt->fetchColumn() > 25) 
            $tooManySubmissions = true;
        // Close file db connection
        $db = null;
    }
    return $tooManySubmissions;
}
?>

你希望有人为你编写代码吗!!如果没有其他方法可以按照我希望的方式将现有的东西组合起来,我会考虑的。谢谢