submitting contact form - php

I am facing problems in sending e-mail through my contact form
I can not determine where is the problem I think that I have done everything right but I am here to take the advice and I hope you will help me.
<?php
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['body']) && isset($_POST['subject'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['body'];
$subject = $_POST['subject'];
$to = 'samir1986#gmail.com';
$header = 'From: '.$name.'<'.$email.'>';
if(!empty($name) && !empty($email) && !empty($body)){
mail($to, $subject, $message,$header);
echo 'An e-mail have been sent successfully';
}
else{
echo 'Please try sending e-mail again';
}
}
?>

Please make sure you've checked the value of each of your fields (for security reasons) then do :
<?php
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['body']) && !empty($_POST['subject'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['body'];
$subject = $_POST['subject'];
$to = 'samir1986#gmail.com';
$header = 'From: '.$name.' <'.$email.'>'."\r\n";
if (mail($to, $subject, $message, $header))
{
echo 'An e-mail have been sent successfully';
}
else
{
echo 'Please try sending e-mail again';
}
}
?>

Related

This script show me this message every time "Please fill out all the mandatory fields." [duplicate]

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.

recaptcha not sending email after verify in php

<?php
if(isset($_POST['submit']))
{
$email=$_POST['email'];
$comment=$_POST['comment'];
$captcha=$_POST['g-recaptcha-response'];
if(!$captcha)
{
echo 'Please check the the captcha form.';
}
$response=file_get_contents("https://www.google.com/recaptcha/api/sitev erify?secret="secretkey" &response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
header('Location:http://mywebsite.com/.errordocs/404.html');
}
else
{
header('Location:http://mywebsite.com/thankyou.php');
}
}
if (isset ($_POST['Side_Form'])){
$name = $_POST['name'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with Enjoy's contact e-mail)
$to = 'somebody#somewhere.com';
$subject = "Main Contact Form";
$message = "Name: $name
Email: $email
Phone: $phone
Comments: $comments";
$headers = "From: $email \r\n";
mail($to, $subject, $message, $headers);
}
?>
The problem is when the recaptcha is verified on the form, it displays the checkmark, user can click submit, but I get a blank screen and email doesn't send. Any help is Greatly appreciated, have been working on this for about 3 weeks.
your currently redirecting before actually sending the email. here is your code restructured. also fixed the recaptcha response processing
<?php
if (isset($_POST['submit'])){
$email = $_POST['email'];
$comment = $_POST['comment'];
$captcha = $_POST['g-recaptcha-response'];
if (! $captcha){
echo 'Please check the the captcha form.';
exit();
}else{
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretkey . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$responseKeys = json_decode($response,true);
if (intval($responseKeys["success"]) !== 1){
header('Location:http://mywebsite.com/.errordocs/404.html');
exit();
}else{
if (isset($_POST['Side_Form'])){//not sure what this is, hopefully you do :-)
$name = $_POST['name'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with Enjoy's contact e-mail)
$to = 'somebody#somewhere.com';
$subject = "Main Contact Form";
$message = "Name: $name
Email: $email
Phone: $phone
Comments: $comments";
$headers = "From: $email \r\n";
mail($to,$subject,$message,$headers);
header('Location:http://mywebsite.com/thankyou.php');
exit();
}
}
}
}
?>

my php mail function doesn't seem to be sending me an email

My email function isn't working on my website. When I complete the form it doesn't send to my email address? What am I missing? Can anyone help steer me in the right direction?
<?php
// form submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// list variables
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$company = $_POST['company'];
$industry = $_POST['industry'];
$phone = $_POST['phone'];
// email
$to = 'myemail#example.com';
$subject = 'Contact Form:';
$emailBody = "
Hi,
<br /><br />
<strong>Name</strong> : $name.<br />
<strong>Phone</strong> : $phone.<br />
<strong>Email</strong> : $email.<br />
<strong>Company</strong> : $company.<br />
<strong>Industry</strong> : $Industry.<br />
<strong>Comments</strong> : $comment.";
// mail
if ($_POST['name']) {
} else {
echo "please specify your name<br>";
}
if ($_POST['email']) {
} else {
echo "please specify your email<br>";
}
if ($_POST['comment']) {
echo "Hi, $name, <br>
Thank you for the following comments: <br> <em>$comment</em>
<br>I will get back to you shortly at $email";
} else {
echo 'please specify your request';
}
}
if ($name && $email && $comments) {
mail('$to', '$subject', 'From: $emailBody');
}
?>
Many thanks for your help
Basic PHP: You're using the WRONG string types:
mail('$to', '$subject', 'From: $emailBody');
^---^---
'-quoted strings do NOT expand variables. You're trying to send the email to an address which is the literal characters $, t, and o. For such "just a variable" usage, you won't need quotes AT ALL:
mail($to, $subject, "From: $emailBody");
^--- ^--- note the different quotes here
To add to Marc's answer: (compliment, I should say)
This if ($name && $email && $comments) should be if ($name && $email && $comment) since you have $comment = $_POST['comment']; therefore no variable match, the conditional statement fails because of it, and so will the mail() function.
Change to:
if ($name && $email && $comment) {
mail($to, $subject, "From: $emailBody");
}

php mail not returning true or false, nor is it returning a specified message

I am new to php and I am having issues with the php mail function. I am testing on a live server, not localhost. When I complete my form I get no response from the mail function. I'm not sure what i'm doing wrong.
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
if (isset($_POST['fullname'])) { $fullname = $_POST['fullname']; }
if (isset($_POST['email'])) { $email = $_POST['email']; }
if (isset($_POST['phone'])) { $phone = $_POST['phone']; }
if (isset($_POST['form_message'])) {
$form_message = filter_var($_POST['form_message'], FILTER_SANITIZE_STRING); }
$form_errors = false;
if( $fullname === '') :
$form_errors = true;
endif;
if ($form_message === '') :
$form_errors = true;
endif;
if (!$form_errors) :
$to = "email#address.com";
$subject = "From $fullname --VTS Specialist Contact Form";
$message = "$form_message";
$replyto = "From: $email \r\n" .
"Reply-To: email#address.com";
if(mail($to, $subject, $message)):
$msg = "Thanks for reaching out to VTS Specialist, We will get back to you as soon as possible!";
else:
$msg = " Sorry your message could not be sent, try again.";
endif; # mail form data
endif; #check for form errors
endif;
?>
Try adding an echo statement at the bottom - as Mario mentions, you are only declaring it, you're not displaying it.
if(mail($to, $subject, $message)):
$msg = "Thanks for reaching out to VTS Specialist, We will get back to you as soon as possible!";
else:
$msg = " Sorry your message could not be sent, try again.";
endif; # mail form data
echo $msg;

PHP 'email sent' but not received

Stuck on a simple php mail thing. Can anybody spot where I've gone wrong. Would be happy for any help.
<?php
$to = "example#website.co.uk";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
mail($name, $email, $subject, $message);
if(mail($name, $email, $subject, $message)) {
echo "E-Mail Sent";
} else {
echo "There was a problem";
}
?>
The first comment pretty much said it all, if you need a beginner-friendly tutorial of the mail() function, you should check out PHP Sending E-mails
<?php
$to = "example#website.co.uk";
$name = 'From:'.$_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if(mail($to, $subject, $message, $name)) {
echo "E-Mail Sent";
} else {
echo "There was a problem";
}
?>
Try this instead, it would need spam proofing etc...
<?php
$to = 'example#example.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $message, $headers);
?>

Categories