Trouble sending email through contact form with php [duplicate] - php

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
I've never worked with php before and am trying to make my contact form functional with the help of some php code I acquired through google.
First off do I need to link it to my html page somehow (like css/ javascript) or does it work fine if it shares the same folder?
I have modified the names of certain parts to match the names in my html but am unable to get it to work. When I click the submit button I get a page saying...
"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Deadline: "; $Body .= $Dealine; $Body .= "\n"; $Body .= "Interested: "; $Body .= $Interested; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print ""; } else{ print ""; } ?>
The php code is as follows.
<?php
$EmailFrom = "c#c.co.nz";
$EmailTo = "c#c.co.nz";
$Subject = "contact-form";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Email = Trim(stripslashes($_POST['Email']));
$Deadline = Trim(stripslashes($_POST['Dealine']));
$Interested = Trim(stripslashes($_POST['Interested']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Deadline: ";
$Body .= $Dealine;
$Body .= "\n";
$Body .= "Interested: ";
$Body .= $Interested;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact-thanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Here is my html
<div role="form" class="form-wrapper">
<form name="contact-f" action="contact-form.php" method="post">
<div class="f-width">
<div class="f-col-1 f-group-1"><input type="text" name="Name" class="c-back input-t f-col-1" placeholder="Name" /></div>
<div class="f-col-1 f-group-2"><input type="email" name="Company" class="c-back input-t f-col-1" placeholder="Company Name" /></div>
<div class="f-col-1 f-group-1"><input type="text" name="Email" class="c-back input-t f-col-1" placeholder="Email Address" /></div>
<div class="f-col-1 f-group-2"><select name="Deadline" class="c-back input-t f-col-1"><option class="opt-f">Do you have a Deadline?</option><option value="Not yet" class="opt-f">Not yet</option><option value="Less than 1 Month" class="opt-f">Less than 1 Month</option><option value="2-3 Months" class="opt-f">2-3 Months</option><option value="3-6 Months" class="opt-f">3-6 Months</option><option value="6+ Months" class="opt-f">6+ Months</option></select></div>
<div class="f-col-2 f-group-3"><select name="Interested" class="c-back input-t f-col-2"><option class="opt-f">What are you interested in?</option><option value="Branding" class="opt-f">Branding</option><option value="Print Design" class="opt-f">Print Design</option><option value="Illustration" class="opt-f">Illustration</option><option value="Website / UI Design" class="opt-f">Website / UI Design</option><option value="Literature" class="opt-f">Literature</option><option value="Video Editing" class="opt-f">Video Editing</option><option value="Other" class="opt-f">Other</option></select></div>
<div class="f-col-3 f-group-4"><textarea name="Message" class="c-back input-t message-f f-col-3" placeholder="Describe your project..."></textarea></div>
<div class="f-submit f-group-5"><input type="Submit" name="Submit" value="Send" class="form-btn" /></div>
</div></form></div>
(And yes I have created the contact-thanks.php file it calls for at the end).

So many $Body , How About :
$Body = " Name : $Name \n Company : $Company \n Deadline : $Deadline \n Interested : $Interested \n Message : $Message";
Is This What You Looking For ?

Related

How would I add a check box to this PHP form [duplicate]

This question already has answers here:
How to read if a checkbox is checked in PHP?
(21 answers)
Closed 5 years ago.
I have a working form but I would like to add a check box to it. Sorry, completely new to PHP so I'm unsure what I would need to add to the PHP to make it work.
The HTML:
<label for="Name"></label>
<input type="text" placeholder="Name" name="Name" id="Name" />
<label for="Tel"></label>
<input type="text" placeholder="Tel" name="Tel" id="Tel" />
<label for="Email"></label>
<input type="text" name="Email" placeholder="Email address" id="Email" />
<label for="Message"></label><br />
<textarea name="Message" placeholder="Your Message" rows="10" cols="20" id="Message"></textarea>
<label for="Subscribe"></label>
<input type="checkBox" name="CheckBox" id="checkbox" /> <p>Please tick this box to be kept up to date with offers</p>
The PHP:
$EmailFrom = "me#test.com";
$EmailTo = "me#test.com";
$Subject = "Enquiry from website";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$Message = Trim(stripslashes($_POST['Checkbox']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=https://myurl.com\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
Any help would be really appreciated. Thanks
You're overwriting the $Message var with the value of the checkbox:
$Message = Trim(stripslashes($_POST['Message']));
$Message = Trim(stripslashes($_POST['Checkbox']));
You could also consider that checkboxes send on/off values unless you specify a different value with the parameter value="xxxx"

PHP form - Why validation doesn´t work?

I´m trying to validate a simple PHP form with HTML code but it doesn´t work properly.
Here you are HTML code:
<form action="expresscontactform.php" method="post" name="form1" id="form1">
<label>Name </label><input id="Name" name="Name" type="text" value="<?php echo $_POST['Name']; ?>">
<span class="error"> <?php echo $errors[1]; ?> </span>
<label>Email </label><input id="Email" name="Email" type="text" value="<?php echo $_POST['Email']; ?>">
<span class="error"> <?php echo $errors[4]; ?> </span>
<label>Phone </label><input id="Phone" name="Phone" type="text" value="<?php echo $_POST['Phone']; ?>">
<span class="error"> <?php echo $errors[2]; ?> </span>
<label>Country of origin</label><input id="Country" name="Country" type="text" value="<?php echo $_POST['Country']; ?>">
<span class="error"> <?php echo $errors[3]; ?> </span>
<label>Message </label><textarea id="message" cols="5" rows="5" name="Message"></textarea>
<input value="Send" class="send_request_new" type="submit">
</form>
And this is PHP code (on the server):
<?php
if(isset($_POST['send_request_new'])){
$errors = array();
if($_POST['Name'] == ''){
$errors[1] = '<span class="error">Please type your name</span>';
}else if($_POST['Phone'] == ''){
$errors[2] = '<span class="error">Please type your phone number</span>';
}else if($_POST['Country'] == ''){
$errors[3] = '<span class="error">Please type your country</span>';
}else{
$EmailFrom = Trim(stripslashes($_POST['Email']));
$EmailTo = 'webmaster#theacademy.co,' . $EmailFrom;
$Subject = "Online Application Form";
$name = Trim(stripslashes($_POST['Name']));
$phone = $_POST['Phone'];
$country = $_POST['Country'];
$message = $_POST['Message'];
$header = 'From: ' . $EmailFrom . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";
// prepare email body text
$Body .= "Contact form";
$Body .= "\n";
$Body .= "\n";
$Body .= "This is an automatically generated e-mail, to inform you that we received your request. We will contact you as soon as possible.";
$Body .= "\n";
$Body .= "\n";
$Body .= "Kind regards";
$Body .= "\n";
$Body .= "\n";
$Body .= "**********************************************";
$Body .= "\n";
$Body .= "\n";
$Body .= "The Academy";
$Body .= "\n";
$Body .= "\n";
$Body .= "::::::::::::::::::::::::::";
$Body .= "\n";
$Body .= "Your Request:";
$Body .= "\n";
$Body .= "::::::::::::::::::::::::::";
$Body .= "\n";
$Body .= "\n";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Country: ";
$Body .= $country;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $EmailFrom;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
$Body .= "\n";
$Body .= "Sent on " . date('d/m/Y', time());
$Body .= "\n";
$Body .= "\n";
$Body .= "Last visited page: ";
$Body .= $_SERVER['HTTP_REFERER'];
if(mail($EmailTo, $Subject, $Body, $header)){
$result = '<div class="result_ok">Email sent successfully</div>';
// If successfully we reset all the fields
$_POST['nombre'] = '';
$_POST['email'] = '';
$_POST['asunto'] = '';
$_POST['mensaje'] = '';
header("refresh:3;url=http://www.myexample.co/");
}else{
$result = '<div class="result_fail">Error!!</div>';
}
}
}
?>
I think there is a mistake in the line below:
<span class="error"> <?php echo $errors[1]; ?> </span>
But I don´t know where exactly.
One more question, how can I set "The Academy" as email sender?
I don´t know if this is the best way to validate a form, if someone show me other way I will be thankful to learn.
I appreciate a lot if anyone can help me, please.
Thanks in advance.
Firstly, your code's execution is dependant on this conditional statement if(isset($_POST['send_request_new'])) where it's looking for a "named" element called send_request_new therefore would never execute.
Your present (unnamed) submit button
<input value="Send" class="send_request_new" type="submit">
this should be changed to:
<input value="Send" class="send_request_new" type="submit" name="send_request_new">
you have a class named that way, instead of a name.
In order to get it to work, you would need to place your entire HTML/PHP inside the same page and use action=""
I noticed you don't have one for your Email and the message, only for the name/phone number and country.
Plus, to use a personalized method for the sender's name:
Base yourself on the following:
$Name = "The Academy";
$email = "email#example.com";
$header = "From: ". $Name . " <" . $email . ">\r\n";
You can use some of the filters on PHP.net to validate and protect against XSS injection:
-http://php.net/manual/en/filter.filters.validate.php
One of which being FILTER_VALIDATE_EMAIL

Only 2 out of 9 PHP form fields being sent to email

I've got a pretty simple contact form (or so I thought), but only 2 out of 9 contact fields are being delivered to my inbox. Strangely enough, it's the last two fields in the list, although the 'from' email address, and subject matter are working fine.
<form id="quote" class="contact_form" action="contact.php" method="post">
<h2>Easy Contact Form</h2>
<h4>Personal</h4>
<label for="title">Title:</label>
<input type="text" name="title" id="title" required class="required" >
<label for="name">Full Name:</label>
<input type="text" name="fullname" id="fullname" required class="required" >
<label for="email">Email:</label>
<input type="email" name="email" id="email" required placeholder="jsmith#email.com" class="required email">
<label>Phone:</label>
<input type="text" name="phone" id="phone" />
<h4>Address</h4>
<label>House #:</label>
<input type="text" name="house" id="house" />
<label>Street:</label>
<input type="text" name="street" id="street" />
<label>Town/City:</label>
<input type="text" name="town" id="town" />
<label>Postcode:</label>
<input type="text" name="postcode" id="postcode" />
<h4>Lender</h4>
<label>Company Name:</label>
<input type="text" name="companyname" id="companyname" />
<input class="btn" type="image" src="images/submit_btn.jpg"/>
</form>
And here's the PHP:
<?php
$EmailFrom = $_REQUEST['email'];
$EmailTo = "me#email.com";
$Subject = "Information";
$Title = Trim(stripslashes($_POST['title']));
$Fullname = Trim(stripslashes($_POST['fullname']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['phone']));
$House = Trim(stripslashes($_POST['house']));
$Street = Trim(stripslashes($_POST['street']));
$Town = Trim(stripslashes($_POST['town']));
$Postcode = Trim(stripslashes($_POST['postcode']));
$Companyname = Trim(stripslashes($_POST['companyname']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "Error";
exit;
}
// prepare email body text
$Body = "Title: ";
$Body .= $Title;
$Body .= "\n";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body = "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body = "house: ";
$Body .= $house;
$Body .= "\n";
$Body = "Street: ";
$Body .= $Street;
$Body .= "\n";
$Body = "Town: ";
$Body .= $Town;
$Body .= "\n";
$Body = "Postcode: ";
$Body .= $Postcode;
$Body .= "\n";
$Body .= "Company Name: ";
$Body .= $Companyname;
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
echo "Succes";
}
else{
echo "Error";
}
?>
Here's the problem:
// prepare email body text
$Body = "Title: ";
$Body .= $Title;
$Body .= "\n";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body = "Phone: "; //needs dot
$Body .= $Phone;
$Body .= "\n";
$Body = "house: ";
$Body .= $house;
$Body .= "\n";
$Body = "Street: "; //needs dot
$Body .= $Street;
$Body .= "\n";
$Body = "Town: "; //needs dot
$Body .= $Town;
$Body .= "\n";
$Body = "Postcode: "; //needs dot
$Body .= $Postcode;
$Body .= "\n";
$Body .= "Company Name: ";
$Body .= $Companyname;
You need to make sure each line has .= instead of just =. Near the bottom where you have $Body = "Postcode: ";, you're resetting the entire $Body variable.
The problem is from $body.
// prepare email body text
$Body = "Title: ";
$Body .= $Title;
$Body .= "\n";
$Body .= "Fullname: ";
$Body .= $Fullname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body = "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body = "house: ";
$Body .= $house;
$Body .= "\n";
$Body = "Street: ";
$Body .= $Street;
$Body .= "\n";
$Body = "Town: ";
$Body .= $Town;
$Body .= "\n";
$Body = "Postcode: ";
$Body .= $Postcode;
$Body .= "\n";
$Body .= "Company Name: ";
$Body .= $Companyname;
The first variable declaration $Body = "Title: ";is fine, however after that you need to make sure you are appending to the string using .=.
Each time after the intital declaration you use =, it clears everything assigned or appended previously and starts again.
This is why you only see the last 2 form fields, because $Body = "Postcode: "; restarts the variable, removing everything before.
Use .= for every single line except the first one, which should be an =.
Hope this helps!

PHP contact form messages not receiving

Hi I have simple contact form for email on my site, and the form works including the success of a message sent,however, I am not receiving email to the designated webmail server. I am running the latest PHP. Do some web servers cache mail or is there some error in this code I am not seeing.
<?php
$EmailFrom = "email#mydomain.com";
$EmailTo = "email#mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
<div id="contact-area">
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Thought:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Transmit" class="submit-button" />
</form>
<div style="clear: both;"></div></div>
Sending mail is more complicated than just running the mail() command, you need to take a look at the php.ini settings for the email configuration and continue troubleshooting from there.
If you are running on a host that does not configure the mail() function to run, you can try using the following opensource mailer:
PHPMailer
What you can try is to use the examples provided and run from your local development stack (XAMPP, MAMP, WAMP) connected from your home, you should be able to receive mail if configured correctly.
Once it is OK, you can try it on the server, it should send as well, if not, check that the outgoing port for sending mail is not blocked.
Alternative to using PHPMailer, you can look for a simple PHP mailer and try those out as well.
Don't use print "<meta http-equiv=\"refresh\"... because during testing, it caused an endless loop while sending multiple copies of the same Email.
Use header('Location: ...'); instead, along with exit;.
Plus, all your <meta http-equiv=\"refresh\" have generated a parse error.
Working example (tested with Email successfully received):
<?php
$EmailFrom = "email#mydomain.com";
$EmailTo = "email#mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
// echo "error 1";
header('Location: error.htm');
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
header('Location: http://www.example.com/');
exit;
}
else{
header('Location: error.htm');
exit;
}
?>
i think its the mail function headers issue.
http://php.net/manual/en/function.mail.php
http://php.net/manual/en/function.mail.php

php to form script? syntax error unexpected

Hi Can anyone suggest a quick form script for a php form?
I generated the one below from telepro and modified the html and emails a bit, but I get this error for the checkbox
Parse error: syntax error, unexpected '=' in /home/inn.co.uk/public/mailer.php on line 14
thanks for your help
Regards
Judi
<form method="POST" action="contact.php">
<div class="form-field">
<input type="text" name="Name" onFocus="if(this.value=='Name')this.value='';" value="Name">
</div>
<div class="form-field">
<input type="text" name="Telephone" onFocus="if(this.value=='Telephone')this.value='';" value="Telephone">
</div>
<div class="form-field">
<input type="text" name="Timetocall" onFocus="if(this.value=='Time to call')this.value='';" value="Time to call">
</div>
<div class="form-field">
<ul class="tickboxes">
<li>Do you agree to our<br/>Terms of Business <input type="checkbox" name="DoyouagreetoourTermsofBusiness?" value="Yes" /></li></ul></div>
<div class="button">
<input type="image" src="images/submit.jpg" value="" name="submit">
</div>
</form>
<?php
// Website Contact Form Generator
// http://www.tele-pro.co.uk/scripts/contact_form/
// This script is free to use as long as you
// retain the credit link
// get posted data into local variables
$EmailFrom = "enquiry#inn.co.uk";
$EmailTo = "judith#yahoo.co.uk";
$Subject = "New enquiry";
$Name = Trim(stripslashes($_POST['Name']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Timetocall = Trim(stripslashes($_POST['Timetocall']));
$DoyouagreetoourTermsofBusiness? = Trim(stripslashes($_POST['DoyouagreetoourTermsofBusiness?']));
// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;
if (Trim($Telephone)=="") $validationOK=false;
if (Trim($Timetocall)=="") $validationOK=false;
if (Trim($DoyouagreetoourTermsofBusiness?)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Timetocall: ";
$Body .= $Timetocall;
$Body .= "\n";
$Body .= "DoyouagreetoourTermsofBusiness?: ";
$Body .= $DoyouagreetoourTermsofBusiness?;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
You are using a question mark in your variable name. That doesn't work.
See the PHP Manual on variables for naming conventions.
replace
$DoyouagreetoourTermsofBusiness?
by
$DoyouagreetoourTermsofBusiness
and it will work fine.

Categories