im sure there is a simple answer to this, but its not something i’ve see posted anywhere.
i have a php from in a html page, i got it from a PHP book, its well scripted and secure.
in the form the user submits some data and nomimates friends to send a link.
i wish to collect the data and send it to my email address. and i want the friends to receive a pre-written email that contains only some of the information that was collected.
i have successfully completed this in flash by sending the variables to two separate php files. Can this is done in html? on clicking the send button the variables are sent to two separate processing php files? or can it be done within one php file? either way is good for me.
thanks for the help. i attached the code below. the code is all contained in the same page. i separated the code into that which sits above the HTML and the form itself
<?php
include('includes/title.inc.php');
include('includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
nukeMagicQuotes();
}
// process the email
if (array_key_exists('send', $_POST)) {
$to = 'info@website.com'; // use your own email address
$subject = 'Feedback from Contact Form';
// list expected fields
$expected = array('name', 'email', 'phone', 'comments', 'howhear');
// set required fields
$required = array('name', 'email', 'comments', 'howhear');
// create empty array for any missing fields
$missing = array();
// assume that there is nothing suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';
// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
// check the $_POST array and any sub-arrays for suspect content
isSuspect($_POST, $pattern, $suspect);
if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}
// validate the email address
if (!empty($email)) {
// regex to ensure no illegal characters in email address
$checkEmail = '/^[^@]+@[^\s
\'";,@%]+$/';
// reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
array_push($missing, 'email');
}
}
// go ahead only if not suspect and all required fields OK
if (!$suspect && empty($missing)) {
// build the message
$message = "Name: $name
";
$message .= "Email: $email
";
$message .= "Phone: $phone
";
$message .= "Comments: $comments
";
$message .= "How did you hear of us?: $howhear";
// limit line length to 70 characters
$message = wordwrap($message, 70);
// create additional headers
$additionalHeaders = 'From: Web Contact Form<info@website.com>';
if (!empty($email)) {
$additionalHeaders .= "
Reply-To: $email";
}
// send it
$mailSent = mail($to, $subject, $message, $additionalHeaders);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
?>
Basically all you need to do is append a new message and a new call to mail()…
Set the new user mail body message (anywhere in the file)
$usermessage = 'Thanks for registering!';
$usersubject = 'Reg info';
And then call a new mail() (I’m appending this into your code;
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
mail($email, $usersubject, $usermessage, $additionalHeaders);
unset($missing);
}