code_len = 4; // length of verification code $this->debug = false; $this->debug_die = false; $this->website = ''; $this->body_header = ''; $this->body_footer = ''; // $this->standard_subject = 'Online mailformulier'; // html $this->html_on = false; $this->html_style = ''; $this->send_confirmation = false; $this->confirmation_content = ''; $this->sender = false; // set later $this->form_sender = false; // set later $this->use_this_sender = false; // can be set with a valid emailadres () $this->confirmation_mail = "Bedankt voor uw bericht.\n\n\n"; $this->confirmation_mail_sender = 'no-reply'; $this->mail_subject = 'bericht'; $this->mail_subject_confirm = 'verzendbevestiging'; $this->show_errors = false; // debug: show errors when processing form $this->show_post = false; // debug: show $_POST values $this->check_code = true; // html tags and styles $this->style_prefix_table_wrapper = 'align="center" style="border-collapse:collapse; width:800px;" '; $this->style_prefix_table = 'style="border-collapse:collapse;" '; $this->style_prefix_td = 'valign="top" style="font-family:Verdana; font-size:12px; padding:2px 8px 4px 0;" '; $this->style_prefix_td_label = 'valign="top" style="font-family:Verdana; font-size:12px; padding:2px 8px 4px 0; color:#909090" '; $this->style_prefix_p = 'style="font-family:Verdana; font-size:12px; color:#a0a0a0;" '; // if (!isset($_SESSION['code'])) $_SESSION['code'] = strtoupper(substr(md5(time()),0, $this->code_len)); } /* -------------------------------------------------------------------------------- Running this form -------------------------------------------------------------------------------- */ function get_html_run_form(){ if ($this->in_form()) { if ($errors = $this->validate()) { $html = $this->get_hml_mailform($errors, false); } else { if ($this->process()) $html = $this->get_hml_mailform_success(); else $html = $this->get_hml_mailform_failure(); } } else { $html = $this->get_hml_mailform('', true); } return $html; } function get_hml_mailform() { // extend this class & function return; } function get_hml_mailform_success() { // extend this class & function return; } function get_hml_mailform_failure() { // extend this class & function return; } function get_html_table_wrapper_td_footer() { // extend this class & function return; } function get_html_table_wrapper_td_header() { // extend this class & function return; } function additional_subject() { // extend this class & function // add something extra to the email sent, like an order number return; } function send_additional_info() { // extend this class & function return; } /* -------------------------------------------------------------------------------- Form handling -------------------------------------------------------------------------------- */ function in_form(){ if (array_key_exists('submit',$_POST)) // ??? return true; else return false; } function add_mandatory_field($field){ if (!isset($_POST[$field]) or empty($_POST[$field])) return "\n"; } function add_recipients($recipients){ // add 1 or more recipients to the mailform separate using a comma (,) $this->recipients = explode(',', $recipients); // all recipients in an array $domain = explode('@',$this->recipients[0]); $this->domain = $domain[1]; // get domain of first email address } function add_body_header($text){ $this->body_header .= $text; } function add_body_footer($text){ $this->body_footer .= $text; } function add_body_footer_tracking(){ // call this to add this footer to each mail $this->body_footer .= "\n\n\n\n\n". '

style_prefix_p.">\n". "
\n". "IP: {$_SERVER['REMOTE_ADDR']}
\n". "Host: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])."\n". "

\n"; } function add_body_footer_server_time(){ // call this to add this footer to each mail $this->body_footer .= "Webservertijd: ".date("Y-m-d H:i")."\n"; } function add_html_style($style=''){ // obsolete if ($style) { $this->html_style .= "$style"; } else { $this->html_style = "body, p, table { font-family:Verdana; font-size:12px; }\n". "table { border:0px solid; border-collapse:collapse; }\n". "td { padding:4px 10px 4px 0; vertical-align:top; }\n". "td.label { color:#909090; }\n"; } } /* -------------------------------------------------------------------------------- Engine -------------------------------------------------------------------------------- */ function validate() { $this->validate_class(); // array for error messages $error = array(); // trim all values foreach($_POST as $key=> $value) $_POST[$key] = trim($value); // put array keys in an array $this->field_names = array_keys($_POST); // all keys in lowercase foreach($this->field_names as $key=>$value) $this->field_names[$key] = strtolower($value); // check mandatory fields foreach($_POST AS $key => $value) { $positie = strpos($key, '*'); if($positie > 0) { if (empty($_POST[$key])) { $field_name = strtolower(substr($key, 0, $positie)); // veldnaam zonder sterretje $error[$field_name] = "Het veld '".$field_name."' is verplicht en moet worden ingevuld."; } } } // code verification if ($this->check_code) { $field_name = 'code*'; $field_name2 = 'code'; // remove star if (in_array($field_name, $this->field_names)) { $code_input = trim(strtoupper($_POST[$field_name])); if ($code_input and ($_SESSION['code'] != $code_input)) { $error[$field_name2] = 'Ingevoerde code is niet correct'; } else { // error message will be given later because field is mandatory } } else { die('AE - veld voor invoer bevestigingscode bestaat niet'); } } // look for other sender in $_POST if (in_array("mail", $this->field_names)) { $field_name="mail"; $this->form_sender = $_POST['mail']; } elseif (in_array("email", $this->field_names)) { $field_name="email"; $this->form_sender = $_POST['email']; } elseif (in_array("e-mail", $this->field_names)) { $field_name="e-mail";$this->form_sender = $_POST['e-mail']; } elseif (in_array("mail*", $this->field_names)) { $field_name="mail"; $this->form_sender = $_POST['mail*']; } elseif (in_array("email*", $this->field_names)) { $field_name="email"; $this->form_sender = $_POST['email*']; } elseif (in_array("e-mail*", $this->field_names)) { $field_name="e-mail";$this->form_sender = $_POST['e-mail*']; } else { $this->form_sender = "noreply@".$this->website; $bedankt_mail = FALSE; } if (!validate_email($this->form_sender)) $error[$field_name] = "Het opgegeven emailadres is niet geldig."; if ($this->use_this_sender) $this->sender = $this->use_this_sender; else $this->sender = $this->form_sender; return $error; } function validate_class(){ // validate for fatal errors if (!isset($this->recipients[0]) or !validate_email($this->recipients[0])) { die("AE - Klasse [c_mail_form] heeft geen mail ontvangers [recipients]."); } if (!validate_domain($this->website)) { die("AE - Klasse [c_mail_form] heeft geen geldig website domein."); } } function process_custom(){ // extend this class & function return; } function process() { $this->validate_class(); $this->process_custom(); if ($this->mail_subject) { $subject = $this->mail_subject.$this->additional_subject(); } else { if (in_array("onderwerp", $this->field_names)) { $subject = $_POST['onderwerp']; } elseif (in_array("subject",$this->field_names)) { $subject = $_POST['subject']; } elseif (in_array("onderwerp*",$this->field_names)) { $subject = $_POST['onderwerp*']; } elseif (in_array("subject*",$this->field_names)) { $subject = $_POST['subject*']; } else { $subject = $this->standard_subject; } } // construct headers (part 1) $headers = "MIME-Version: 1.0\r\n"; $headers .= "From: {$this->sender}\r\n"; $headers .= ($this->html_on) ? "Content-Type: text/html; charset=iso-8859-1\r\n" : "Content-Type: text/plain; charset=iso-8859-1\r\n"; $content = $this->construct_content(); // construct the body !!! changes body_confirmation also $body = $this->construct_body($content); foreach ($this->recipients as $recipient) { $recipient = trim($recipient); if ($this->debug) $result = true; else $result = mail($recipient, $subject, $body, $headers); $this->process_mail_result($result, $recipient, $subject, $body, $headers); // custom if (!$result) return false; } // if ($this->send_confirmation) { // construct headers (part 2 - different sender) $headers = "MIME-Version: 1.0\r\n"; if (@$this->website_email) $headers .= "From: {$this->website_email}\r\n"; else $headers .= "From: {$this->sender}\r\n"; $headers .= ($this->html_on) ? "Content-Type: text/html; charset=iso-8859-1\r\n" : "Content-Type: text/plain; charset=iso-8859-1\r\n"; // re-construct the body $body = $this->construct_body($this->confirmation_content.$content); $result = mail($this->form_sender, $this->mail_subject_confirm, $body, $headers); if (!$result) return false; } // processing succes unset($_SESSION['code']); return true; } function process_mail_result($result, $recipient, $subject, $body, $headers){ // extend this class if necessary } function construct_body($content){ $s = $this->body_header. $content. $this->body_footer; if ($this->html_on) $s = $this->make_html_email($s); return $s; } function construct_content(){ // add $_POST values in mail body, except the submit buttons $content = ''; if ($this->html_on) $content .= 'style_prefix_table.">\n"; foreach($_POST as $key => $value) { // skip fields with a ~ if((strpos($key, '~')) === false) { // strip * if (strpos($key, '*')>0) $key = strtolower(substr($key, 0, strpos($key, '*'))); // do not add buttons if (strtolower($key) != "submit") { /* step 1 */ $value = htmlspecialchars($value); // do not allow tags /* step 2 */ if ($this->html_on) $value = nl2br($value); //
for textarea's if ($this->html_on) { $content .= "\n". "\n". "\n". "\n"; } else { $content .= ucfirst(strtolower($key)).": ".$value."\n"; } } } } if ($this->html_on) $content .= "
style_prefix_td_label."class=\"label\">".ucfirst(strtolower($key)).": style_prefix_td.">".$value."
\n"; $content .= $this->send_additional_info(); return $content; } function make_html_email($text){ /* gmail totally removes any