Log in

View Full Version : How to generate a email to user from a form



rangerjoe
12-11-2008, 03:43 PM
Hi guys, I'm trying to get my hands around php, so any help would be really appreciated. THANKS IN ADVANCE FOR YOUR HELP...

I have a form set up that works and will email me the user's input field info, but I want to automatically generate/send a thank you email to the user's email address once the user submits the form.

Based on what I've named things in my form, what code do I need to place in there, and where do I place it in order to get an email auto-generated to the user upon submission?


Here is my current php code for the form that I'm using:


<?php // Option $Explicit; ?>
<?php // ------------- SET DATABASE CONNECTION ------------------- ?>
<?php include ("variables.php") ?>
<?php // ------------- SET SQL ------------------- ?>
<?php
$contact_table="contacts";
mysql_connect($host,$user,$password);
mysql_select_db($database);

function mktime_from_mysqldate($mysqldate) {
$break = explode(" ", $mysqldate);
$datebreak = explode("-", $break[0]);
$time = explode(":", $break[1]);
return mktime($time[0],$time[1],$time[2],$datebreak[1],$datebreak[2],$datebreak[0]);
}
function sbad($text)
{
$text= addslashes($text);
$text= mysql_real_escape_string($text);
$text= strip_tags($text);
return $text;
}



if($_POST["posted"]==1) {
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
$salutation=sbad($_POST["Salutation"]);
$email=sbad($_POST["email"]);
$fname=sbad($_POST["First"]);
$lname=sbad($_POST["Last"]);
$title=sbad($_POST["Job_Title"]);
$company=sbad($_POST["Company"]);
$address_1=sbad($_POST["Address_1"]);
$address_2=sbad($_POST["Address_2"]);
$city=sbad($_POST["City"]);
$state=sbad($_POST["State"]);
$zip=sbad($_POST["Zip"]);
$country=sbad($_POST["Country"]);
$phone=sbad($_POST["Phone"]);
$cell=sbad($_POST["Cell"]);
$fax=sbad($_POST["Fax"]);
$mailinglist=sbad($_POST["Mailing_List"]);
$remove=sbad($_POST["Remove"]);
$comment=sbad($_POST["Comment"]);
$CISSP=sbad($_POST["cert_in_CISSP"]);
$GIAC=sbad($_POST["cert_in_GIAC"]);
$CISA=sbad($_POST["cert_in_CISA"]);
if ($remove=="Yes")
$flag="REMOVE FROM MAILING LIST";

$email = ereg_replace("\n","",$email);
$fname = ereg_replace("\n","",$fname);
$lname = ereg_replace("\n","",$lname);
$mailtext="$flag \n Contact from: $salutation $fname $lname \n $email\n Position: $title \n Company: $company \n Phone: $phone Cell: $cell Fax: $fax";
$mailtext.="\nAddress: \n $address_1 \n $address_2 \n $city,$state $zip $country \n Comments:\n $comment";
$mailtext.="\nWants to be added to mailing list? $mailinglist\n";
$mailtext.="CISSP Cert? $CISSP\nGIAC Cert? $GIAC\nCISA Cert? $CISA";
#formmail mailtext format
$now=date("l dS o F Y h:i:s A");
$mailtext="Below is the result of your feedback form.\nIt was submitted by $email on: $now\n\n---------------------------------------------\n\nSalutation: $salutation\n\nFirst: $fname\n\nLast: $lname\n\nJob Title: $title \n\nCompany: $company\n\nAddress_1: $address_1\n\nAddress_2: $address_2\n\nCity: $city\n\nState: $state\n\nZip Code: $zip\n\nCountry: $country\n\nDirect Line: $phone\n\nCell: $cell\n\nFax: $fax\n\nComment: $comment\n\nMailing List: $mailinglist $flag \n\ncert_in_CISSP: $CISSP\ncert_in_GIAC: $GIAC\n\ncert_in_CISA: $CISA\n\n---------------------------------------------";


$webmaster_addr = 'joec@yensidinc.com';
#send a quick mail to webmaster

mail("joec@yensidinc.com","Enter to Win - Free Training",$mailtext,"From: $email\nReply-to: $email");

#prep sql query
$sql="INSERT INTO $contact_table ";
$sql.="(RegTime,Salutation,First_Name,Last_Name,Job_Title, Company, Address_1,Address_2,City,State,Zip";
$sql.=",Country,Phone,Cell_Phone,Fax,Email,Mailing_List,Remove,Comments,CISSP,GIAC,CISA) VALUES ";
$sql.=" ('".date("Y-n-j H:i:s")."','$salutation','$fname','$lname','$title','$company','$address_1','$address_2'";
$sql.=",'$city','$state','$zip','$country','$phone','$cell','$fax','$email','$mailinglist','$remove','$comment'";
$sql.=",'$CISSP','$GIAC','$CISA');";
#print $sql;
#drop into db
if (!($res = mysql_db_query($database, $sql))) {
$err = mysql_error();
#something bad happened
mail("joec@yensidinc.com","Database error in win9mo.php","Error: $err\nValues: $values_str","From: $webmaster_addr");
}
include("win9mo-ty.php");
}
} else {

?>

prasanthmj
12-12-2008, 07:28 PM
//This is your existing code ...
$webmaster_addr = 'joec@yensidinc.com';
#send a quick mail to webmaster

mail("joec@yensidinc.com","Enter to Win - Free Training",$mailtext,"From: $email\nReply-to: $email");
//new code for auto-response

//The body of the response email. modify as required
$response ="Hello $fname\n Thanks for the submission. we will get in touch with you soon!\n Thanks\njoec@yensidinc.com";

mail($email,"Thanks for the submission",$response,"From: joec@yensidinc.com\nReply-to: joec@yensidinc.com");



hope this helps.

for examples of email contact forms see:PHP based email contact form (http://www.html-form-guide.com/contact-form/php-email-contact-form.html)

rangerjoe
12-12-2008, 08:38 PM
Hey, thanks for your reposnse and help. I placed your code in the proper place and tested the form, but the email address that I entered while filling out the form did not receive the "thank you" email. Could there be something within my code that is causing it to not generate a reply?

Thanks again for your time and help!

Joe

rangerjoe
12-16-2008, 05:03 PM
Hey all, can anyone help me with this?

Thanks

rangerjoe
12-16-2008, 05:46 PM
Hey guys, that code did work, thanks!

For some reason, one of my email addresses wasn't receiving the auto-reply message, but I tested it using a gmail and yahoo mail account and it worked.

Thanks,
Joe