I have a problem with this code returning nothing but a blank page... I have followed several different tutorials to try and get this working, I am not a proficient PHP coder at all - But do have a little understanding. My server however, doesn't show error messages. So pinpointing this is rather hard for me to do!
I added this top section here to prevent spam using a hidden field on the html page that posts to this email page.
<?php
$if(isset($_POST['subject'],$_POST['Customer'],$_POST['Email'],$_POST['Phone'],$_POST['Comment'],$_POST['Product'],$_POST['Amount'],$_POST['Valid'])) {
$if(isset($_POST['Name']) && !empty($_POST['Name'])) {
echo "Spam Detected!";
Die();
}
Here is the email part that is supposed to send email once the above section determines that field "name" is empty.
$subject = $_POST['subject'];
$name = $_POST['Customer'];
$from = $_POST['Email'];
$phone = $_POST['Phone'];
$message = $_POST['Comment'];
$prod = $_POST['Product'];
$cash = $_POST['Amount'];
$valid = $_POST['Valid'];
$to = "email#email.com";
$body = 'HTML EMAIL CONTENT HERE...';
$headers .= "From: $from " . "Subject";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$subject,$body,$headers);
}
?>
Any and all help will be greatly appreciated!
Thanks
use this i see your code you use $if try if without $
if(isset($_POST['subject'],$_POST['Customer'],$_POST['Email'],$_POST['Phone'],$_POST['Comment'],$_POST['Product'],$_POST['Amount'],$_POST['Valid'])) {
if(isset($_POST['Name']) && !empty($_POST['Name'])) {
echo "Spam Detected!";
Die();
}
}
Related
Error:
the code:
<?php
$name = $_POST['name'];
$mail = $_POST['mail'];
$message = $_POST['message'];
$to ="bejidev27#gmail.com";
$subject = "New contact from " .$name;
$body = "";
$body .= "Name : " .$name. "\r\n";
$body .= "Email : " .$mail. "\r\n";
$body .= $message. "\r\n";
if($mail !=NULL){
mail($to, $subject, $body);
header("Location: index.html") ;
header("Location: done.html") ;
}
?>
It's not sending the mail nor the page is working i need help ,thanks in advance :D
First, check if your servers are running properly. Then, for the following
$name = $_POST['name'];
$mail = $_POST['mail'];
$message = $_POST['message'];
There need to be values for each you. If you are getting the values from a form, then you have to handle the form first using the 'isset' function, to be able to pass the values to each variables.
This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 12 months ago.
I have been trying to add form submit to my web form, so that it submits data to my email using POST.
So far this is what I have and it works perfectly:
<?php
if(isset($_POST['submit'])){
$user = $_POST['user'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$to = "your-webmail here";
$subject = "Applied Data";
$txt = "Name-'.$user.',<br>Phone -'.$phone.',<br>Email-'.$email.'";
$headers = "From: your#gmail.com" . "\r\n" .
$headers .= 'Cc: your#gmail.com' . "\r\n";
$ab=(mail($to,$subject,$txt,$headers));
if($ab)
{
echo"<script>alert('SignUp successfully.')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
else
{
echo"Failed";
}
}
?>
Is there a way to add a redirect to this code, so it redirects after form is submitted?
You have two options (probably more...)
Refactor your code to use AJAX to process the results of the form;
Don't display the headers until you know whether the form has been submitted / processed correctly. A redirect won't work if headers have already been sent.
As you've provided a non-AJAX problem:
<?php
if(isset($_POST['submit'])){
$user = $_POST['user'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$pageHeaders = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"; // etc.
$to = "your-webmail here";
$subject = "Applied Data";
$txt = "Name-'.$user.',<br>Phone -'.$phone.',<br>Email-'.$email.'";
$headers = "From: your#gmail.com" . "\r\n" .
$headers .= 'Cc: your#gmail.com' . "\r\n";
$ab=(mail($to,$subject,$txt,$headers));
if($ab)
{
header("Location: index.php");
die();
}
else
echo $pageHeaders; // You will need to create page headers
echo"Failed";
}
}
?>
This solution doesn't allow for an alert or message to indicate successful save. This could be achieved by redirecting to an interstitial page which can redirect after a timer. See here for an example:
use $_SESSION or $_COOKIE or a flag in your DB/in a temporary file to read & write.
for example:
if (isset($_SESSION['mail_sent']) && $_SESSION['mail_sent'] === true) {
redirect_to($target_url);
exit;
}
if(isset($_POST['submit'])) {
...
if (sending was successful) {
$_SESSION['mail_sent'] === true
}
else {
$_SESSION['mail_sent'] === false
}
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm trying everything but this PHP script show this error "Please fill out all the mandatory fields."
Please help me with how to solve this problem.
<?php
session_start();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
echo "Please fill out all the mandatory fields.";
}
} else {
echo "Your enquiry could not be sent for some reason; please try sending us again.";
}
?>
Your if else statements are positioned incorrectly.
They point to wrong conditions.
The rearranged code:
<?php
session_start();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($email, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
// #1: Swipe with #2
echo "Your enquiry could not be sent for some reason; please try sending us again."; // Flip this with #2
}
} else {
echo "Please fill out all the mandatory fields."; // #2
}
?>
Too many nested IF statements is a bad practice because it is difficult to debug. Instead, you should break them to small statements
For example:
<?php
session_start();
if !(isset($_POST['fullname']) || isset($_POST['email']) || isset($_POST['phone'])) {
echo "Your enquiry could not be sent for some reason; please try sending us again.";
exit();
}
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
echo "Please fill out all the mandatory fields.";
}
?>
Now the code becomes clearer and you can trace back. Seem your code has error at mail function so it return False, then the message shows up.
I made a form for contacting me on my website.
This is the php code:
<?php
$errors = '';
$myemail = 'yourname#website.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
"name: $name \n\n email: $email_address \n\n\n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact_thanks.html');
}
?>
When I use the form on the website, I receive a mail with weird symbols. That is because I write in Hebrew in the form- English letters are delivered well.
When I change "myemail" to another mail, the message is delivered well, even in Hebrew, but I want the message to pass to the first mail.
I changed many times the encoding, but none seems to solve the problem, perhaps I hadn't tried the correct one yet.
I am not the one who wrote the code and my I don't know php well, so please give me answers that are easy to comprehend.
You have to specify content encoding in your mail header. For this reason, add this line:
$headers .= "\nContent-type: text/html; charset=UTF-8";
I wrote a code using php, html and css. This an contact form that is received by an email after submission. There is no error while submitting but i am not getting the email. I am including the code here. This is the link: http://complaintdesk.byethost15.com/contact.php.
Also I incude the code here.
contact.php
<?php
if(isset($_POST['submit'])){
$name = $_POST['fullname'];
$branch = $_POST['branch'];
$usn = $_POST['usn'];
$sem = $_POST['sem'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$description = $_POST['comment'];
$to = 'email#example.com';
$display = 'From:</br>Name: $name</br>USN: $usn</br>Branch: $branch</br>Semester: $sem</br>Email: $email</br></br>$description';
mail($to,$subject,$display);
echo "<script>alert('Your Complaint has been succesfully submitted, We will contact you soon.')</script>";
};
?>
And the rest of code i am not including...
Try this:
$to_address = "Your email address";
$subject = $_POST['subject'];
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: ".$email."\r\n";
if (mail($to_address,$subject,$description,$headers)){
echo "Success";
}
else{
echo "<h2>Unable to submit the form, please recheck the details.</h2>" ;
}
Update: If this does not work then there might be an error with your email configuration. As suggested by other users in the comments above, you might want to look into that part too. This answer is based on assumption that your email server/PHP is configured correctly.