i have bought a template that have built in contact form
problem is that it submits every thing except "company" name
i messed around with it but cant get to work it.
if you can point me to some solution i would be greatful
thanks in advance
this is php script
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."#"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "--------#gmail.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'Please fill in all required fields, marked with *'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(#mail($your_email,$email_subject,$email_content)) {
echo 'Thanks for your message, will contact you soon.';
} else {
echo 'ERROR!';
}
}
$values = array ('name','email','message');
Add 'company' to this list in the PHP script.
Also, I would modify the foreach loop to look like this, to get the intended functionality:
foreach($values as $key => $value)
{
if(in_array($value,$required))
{
if(empty($_POST[$value]))
{
echo 'Please fill in all required fields, marked with *';
exit;
}
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
This way, fields that aren't in the $required array are still added to the email if they exist - they just don't have to pass the empty check.
The problem is this line:
$email_content .= $value.': '.$_POST[$value]."\n";
Is never reached unless this line:
if (in_array($value,$required)) {
... is satisfied. This means, only fields listed in $required are going to be appended to the email. If that is OK, then simply change these lines:
$required = array('name','email','message');
$values = array ('name','email','message');
To read:
$required = array ('name','email','message','company');
$values = array ('name','email','message','company');
If that is NOT ok, change this:
foreach ($values as $key => $value) {
if (in_array($value,$required)) {
if ($key != 'subject' && $key != 'company') {
if (empty($_POST[$value])) {
echo 'Please fill in all required fields, marked with *';
exit;
}
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
To look like this:
foreach ($values as $key => $value) {
if (in_array($value,$required)) {
if ($key != 'subject' && $key != 'company') {
if (empty($_POST[$value])) {
echo 'Please fill in all required fields, marked with *';
exit;
}
}
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
Then you can take company out of $required, but leave it in $values. I suspect the template worked fine when you started because all fields were required.
If it still does not work, please change this (up top):
if(!$_POST) exit;
To look like this:
if(!$_POST) exit;
print_r($_POST);
... and paste the additional output in your question.
Also, consider buying templates from another vendor :)
Related
I created a php email form for a contact us page to my site, and want to use the same form for multiple email addresses. However, when I copy the code to the new page and add a different email address, it goes to the same email address as the original contact form.
I change the
$email_to = 'example#gmail.com, customer#website.com';
to a different email address, but it won't work, and keeps sending to the original email address.
How can I make this form usable for multiple emails for multiple email forms?
<?php
// Set email variables
$email_to = 'SARAI.ROMEROEVANS#lausd.net, customer#website.com';
$email_subject = 'Contact Form Submission Bancroft Website';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^#\s]+#([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
If you need more code, let me know
As far as I know you can't specify multiple email addresses in the first parameter of the mail() function. EDIT - you can, but I had assumed this wasn't possible as it has caused my problems in the past.
Here is an edited version of your code. I split $email_to up by commas and then loop through each email address to send an email to each.
I have also added some if statements to control who the recipients are. If you are going to re-use the code for multiple forms then pass a formID as a hidden field and then you can specify in the PHP who should receive the emails. No need to duplicate the PHP code for each form then ...
<?php
// Set email variables
if($_POST['formID'] == 1) {
$email_to = 'SARAI.ROMEROEVANS#lausd.net, customer#website.com';
} elseif($_POST['formID'] == 2) {
$email_to = 'test#test.com, customer#website.com';
} else {
$email_to = 'blah#blah.com, customer#website.com';
}
$email_subject = 'Contact Form Submission Bancroft Website';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
$email = explode(',', $email_to);
foreach($email as $e) {
$e = trim($e);
mail($e, $email_subject, $email_content);
}
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^#\s]+#([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
I'm currently using Godaddy webmail service. Basically, the problem with this contact form is that you can submit the message, but it doesn't get sent. (Note: The email address in the '$email_to' area is not the actual email that I used to test the form.).
Below is the code.
<?php
// Set email variables
$email_to = 'randomaddress#gmail.com';
$email_subject = 'Form submission';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^#\s]+#([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
check your server. SMTP is enable or not, if SMTP not enable please set up SMTP.
We have a large php mailer form that works like a charm when used by itself. But when I put it inside a Wordpress page, the mailer no longer sends (and yes, php is enabled in Wordpress content). The form goes through the motions and doesn't show any errors, but it simply doesn't send the email. So obviously something is conflicting with Wordpress and I have no idea what it would be. Has anyone experienced this?
I guess I could post some of the form code if necessary, but again, it works fine by itself. Any ideas on why Wordpress would break php mail?
<?php
if(!empty($_POST['first-name'])) {
$_POST['email'] = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$agree1 = implode(", ", $_POST['certify-info-true']);
$agree2 = implode(", ", $_POST['understand-false-info']);
$agree3 = implode(", ", $_POST['final-sig']);
foreach($_POST as $name => $value) {
$value = filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
if($name == 'submit') {
//skip
}
elseif ($name == 'certify-info-true') {
$res .= "$name : $agree1 \n \n";
}
elseif ($name == 'understand-false-info') {
$res .= "$name : $agree2 \n \n";
}
elseif ($name == 'final-sig') {
$res .= "$name : $agree3 \n \n";
}
else {
$res .= "$name : $value \n \n";
}
}
//SET here the destination email address and subject line
mail('test#email.com','Employment Application', "$res", "From: {$_POST['email']}");
//Errors
//ini_set( 'display_errors', 1 );
//error_reporting( E_ALL );
//SET here the thank you message
echo "<h2>Thank you for your interest in becoming an employee. If you meet the correct qualifications, we will contact you.</h2>";
//print_r($_POST);
} else {
?>
<form action="" id="employment" method="POST">
<!--form snipped-->
</form>
<?php
}
?>
I have some code to check an empty post like this:
foreach($_POST as $key => $value)
{
if (empty($_POST[$key]))
{
$errors[] = "$key" . "is empty.";
}
}
how can i add some custom messages because using $key + is empty isn't good enough for me. i want to display custom messages for every post. maybe some function like validate($name, Please enter your name, required). but i don't have a clue how to do it, can anyone can provide me some method i can try?
Since you want a custom message depending on the key, you should just use a switch.
$errors = array();
foreach($_POST as $key => $value)
{
if (empty($value))
{
switch($key) {
case 'name':
$errors[] = "The name is empty.";
break;
case 'age':
$errors[] = "The age is empty.";
break;
default:
$errors[] = $key . " is empty.";
}
}
}
* Edit *
If you want to do special treatment according to the key, and use a function :
$errors = array();
foreach($_POST as $key => $value)
{
$result = validate($key, $value);
if (!empty($result)) {
$errors[] = $result;
}
}
function validate($key, $value) {
if ($key == 'name' && empty($value)) {
return 'You must enter your name';
}
elseif ($key == 'age' && empty($value)) {
return 'You must enter your date of birth';
}
elseif ($key == 'email' && filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
return 'Your email is incorrect';
}
return '';
}
Check for Form Validation scripts/tutorials with PHP.
Here are some quick findings:
Using modular approach: http://www.sitepoint.com/form-validation-with-php/
Using class based (OOP) approach: http://www.html-form-guide.com/php-form/php-form-validation.html
if you want to use same approach, then use this
foreach($_POST as $key => $value)
{
if (empty($_POST[$key]))
{
$errors[] = getValidationMessage($key);
}
}
function getValidationMessage($key){
if($key == "user_name")
return "please enter user name.";
else if ($key == "date_of_birth")
return "please enter you date of birth";
//and so on,
//based on param names in your html-forms
}
Something like this?
foreach ($_POST as $k => $v) {
$errors[] = validate($k);
}
function validate($key) {
if(empty($_POST[$key])) {
switch ($key) {
case 'name':
$message = "Name is empty";
break;
case 'email':
$message = "fill in your email address";
break;
default:
$message = $key." is empty";
break;
}
return $message;
}
return false;
}
My e-mail processor is working... but how do I get the From e-mail address to show up instead of "myhost"? I'm getting anonymous#q0.xxxxxxxxxxxxx.com. So, when someone fills in the form, I want his e-mail to be the reply address.
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."#"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','telephone','company','message');
$required = array('name','email','telephone','message');
$your_email = "myemail#somehost";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(#mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>
Add this to your file:
$headers = 'From: ' . $your_email . "\r\n" .
'Reply-To: ' . $your_email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Then when you call the mail command use this:
mail($your_email,$email_subject,$email_content,$headers);
By default, the email sent by PHP [ mail() function ] uses as sender your server.
To change the sender, you must modify the header of email.
You can do it this way:
$emailHeader = "From: theSender#yourdomain.com" . "\r\n" . "Reply-To: theSender#yourdomain.com" . "\r\n";
// add the header argument to mail function
mail($your_email,$email_subject,$email_content,$emailHeader);
Note that we added a fourth argument to the mail function.
okay... i'd say strip the whole $values = array.... line and replace is with
$values = array();
foreach($_POST as $k => $v) if(strtolower($k) != 'submit') $values[$k] = htmlspecialchars($v);
that add's ALL your POST data to the $values array
and your content generator should look like this
foreach($values as $key => $value){
if(in_array($key,$required) && trim($value)==''){
echo 'PLEASE FILL IN REQUIRED FIELDS'; exit;
}
$email_content .= $key.': '.$value."\n"; // makes more sense
}
hope i got your question right oO
Hey. Just by the way: from what i see here,
if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
does not make sense.
your key's will always be: 0 to count($values)... you don't have an associative array. or is this $values array just for testing purposes?