PDA

View Full Version : send mail to client and business



designer77
07-10-2007, 05:41 PM
Hi, I am a graphic designer. I am by no ways a programmer, but I know a little of html, css and actionscript, my main problem is php. I know nothing about it. I downloaded a form from one of you guys, which I thank very much.
The problem is that I have a code in which when a form is submited all data gets to the business email. I want add a message to the client that submited the data, something like thank you for example. The problem is that I have tried in so many ways and it doesnt work, only for the business works.
If anyone could help me out I would be more than grateful, because Ive spent two days trying to figure out this simple thing. Thank you in advance, and here is the code:

function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

require("config.inc.php");

if ($logging == 1) {
$timestamp = date('Y-m-d');
foreach ($_POST as $key => $string) {
$value = stripslashes($string);
$logmsg .= ucfirst ($key) ." : ". $value . "\n";
}
$fh = fopen("$logpath/$timestamp.txt", "a+") or die( "Error opening file" );
if (!fwrite($fh, $logmsg)) { die( "Error writing to file!" ); }
if (!fwrite($fh, "\n---------------\n\n")) { die( "Error writing to file!" ); }
fclose($fh) or die( "Error closing file!" );
}

session_start();
if(($_SESSION['security_code'] == $_REQUEST['security_code']) && (!empty($_SESSION['security_code'])) ) {
$boundary = md5(date('r', time()));
$msg = "This is a multi-part message in MIME format.\n\n";
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= "$intro\n";
foreach ($_POST as $key => $string) {
$value = stripslashes($string);
if ($value == '') { $badfield = true; $emptykey = $key; }
if ($key != "security_code" && $key != "submit" && $key !="MAX_FILE_SIZE") {
if ($key == "email" && check_email_address($value) == false) { $bademail = true; }
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
if ($key == name) {
$name = $value;
}
if ($key == email) {
$headers = "From: " . $name . " <" . $value . ">\n";
$headers .= "Reply-To: " . $value . "\n";
$headers .= "Return-Path: " . $value . "\n";
$headers .= "Envelope-from: " . $value . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
}
}
if ($attachments == 0) {
$msg .= "--$boundary--\n";
}
if (isset($_FILES['attachment']) && $attachments == 1) {
if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {
$file_upload = true;
$tmp_path = "tmp/" . basename( $_FILES['attachment']['name']);
if(move_uploaded_file($_FILES['attachment']['tmp_name'], $tmp_path)) {
$file = fopen($tmp_path,'rb');
$data = fread($file,filesize($tmp_path));
fclose($file);
}
$attachment = chunk_split(base64_encode($data));
$attachment_name = $_FILES['attachment']['name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_size = $_FILES['attachment']['size'];
$file_extension = strtolower(substr(strrchr($attachment_name,"."),1));
$msg .= "\n--$boundary\n";
$msg .= "Content-Type: $attachment_type; name=\"$attachment_name\"\n";
$msg .= "Content-Transfer-Encoding: base64\n";
$msg .= "Content-Disposition: attachment; filename=$attachment_name\n\n\n";
$msg .= "$attachment\n";

}
}
session_unregister(security_code);
if ($attachments == 1 && $attachment_size > $max_file_size && $file_upload == true) {
header ("Location:$bigfile");
}
else if ($attachments == 1 && !in_array($file_extension, $file_types) && $file_upload == true) {
header ("Location:$badfile");
}
else if ($formval == 1 && $emailval == 1) {
if ($bademail != true && $badfield != true) {
mail($to, $subject, $msg, $headers);
header ("Location:$thanks");
mail($email, "Thank you for your visit",
"Hello $name,\n
Thank you for visiting this site!","FROM:$to");
}
else if ($bademail != true && $badfield == true) {
header ("Location:$emptyfield");
}
else { header ("Location:$invalid"); }
}
else if ($formval != 1 && $emailval == 1) {
if ($bademail != true) {
mail($to, $subject, $msg, $headers);
header ("Location:$thanks");
mail($email, "Thank you for your visit",
"Hello $name,\n
Thank you for visiting this site!","FROM:$to");
}

else { header ("Location:$invalid"); }

}
else if ($formval == 1 && $emailval != 1) {
if ($badfield != true) {
mail($to, $subject, $msg, $headers);
header ("Location:$thanks");
mail($email, "Thank you for your visit",
"Hello $name,\n
Thank you for visiting this site!","FROM:$to");
}
else {
header ("Location:$emptyfield");
}
}

}
else {
session_unregister(security_code);
header ("Location:$wrong");
}
if (file_exists($tmp_path)) {
unlink($tmp_path);
}
?>

designer77
07-10-2007, 05:48 PM
Hi, I am a graphic designer. I am by no ways a programmer, but I know a little of html, css and actionscript, my main problem is php. I know nothing about it. I downloaded a form from one of you guys, which I thank very much.
The problem is that I have a code in which when a form is submited all data gets to the business email. I want add a message to the client that submited the data, something like thank you for example. The problem is that I have tried in so many ways and it doesnt work, only for the business works.
If anyone could help me out I would be more than grateful, because Ive spent two days trying to figure out this simple thing. Thank you in advance, and here is the code:


function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

require("config.inc.php");

if ($logging == 1) {
$timestamp = date('Y-m-d');
foreach ($_POST as $key => $string) {
$value = stripslashes($string);
$logmsg .= ucfirst ($key) ." : ". $value . "\n";
}
$fh = fopen("$logpath/$timestamp.txt", "a+") or die( "Error opening file" );
if (!fwrite($fh, $logmsg)) { die( "Error writing to file!" ); }
if (!fwrite($fh, "\n---------------\n\n")) { die( "Error writing to file!" ); }
fclose($fh) or die( "Error closing file!" );
}

session_start();
if(($_SESSION['security_code'] == $_REQUEST['security_code']) && (!empty($_SESSION['security_code'])) ) {
$boundary = md5(date('r', time()));
$msg = "This is a multi-part message in MIME format.\n\n";
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= "$intro\n";
foreach ($_POST as $key => $string) {
$value = stripslashes($string);
if ($value == '') { $badfield = true; $emptykey = $key; }
if ($key != "security_code" && $key != "submit" && $key !="MAX_FILE_SIZE") {
if ($key == "email" && check_email_address($value) == false) { $bademail = true; }
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
if ($key == name) {
$name = $value;
}
if ($key == email) {
$headers = "From: " . $name . " <" . $value . ">\n";
$headers .= "Reply-To: " . $value . "\n";
$headers .= "Return-Path: " . $value . "\n";
$headers .= "Envelope-from: " . $value . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
}
}
if ($attachments == 0) {
$msg .= "--$boundary--\n";
}
if (isset($_FILES['attachment']) && $attachments == 1) {
if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {
$file_upload = true;
$tmp_path = "tmp/" . basename( $_FILES['attachment']['name']);
if(move_uploaded_file($_FILES['attachment']['tmp_name'], $tmp_path)) {
$file = fopen($tmp_path,'rb');
$data = fread($file,filesize($tmp_path));
fclose($file);
}
$attachment = chunk_split(base64_encode($data));
$attachment_name = $_FILES['attachment']['name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_size = $_FILES['attachment']['size'];
$file_extension = strtolower(substr(strrchr($attachment_name,"."),1));
$msg .= "\n--$boundary\n";
$msg .= "Content-Type: $attachment_type; name=\"$attachment_name\"\n";
$msg .= "Content-Transfer-Encoding: base64\n";
$msg .= "Content-Disposition: attachment; filename=$attachment_name\n\n\n";
$msg .= "$attachment\n";

}
}
session_unregister(security_code);
if ($attachments == 1 && $attachment_size > $max_file_size && $file_upload == true) {
header ("Location:$bigfile");
}
else if ($attachments == 1 && !in_array($file_extension, $file_types) && $file_upload == true) {
header ("Location:$badfile");
}
else if ($formval == 1 && $emailval == 1) {
if ($bademail != true && $badfield != true) {
mail($to, $subject, $msg, $headers);
header ("Location:$thanks");
mail($email, "Thank you for your visit",
"Hello $name,\n
Thank you for visiting this site!","FROM:$to");
}
else if ($bademail != true && $badfield == true) {
header ("Location:$emptyfield");
}
else { header ("Location:$invalid"); }
}
else if ($formval != 1 && $emailval == 1) {
if ($bademail != true) {
mail($to, $subject, $msg, $headers);
header ("Location:$thanks");
mail($email, "Thank you for your visit",
"Hello $name,\n
Thank you for visiting this site!","FROM:$to");
}

else { header ("Location:$invalid"); }

}
else if ($formval == 1 && $emailval != 1) {
if ($badfield != true) {
mail($to, $subject, $msg, $headers);
header ("Location:$thanks");
mail($email, "Thank you for your visit",
"Hello $name,\n
Thank you for visiting this site!","FROM:$to");
}
else {
header ("Location:$emptyfield");
}
}

}
else {
session_unregister(security_code);
header ("Location:$wrong");
}
if (file_exists($tmp_path)) {
unlink($tmp_path);
}
?>

sjlplat
07-10-2007, 05:58 PM
I think I can help you with that. I wrote the script. ;)

I'll write up a new mailer.php file and post it for download here. Shouldn't take long.

designer77
07-10-2007, 06:14 PM
thank you very much sjlplat, and oops, sorry for my same post twice.

sjlplat
07-10-2007, 06:15 PM
Here ya go. Automatic confirmation email sent to anyone who successfully fills out the form. Just replace your mailer.php file with this one and you're ready to go.

If it gives you any trouble, let me know and I'll do what I can to get you up and running. :)

designer77
07-10-2007, 06:18 PM
thank you very much, its glad to find people that helps other.

sjlplat
07-10-2007, 06:21 PM
My pleasure. If you ever have any problems with my scripts, I offer support on my forums. I'm a one-man operation, so the more complicated things can take time but I try to do what I can for everyone that asks.

On a side note: I'm working on a new Mailcode release that will include a copy of the submitted form on the "Thank You" page. I will probably add the option to send confirmation messages as well, but I will provide a switch in the config file so it can be turned on or off.

This script is getting pretty popular, so there's a good chance I will have to work on some sort of admin panel eventually. I'll try to release 2 different builds. Users will have the option of using a MySQL database or a flat file to store settings.

designer77
07-10-2007, 06:28 PM
I tried already and works perfectly!!!!

The next release sounds really interesting, thank you very much again.

sjlplat
07-10-2007, 06:29 PM
Awesome! :)

It's not very often I get it on the first try, even after I test it locally. There's usually some kinda quirk so let me know if you find one. It was a quick 15-minute job, so I probably overlooked something.