strange behavior on contact form - php

I am having a strange problem with this form. I have made it yesterday night and it was working fine, sending all the emails as it should. However, I've run it today and it won't simply work at all. I am always getting the error message. Any clues? Thank you.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$depart = $_POST['departamento'];
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$corpo = "Nova Mensagem\n";
$corpo .= "De: " . $name . "\n";
$corpo .= "Email: " . $email . "\n";
$corpo.=" Para o departamento " . $depart. "\n";
$corpo .= "Mensagem: " . $message . "\n";
if ($depart = administrativo)
{
$email_to = '';
}
elseif ($depart = financeiro)
{
$email_to = '';
}
elseif ($depart = Suporte)
{
$email_to = '';
}
else
{
$email_to = '';
}
$status = mail($email_to, $subject, $corpo, $headers);
if($status) {
echo "<script> window.location.href = ''; </script>";
}
else {
echo "<script> window.location.href = ''; </script>";
}
?>

Instead of = use == for comparison
for example - instead of:
if( $depart = administrativo)
use
if( $depart == "administrativo" )

You should enclose strings within quotes. Moreover, == (comparing objects of different types) && === (comparing objects of same types) are used for comparing and = is used for assigning. So, change the code as follows (inside the if statements) :
if ($depart == 'administrativo')
{
$email_to = '';
}
elseif ($depart == 'financeiro')
{
$email_to = '';
}
elseif ($depart == 'Suporte')
{
$email_to = '';
}
else
{
$email_to = '';
}

Related

Sending an email to 2 variables in PHP

Im trying to send an email to 2 variables currently I have tried to add 3 emails to an array and put that into a variable and get the other email from the form, I then dont know how to put both of those variables together when sending so they each have their own "to" but this does not work or something doesnt and I dont know what?? And yes I have real emails that I use these are placeholders for this!!!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['submit'])) {
$email = $_POST['LOGSE'];
$email2 = array("test#test.com", "test#test.com", "test#test.com");
$name = $_POST['full_name'];
$eventtitle = $_POST['EventT'];
$InCharge = $_POST['InCharge'];
$Venue = $_POST['Venue'];
$VenY = $_POST['VenR'];
if($VenY != "Yes"){
$ava = " Have not checked if";
}
else{
$ava = "";
}
$dates = $_POST['dateS'];
$datee = $_POST['dateE'];
$adults = $_POST['Adults'];
$children = $_POST['Children'];
$catreq = $_POST['CateReq'];
if (catreq != ''){
$catreq = $catreq;
}
else{
$catreq = "No Catering Needed";
}
$logreq = $_POST['LogReq'];
if (logreq != ''){
$logreq = $logreq;
}
else{
$logreq = "No Logistic Equipment Needed";
}
$itreq = $_POST['ITReq'];
if (itreq != ''){
$itreq = $itreq;
}
else{
$itreq = "No IT Needed";
}
$tran = $_POST['TransR'];
if($tran != Yes){
$tran = "NO ";
}
else{
$tran = "";
}
$Risk = $_POST['RiskR'];
if($Risk != Yes){
$Risk = "NO ";
}
else{
$Risk = "";
}
$othern = $_POST['OtherN'];
// The Email:
$from = 'test#test.com';
$to = $email;
$to = $email2;
$subject = 'Event Form ' .$eventtitle;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=UTF-8' . "\r\n";
$headers .= 'From: Event Form<test#test.com>' . "\r\n";
$headers .= 'Cc: test#test.com, test#test.com' . "\r\n";
$body = '<html>
<head>
<title>Event</title>
<style>
h1 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
h3 {
font-family: "Century Gothic", CenturyGothic,
AppleGothic, sans-serif;
}
</style>
</head>
<body>
<h1>Event Details</h1>
<h3>Name: '.$name.'</h3>
<h3>Event Title: '.$eventtitle.'</h3>
<h3>Event Manager: '.$InCharge.'</h3>
<h3>Venue: '.$Venue.' - '.$ava.' Available</h3>
<h3>Date Start: '.$dates.'</h3>
<h3>Date End: '.$datee.'</h3>
<h3>Adults Attending: '.$adults.' Children Attending:
'.$children.'</h3>
<h3>Catering Requirements: '.$catreq.'</h3>
<h3>Logistic Requirements/Equipment: '.$logreq.'</h3>
<h3>IT Requirements: '.$itreq.'</h3>
<h3>Other Notes: '.$othern.'</h3>
<h3><font color="red">'.$tran.'</font>Transport Has Been
Booked</h3>
</body>
</html>' . "\r\n";
mail( $to, $subject, $body ,$headers );
/* echo "An Email Has Been Sent<br>Thank You."; */
header('Location: ./thanks.html');
}
}
The $to in mail function should be a string. If there are more than one email address then those should be comma separated. In your case you might need to do like following.
$to = $email . ','. implode(',', $email2);
Add email address with the comma
$email_to = "test#test.com,some#other.com,yet#another.net";
Also you can add
$headers = "Bcc: someone#domain.com";
Use a foreach on the array:
foreach($email2 as $to){
mail( $to, $subject, $body ,$headers );
}
Or like others are suggesting:
$to = $email.",".implode(",",$email2);
That doesn't make sense:
$to = $email;
$to = $email2;
Parmaeter $to should be a string of comma-seperated email-addresses, e.g.:
$to = $email1 . ", " . $email2 . ", " . $email3;

Add a part of my form to the e-mail i receive using PHP

In my contact form i recently added a selector ( http://shopzuinig.nl/contact.html ) and styled it the way i wanted, but when i fill in the form and press send, the choice for a location is not included in the e-mail i receive. Can someone provide me with the PHP code to make this happen?
Here is my current PHP code:
<?php
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
$replyto='restaurant#dellitalia.nl';
$subject = 'Verzoek via de website';
if($post)
{
function ValidateEmail($email)
{
$regex = "/([a-z0-9_\.\-]+)". # name
"#". # at
"([a-z0-9\.\-]+){2,255}". # domain & possibly subdomains
"\.". # period
"([a-z]+){2,10}/i"; # domain extension
$eregi = preg_replace($regex, '', $email);
return empty($eregi) ? true : false;
}
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$message = stripslashes($_POST['message']);
$phone = stripslashes($_POST['phone']);
$answer = trim($_POST['answer']);
$verificationanswer="6"; // plz change edit your human answer
$from=$email;
$to=$replyto;
$error = '';
$headers= "From: $name <" . $email . "> \n";
$headers.= "Reply-to:" . $email . "\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers = "Content-Type: text/html; charset=utf-8\n".$headers;
// Checks Name Field
if(!$name || !$email || $email && !ValidateEmail($email) || $answer <> $verificationanswer || !$message || strlen($message) < 1)
{
$error .= 'De velden zijn niet correct ingevuld.<br />';
}
if(!$error)
{
$messages.="Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Message: $message <br>";
$mail = mail($to,$subject,$messages,$headers);
if($mail)
{
echo 'OK';
if($autorespond == "yes")
{
include("autoresponde.php");
}
}
}
else
{
echo '<div class="error">'.$error.'</div>';
}
}
?>
Location Missing in your message. Include location to get location details in your mail.
if(!$error)
{
$mydropdown=$_POST['mydropdown'];
$mydropdown=mysql_real_escape_string($mydropdown);
$messages.="Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Message: $message <br>";
$messages.="Location: $mydropdown<br>"; // Missing. Include Location Here
$mail = mail($to,$subject,$messages,$headers);
if($mail)
{
echo 'OK';
if($autorespond == "yes")
{
include("autoresponde.php");
}
}
}

Trouble with PHP validating select box array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am adding some new validation to a current form, I have three separate arrays that I have built for select boxes and I need to be sure that the user is making at least one of the selections. At this point when the user fills our the form, it will submit and send, but the "Required" shows next to an empty select box in the form even through the notice says it has been sent and the form will send even if the user hasn't made a selection. I have tried searching everywhere for a solution, hoping to get a second set of eyes on this issue. Thank you in advance for your time!
The select box within the form -
<?php echo $monthMessage; ?>
<select name="mm" class="select-form-style">
<option>
- Select Month -
</option>
<?php
foreach ($months as $month) {
$selected = (!empty($mm) && $mm == $month) ? 'selected="selected"' : '';
?>
<option value="<?php echo $month;?>" <?php echo $selected;?>>
<?php echo $month;?>
</option>
<?php
}
?>
The PHP form validation
<?php #Registration
$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December');
$season = array('Fall 2014', 'Winter 2014', 'Spring 2015', 'Summer 2015');
$locations = array('Priest River', 'Sandpoint', 'Online');
function makeSafe($value) {
return stripslashes(trim($value));
}
//if the form was subitted
if (!empty($_POST['submit'])) {
//form submitted through post, do validation
$authorization = (!empty($_POST['authorization'])) ? makeSafe($_POST['authorization']) : '';
$signature = (!empty($_POST['signature'])) ? makeSafe($_POST['signature']) : '';
$cc = makeSafe($_POST['cc']);
$ll = makeSafe($_POST['ll']);
$fname = makeSafe($_POST['fname']);
$mname = makeSafe($_POST['mname']);
$lname = makeSafe($_POST['lname']);
$mm = makeSafe($_POST['mm']);
$day = makeSafe($_POST['day']);
$year = makeSafe($_POST['year']);
$age = makeSafe($_POST['age']);
$school = makeSafe($_POST['school']);
$address = makeSafe($_POST['address']);
$city = makeSafe($_POST['city']);
$state = makeSafe($_POST['state']);
$zip = makeSafe($_POST['zip']);
$gender = makeSafe($_POST['gender']);
$email = makeSafe($_POST['email']);
$phone = makeSafe($_POST['phone']);
$altph = makeSafe($_POST['altph']);
$hp = makeSafe($_POST['hp']);
$packet = (!empty($_POST['signature'])) ? makeSafe($_POST['packet']) : '';
$message = '';
$problem = FALSE;
if(!empty($_POST['mm']))
{
$monthMessage .= '<p class="errorClass">Required</p>';
}
if(!empty($_POST['cc']))
{
$seasonMessage .= '<p class="errorClass">Required</p>';
}
if(!empty($_POST['ll']))
{
$locationMessage .= '<p class="errorClass">Required</p>';
}
if($_POST['authorizaton']!="1" AND $_POST['packet']!="1"){
$problem = TRUE;
$parentMessage .= '<p class="errorClass parentMsg">Please read and check the boxes below
</p>';
}
if($_POST['signature']!="1"){
$problem = TRUE;
$signatureMessage .= '<p class="errorClass">Required</p>';
}
//Check First Name
if (!eregi ('^[[:alpha:]\.\' \-]{2,}$',$fname)) {
$problem = TRUE;
$fnameMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[[:alpha:]\.\' \-]{2,}$',$lname)) {
$problem = TRUE;
$lnameMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[[:alnum:]\.\' \-]{4,}$', $school)) {
$problem = TRUE;
$schoolMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[[:alnum:]\.\' \#\-]{4,}$', $address)) {
$problem = TRUE;
$addyMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[[:alpha:]\.\' \-]{4,}$', $city)) {
$problem = TRUE;
$cityMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[[:alpha:]\.\' \-]{2,2}$', $state)) {
$problem = TRUE;
$stateMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[[:alnum:]][a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',$email)) {
$problem = TRUE;
$emailMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[0-9]{5}(\-[0-9]{4})?$',$zip)) {
$problem = TRUE;
$zipMessage .= '<p class="errorClass">Required</p>';
}
if (!eregi ('^[0-9.\(\)\-\ ]{10,14}$', $phone)) {
$problem = TRUE;
$phoneMessage .= '<p class="errorClass">Required</p>';
}
if(!eregi('^[0-9]{2}$',$age)) {
$problem = TRUE;
$ageMessage .= '<p class="errorClass">Required</p>';
}
if(!eregi('^[0-9]{2,4}$',$year)) {
$problem = TRUE;
$yearMessage .= '<p class="errorClass">Required</p>';
}
if(!eregi('^[0-9]{1,2}$',$day)) {
$problem = TRUE;
$dayMessage .= '<p class="errorClass">Required</p>';
}
if ($problem == TRUE) {//Something went wrong
echo $message;
echo '';
}
else{
$to = "webangel119#yahoo.com";
$subject = "Buckle-Up New Student Registration";
$headers = 'From: jen.byron83#gmail.com' . "\r\n" .'X-Mailer: PHP/' . phpversion();
$msg .= "PLAIN TEXT EMAIL\n\n";
$msg .= "Location: $ll Class: $cc\n\n";
$msg .= "Name: $fname $mname $lname\n\n";
$msg .= "Birthday: $mm $day $year Age: $age Gender: $gender\n\n";
$msg .= "School: $school\n\n";
$msg .= "Address: $address\n\n";
$msg .= "City: $city State: $state Zip Code: $zip\n\n";
$msg .= "Phone: $phone Alternate Contact: $altph\n\n";
$msg .= "Email: $email\n\n";
if(mail($to, $subject, $msg, $headers) == false){
// Email failed
echo '<p>An error occured while trying to process your request</p>';
}
else {
echo '<p>Thank you for registering with Buckle-Up Driving School!</p>';
$authorization = '';
$ll = '';
$cc = '';
$fname = '';
$mname = '';
$lname = '';
$mm = '';
$day = '';
$year = '';
$age = '';
$school = '';
$address = '';
$city = '';
$state = '';
$zip = '';
$gender = '';
$email = '';
$phone = '';
$altph = '';
$hp = '';
$signature = '';
$packet = '';
}
}
}
?>
I'm not sure if i understood the problem, but if i follow my feeling about this, my answer will be :
Your mistake is up there :
if(!empty($_POST['mm']))
{
$monthMessage .= '<p class="errorClass">Required</p>';
}
So, if $_POST['mm'] is not empty, then "Required" will be shown. But you have a probleme, this value will never be empty :)
Even if no month is selected ("- Select Month -" selected), the navigator will send the content of the option tag as the value of select ;)
So you have $_POST['mm'] = "- Select Month -"
You have to do this test :
// if mm sent and is not empty
if(isset($_POST['mm']) && !empty($_POST['mm']))
{
// if mm is in $months array
if(in_array($_POST['mm'], $months))
{
// this value is not accepted or "- Select Month -" selected
}
else
{
// seems a month is selected and into your $months list
}
}
else
{
// this case happens if don't send "mm" in post
// or if the value of mm is empty
}
Btw, a value will never be sure by doing stripslashes(trim($val)); care with this.
Btw2, you can apply makeSafe() to your whole $_POST array by doing this :
foreach($_POST as $k => $v)
{
$P[$k] = makeSafe($v);
}
then call your data like $P['mm']
Hope it helped !

Two Form Post Actions - Pass data to another page AND email the data

I am currently working on a form that needs two post actions with one submit button. I am not extremely versed in PHP, only know enough to make my way around current tasks, until now.
Here is the code for the page the form is on:
<?php
if ($_POST) {
if (empty($_POST['first']) ||
empty($_POST['last']) ||
empty($_POST['email']) ||
empty($_POST['location'])) {
$errors = 1;
} elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors = 2;
} else {
$to = "emailgoeshere#gmail.com";
$subject = "Blah blah blah";
$message .= "Name: ".$_POST['first']." ".$_POST['last']."\n";
$message .= "Email: ".$_POST['email']."\n";
$message .= "Cell Phone: ".$_POST['cell']."\n";
$message .= "Location: ".$_POST['location']."\n";
$from = $_POST['email'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('Location: freepass.php');
exit;
}
}
if ($errors == 1) {
$errors = "Please fill out all fields";
} elseif ($errors == 2) {
$errors = "Please enter a valid email";
}
?>
This is the form action:
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
This is the code on the page that the data will pass to:
<html>
<head>
</head>
<body>
<?php echo $_POST["first"]; ?> <?php echo $_POST["last"]; ?>
<br>
<?php echo $_POST["email"]; ?>
<br>
<?php echo $_POST["cell"]; ?>
<br>
<?php echo $_POST["location"]; ?>
</body>
</html>
This is a very quick solution but it should do the trick.
if ($_POST) {
$errors = null;
$error_message = "<ul>";
if (empty($_POST['first']) ||
empty($_POST['last']) ||
empty($_POST['email']) ||
empty($_POST['location'])) {
$errors = 1;
}
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors = 2;
}
if($errors == null) {
$to = "grimegriever#gmail.com";
$subject = "City Fitness 7-Day Pass Applicant";
$message .= "Name: ".$_POST['first']." ".$_POST['last']."\n";
$message .= "Email: ".$_POST['email']."\n";
$message .= "Cell Phone: ".$_POST['cell']."\n";
$message .= "Location: ".$_POST['location']."\n";
$from = $_POST['email'];
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
header('Location: freepass.php');
exit;
} else {
if ($errors == 1) {
$error_message .= "<li>Please fill out all fields</li>";
}
if ($errors == 2) {
$error_message .= "<li>Please enter a valid email</li>";
}
$error_message .= "</ul>";
}
}
I'm sure there are much more efficient solutions, but this will work.

Sending mail php

Hey, no idea why this sint working, but the message is being sent as "0". I think the e-mail fIELD is what is causing it
<?php
if ($_POST['check'] == 'checked'){
header("location: /nospamplease.html");
exit();
}
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$children = $_POST['children'];
$hot = $_POST['hot'];
$comments = $_POST['comments'];
/*echo $name;
echo $email;
echo $phone;
echo $date;
echo $children;
echo $hot;
echo $comments;*/
if($name == "" || $email == "" || $phone == "" || $date == "" || $children == "" || $hot == "" || $comments == ""){
echo "Please ensure all fields were filled out!";
exit();
}else{
$to = "######";
$subject = "Birthday enquiry";
$message = "Name: ".$name;
$message += "Email: ".$email;
$message += "Phone: ".$phone;
$message += "Date: ".$date;
$message += "Children: ".$children;
$message += "Hot or cold: ".$hot;
$message += "Comments: ".$comments;
//echo $message;
if(mail($to, $subject, $message)){
echo "Thank you for your enquiry, we will contact you within the next 24 hours! <br /> Click <a href='###'> here</a> to go back to the website!";
}else{
echo "There was an error, contact us directly: <a href='mailto:##'>email</a>";
}
}
?>
I echoed out the variables, they all are getting posted fine
Thanks guys
You can't use + to concatenate strings in PHP. Use .
$message .= "Email: ".$email;

Categories