...

View Full Version : php form



lupes
06-22-2006, 01:13 PM
i have this php form, and once the form is submitted, i want it to send to 2 email addresses instead of just one.

Ive tried separating with commas, semi-colons and adding the next email address.


I even tried adding the 'cc', but i obviously did it wrong cos it didnt work.

Can someone please help me with this.

Here's the code


<?php
class SimpleContact {

var $state; // 0 = fail, 1 = ready, 2 = attempted injection attack, 3 = mail sent
var $errorMsg; // human readable version of $this->state with specific detail of error's cause

var $_to;
var $_from;
var $_subject;
var $_msg;

function _isEmail($email) {
if (preg_match("#^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$#", $email))
{ return true; }
else
{ return false; }
}

function _isInjection($text) {
$text = strtolower($text);
if (preg_match('#(content\s*-\s*disposition)|(bcc\:)|(cc\:)|(content\s*-\s*transfer\s*-\s*encoding)|(mime\s*-\s*version)|(multipart\s*/\s*mixed)|(multipart\s*/\s*alternative)|(multipart\s*/\s*related)|(reply\s*-\s*to)|(x\s*-\s*mailer)|(x\s*-\s*sender)|(x\s*-\s*uidl)#is',$text))
{ return true; }
else
{ return false;}
}

function init($to, $from, $name, $subject, $msg) {
// start with least expensive process, end with regex madness.
if( empty($name) ) {
$this->state = 0;
$this->errorMsg = 'Please fill in your name';
return false;
}
if( empty($subject) ) {
$this->state = 0;
$this->errorMsg = 'All mail must contain a subject';
return false;
}
if( empty($msg) ) {
$this->state = 0;
$this->errorMsg = 'All mail must contain a message body';
return false;
}
if( empty($from) || !$this->_isEmail($from) ) {
$this->state = 0;
$this->errorMsg = 'Invalid sender email address supplied';
return false;
}
if( empty($to) || !$this->_isEmail($to) ) {
$this->state = 0;
$this->errorMsg = 'Invalid recipient email address supplied';
return false;
}
if( $this->_isInjection($to) || $this->_isInjection($name) || $this->_isInjection($from) || $this->_isInjection($subject) || $this->_isInjection($msg)) {
$this->state = 2;
$this->errorMsg = 'This message has been identified as an email injection attempt';
return false;
}
$this->_to = $to;
$this->_from = $from;
$this->_name = $name;
$this->_subject = $subject;
$this->_msg = $msg;

$this->state = 1;
return true;
}

function sendMail() {
if($this->state !== 1) {
trigger_error('SimpleMail Object is not initialized or contains invalid data - Mail sending cancelled', E_USER_WARNING);
} else {
$header = "Return-Path: {$this->_from}\n";
$header .= "X-Sender: {$this->_from}\n";
$header .= "From: '{$this->_name}' <{$this->_from}>\n";
$header .= "MIME-Version: 1.0\n";
$header .= 'X-Mailer: PHP/'. phpversion();

ini_set(sendmail_from, $this->_from); // stops gmail from putting (unknown sender) in From field
$sent = mail($this->_to, $this->_subject, $this->_msg, $header);
ini_restore( sendmail_from );
if (!$sent) {
trigger_error('SimpleContact failed due to php mail()', E_USER_WARNING);
} else {
$this->state = 3;
}
}
}
}


// bot attacks may miss hidden input elements
if (isset($_POST['confirm'])) {

$to = 'email@email.com'; // administrator or comma deliminated group of administrators
$name = $_POST['name'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['message'];

$fin = FALSE; // is the entire user process complete?
$userOutput = ''; // what the user will see at the end


$mailer = new SimpleContact;
$mailReady = $mailer->init($to, $from, $name, $subject, $msg);

//ensure input is good
if(!$mailReady) {
$userOutput = $mailer->errorMsg;
} else {
$mailer->sendMail();
if($mailer->state !== 3) { //something went wrong, mail() probably failed :|
$userOutput = $mailer->errorMsg;
} else {
$userOutput = "Hi {$_POST['name']}, thanks for your message";
$fin = TRUE;
}
}


}
?>

Digger3000
06-22-2006, 02:40 PM
I don't know how to professionals would do it, but I would do this.


$to = 'email@email.com'; // administrator or comma deliminated group of administrators
$to=explode(",", $to);
$name = $_POST['name'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['message'];

$fin = FALSE; // is the entire user process complete?
$userOutput = ''; // what the user will see at the end


$mailer = new SimpleContact;
foreach($to as $key => $value) {
$mailReady = $mailer->init($value, $from, $name, $subject, $msg);
}

lupes
06-22-2006, 04:06 PM
i tried what u gave, but it just said 'invalid recipient email address'



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum