PHP Mail function didn't work - php

I have use php contact form for my web site.but it's doesn't work properly.when i fill all the field correctly and submit. it's display error message "Invalid data" .
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
$subject=$_REQUEST['name'];
$email=$_REQUEST['email'];
$body=$_REQUEST['message'];
}else{
$to = "abc#abc.com";
$subject = $subject;
$from = $email;
$message = $body;
if (($from=="")||($subject=="")||($message==""))
{
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
} else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
?>

Like this it works:
<?php
$action=$_REQUEST['action'];
if ($action=="") {?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30" required/><br>
Your email:<br>
<input name="email" type="email" value="" size="30" required /><br>
Your message:<br>
<textarea name="message" rows="7" cols="30" required></textarea><br> <br>
<input type="submit" value="Send email" class="topbarbtn"/>
</form>
<?php
}
else {
$to = "...mail address...";
$subject = $_REQUEST['name'];
$from = $_REQUEST['email'];
$message = $_REQUEST['message'];
if (($from=="")||($subject=="")||($message=="")) {
echo '<script type="text/javascript">alert("Invalid Details");</script>' ;
}
else{
$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
}
?>

Noticed a flaw in your logics - you try to set $from as an $email, while you don't get a variable $email
in else statement add this lines:
$from = $_REQUEST['email'];
Plus if mail still is not sending you should considerthat some mail servers, such as qmail, will reject your message if it uses \r\n.
So you should try to use just \n or \n\n as a linebreak in a header.

Related

PHP form redirecting to PHP file itself

I'm trying to get a contact form working, and I'm being redirected to the PHP file itself, and I'm not sure why. I'm fairly new to PHP and I'm not entirely sure what I'm doing wrong. I'd love to be pointed in the right direction. The code is below.
Thanks
HTML:
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
PHP:
<?php
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
?>
This line tells the form what PHP file to send the data to:
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
In particular, it is this part that sends the data to a particular form:
action="form.php"
In fact, this is fine. You can process a form in the same PHP file that contains the form. Just put the PHP form processing at the top:
<?php
if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Just remove enctype="text/plain" otherwise your code is correct in HTML and try to echo feedback variable other way is to write all your code above the HTML like below:
<?php
if (isset($_POST['Email_Address'] && !empty['Email_Address']){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>

PHP contact form

I've looked through a lot of pages to find an answer for my problem. But I can't figure out why my form isn't working.
I tried different versions of codes from premade examples but I can't get it to work.
This is my php above my form
<?php
$name = $_POST['name'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'me#email.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $email)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>';
}
}
?>
and this is my form
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="Your Name">
<label>Email</label>
<input name="email" type="email" placeholder="Your Email">
<label>Subject</label>
<input name="subject" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" name="message" placeholder="Your Message"></textarea>
<input id="submit" name="submit" type="submit" value="Send">
</form>
I've only changed my email in this example here the rest is the same. I'm testing this live on a modern server with php 5+ support
Basicly everything works fine except that I don't get an email.
I can't find out how to make it work sadly. Any ideas would be cool :/
Edit: GOD DAMNIT IM A TOTAL IDIOT
Gmail Spam filter is strong in this one.
You have not defined $from.
That is why it is not sending mail.
Also, please check SMTP settings for your machine/server.
SMTP ports may not be configured, that is why mail is not sending.
Try this for the form:
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" type="text" placeholder="Your Name">
<label>Email</label>
<input name="email" type="text" placeholder="Your Email">
<label>Subject</label>
<input name="subject" type="text" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" type="text" name="message" placeholder="Your Message"></textarea>
<input id="submit" name="submit" type="submit" value="Send">
Also try following this working format:
<?php
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
You could just replace the value inside the " " with the post function.
Edit ( This will surely work ):
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $email)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again!</p>';
}
}
The fourth Parameter for the Mail function is header. Where You can set Mail form and others (http://php.net/manual/en/function.mail.php ). Please set that, and also check the SMTP configuration as "Programming Student" told you.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'to#test.com';
$from = 'from#test.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p style="color: #27ae60;">Your message has been sent!</p>';
} else {
echo '<p style="color: #c0392b;">Something went wrong, go back and try again! </p>';
}
?>
You have missed to assign the $from variable
<form class="contact-form" method="post" action="contact.php">
<label>Name</label>
<input name="name" type="text" placeholder="Your Name">
<label>Email</label>
<input name="email" type="text" placeholder="Your Email">
<label>Subject</label>
<input name="subject" type="text" placeholder="Your Subject">
<label>Message</label>
<textarea class="contact-form-message" name="message" placeholder="Your Message"> </textarea>
<input id="submit" name="submit" type="submit" value="Send">
</form>
Make sure the php file name "contact.php"
Now My HTML goes this way -->
<body> Fields with * are mandatory.
<form action="mail.php" method="POST">
<fieldset>
<legend>Contact Information</legend>
<p>Name*</p> <input type="text" name="name">
<p>Email*</p> <input type="text" name="email">
</fieldset>
<br />
<br />
<fieldset>
<legend>Other Information</legend>
<p>Website</p> <input type="text" name="website">
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Contact Us</option>
<option value="change">Information Change</option>
<option value="addition">Other</option>
</select>
<br />
</fieldset>
<br />
<fieldset>
<legend>Your Message</legend>
<p>Message*</p><textarea name="message" rows="8" cols="29"></textarea><br /><p>
<input type="submit" value="Send" class="but"> <input type="reset" value="Clear" class="but">
</fieldset>
</form>
</body>
and now the main thing that is PHP goes this way ->
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "youremail#domain.com";
$subject = "Contact Form";
$mailheader = "$name submited the form.";
if (filter_var("$email", FILTER_VALIDATE_EMAIL)) {
if ($email === "" || $message === "" || $name === "") {
echo "ERROR! Email and message and Name are Mandatory. <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
else {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
mail($email, "Name of Form Here", "Your Form has been submitted. Your problem will be noticed soon. This is a no reply mail address. Please dont reply back. - WeBoosters India", "Thankyou") or die("Error!");
echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
}
}
else {
echo "The email is not valid. Please write a proper email address. - <a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Back</a>";
}
?>
Hope you like it! Best of luck.

Send HTML Form data Via PHP

can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>

Why is this not working?

I've this little script to send email! But it's not working... Firstly it says that my variables are not defined and then, they confirm me that the message has been sent, but it doensn't happen!
Name:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="name">
</form> <br><br>
E-Mail:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="header">
</form> <br><br>
Subject:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="subject">
</form> <br><br>
Message: <br>
<form action="contactus.php" method="POST">
<textarea rows="10" cols="40" name="message" value=""></textarea>
</form>
<br><br>
<form action="contactus.php" method="POST">
<input type="submit" value="Submit" name="submit">
</form>
<?php
// php script to send emails
$to = 'some#email.com';
if (isset ($_POST['message'])) {
$message = "$name" . "<br><br>" . $_POST['message'];
}
if (isset ($_POST['header'])) {
$header = "From:" . $_POST['header'];
}
if (isset ($_POST['subject'])) {
$subject = ($_POST['subject']);
}
if (isset ($_POST['name'])) {
$name = ($_POST['name']);
}
if (isset($_POST['submit'])){
mail($to, $subject, $message, $header);
echo "Your message has been sent!";
}
//end of the php script
?>
If some of you can help me would be great!
Thank you.
You can't use all those different form elements. You are using 5 separate forms and the only one that is being submitted is the one with the submit button.
Thus, when the form is being submitted there $_POST['submit'] is set, but none of the other ones exist.
So you need your HTML to be:
<form action="contactus.php" method="POST">
Name:
<input type="text" size="32" value="" name="name"><br><br>
E-Mail:
<input type="text" size="32" value="" name="header"><br><br>
Subject:
<input type="text" size="32" value="" name="subject"><br><br>
Message: <br>
<textarea rows="10" cols="40" name="message" value=""></textarea><br><br>
<input type="submit" value="Submit" name="submit">
</form>
and contactus.php:
<?php
$to = 'some#email.com';
if(isset($_POST['message']) && isset($_POST['header']) &&
isset($_POST['subject']) && isset($_POST['submit']) &&
#mail($to, $_POST['subject'], $_POST['message'], $_POST['header'])) {
echo "Your message has been sent!";
}else{
echo "There has been a problem.";
}
?>
Try:
if(mail($to, $subject, $message, $header)) {
echo "Mail sent successfully.";
} else {
echo "PHP's mail() function failed!";
}
Why you have a Form element for each input elements?
You should put all of your elements in a Form together.

Get Checkbox Values from Forms using an External PHP Processor

I have a form that sends its data to a process.php file for processing in that file I have the following code:
<?php
$name = $_GET['name'];
$number = $_GET['number'];
$email = $_GET['email'];
$comment = $_GET['message'];
$sales = $_POST['sales'];
$lettings = $_POST['lettings'];
$to = "me#me.com";
$subject = "New website registration";
$message = "Name: ".$name."\r\n";
if(isset($number) && $number!='')
$message.= "Number: ".$number."\r\n";
if(isset($email) && $email!='')
$message.= "Email: ".$email."\r\n";
if(isset($comment) && $comment!='')
$message.= "Comment: ".$comment."\r\n";
if(isset($sales))
{
$message.= "I am Interested in Sales" . "\r\n";
}else{
//
}
if(isset($lettings))
{
$message.= "I am Interested in Lettings";
}else{
//
}
$headers = "From: ".$name." <".$email.">";
$result = mail($to,$subject,$message,$headers);
return $result;
?>
The HTML for the form is as follows:
<form id="register_form" name="register" method="post" action="/content/contact/process.php">
<input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
<input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
<input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
<textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
<label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
<label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
<input id="submit" type="submit" value="Submit" class="regsender" />
<div class="loading"></div>
</form>
When the checkboxes are checked, the expected messages do not come through in the email. Am I missing something here. I've tried several different approaches to getting this to work, none of which do. Any help would be appreciated. Thanks.
You are using method = POST and retrieving values with $_GET
I have updated your code check it
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$comment = $_POST['message'];
if (isset($_POST['sales']))
{
$sales = $_POST['sales'];
}
else if(isset($_POST['lettings']))
{
$lettings =$_POST['lettings'];
}
$to = "me#me.com";
$subject = "New website registration";
$message = "Name: ".$name."\r\n";
if(isset($number) && $number!='')
$message.= "Number: ".$number."\r\n";
if(isset($email) && $email!='')
$message.= "Email: ".$email."\r\n";
if(isset($comment) && $comment!='')
$message.= "Comment: ".$comment."\r\n";
if(isset($_POST['sales']))
{
$message.= "I am Interested in Sales" . "\r\n";
}
else if(isset($_POST['lettings']))
{
$message.= "I am Interested in Lettings";
}
echo $message;
$headers = "From: ".$name." <".$email.">";
$result = mail($to,$subject,$message,$headers);
return $result;
}
?>
<form id="register_form" name="register" method="post" action="testing_page.php">
<input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
<input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
<input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
<textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
<label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
<label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
<input id="submit" name="submit" type="submit" value="Submit" class="regsender" />
<div class="loading"></div>
</form>

Categories