PHP mail() not receiving the email [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm trying to set up a contact me page and currently I've got it working all the way to the point of the 'message sent' result showing up correctly. However the e-mail never shows up for me in my box inbox.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$antispam = $_POST['antispam'];
$to = 'myemailaddress#gmail.com';
$from = 'From : ' . $email;
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($antispam == '10' || 'Ten' || 'ten') {
$human = true;
}
if($_POST['submit'] && $name != "" && $email != "" && $message != "" && $subject != "") {
if ($human == true) {
if (mail($to, $subject, $body, $from)) {
$result = "Your message was sent successfully!";
} else {
$result = "Something went wrong! Try sending your message again";
}
} else {
$result = "You answered the anti-spam answer incorrectly. Please try again.";
}
} else {
$result = "You did not fill out a required field. Please try again.";
}
?>
<?php echo $result; ?>
I've read seperately that Gmail has issues with PHP mail(), is that possibly the cause?

I found the answer and honestly feel dumber for not recognizing it.
It was never told to send the email, just check if sending it would be true or not. I know that sounds weird but here's the old code:
if ($human == true) {
if (mail($to, $subject, $body, $from)) {
$result = "Your message was sent successfully!";
} else {
$result = "Something went wrong! Try sending your message again";
}
and here's the fixed version
if ($human == true) {
mail($to, $subject, $body, $from);
if (mail($to, $subject, $body, $from)) {
$result = "Your message was sent successfully!";
} else {
$result = "Something went wrong! Try sending your message again";
}
to be short, I added mail($to, $subject, $body, $from); after the check to see if $human == true

Related

Form Validation (PHP)

I need to create validation for this form, and I don't know how to do it right.
<?php
$errName = '';
$errEmail = '';
$errMessage = '';
$result = '';
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'simply#email.tld';
$to = 'again#email.tld';
$subject = 'Form';
$body = "Name: $name \n E-mail: $email \n Message: $message";
}
if (!$_POST['name']) {
$errName = 'Write Name here.';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Write correct e-mail';
}
if (!$_POST['message']) {
$errMessage = 'Write your message';
}
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result = "<div style='color:white;font-size:15px;font-weight:700;'>Your message has been sent.</div>";
} else {
$result = "<div style='color:red;font-size:15px;font-weight:700;'>Your message has not been sent, try again!</div>";
}
}
?>
The form works right but if as example I won't write one thing there is no error, message just isn't sent. Any ideas what's wrong?
The problem I see with your original code is that the variables that contain the error message ($errName, $errEmail, $errMessage) aren't ever echo'd anywhere. They simply get checked if they contain any content and if none of them do then the mail function is called, otherwise nothing.
I believe a better approach to this would be to use a try/catch block. Your approach continues checking for valid variables even if a previous variable has already failed a check and the mail is already going to be prevented because of it. In this application, a couple extra easy checks aren't going to amount to anything significant, resource-wise. But in a larger application it's a good idea to not waste resources if you already know something is going to fail.
I've rewritten your code using the suggested try/catch block.
<?php
if (isset($_POST["submit"])) {
$name = (string) $_POST['name'];
$email = (string) $_POST['email'];
$message = (string) $_POST['message'];
$from = 'simply#email.tld';
$to = 'again#email.tld';
$subject = 'Form';
$body = "Name: $name \n E-mail: $email \n Message: $message";
try {
if (!$name) {
throw new Exception('Write Name here.');
}
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Write correct e-mail');
}
if (!$message) {
throw new Exception('Write your message');
}
if (mail ($to, $subject, $body, $from)) {
$result = "<div style='color:white;font-size:15px;font-weight:700;'>Your message has been sent.</div>";
} else {
throw new Exception("Your message has not been sent, try again!");
}
} catch(Exception $e){
$result = "<div style='color:red;font-size:15px;font-weight:700;'>" . $e->getMessage() . "</div>";
}
echo $result;
}
?>
If a variable doesn't pass one of your checks, a new Exception is thrown with the applicable error message. This stops further execution in the try block and moves execution to the catch block. The $result variable gets filled with your styled error message, which gets echo'd at the end. Likewise, if the mail is successfully sent, the $result variable gets filled with the success message which gets echo'd.

PHP form submit success message inserts unwanted number after message

Created a php form with a success message that appears just below the Submit button upon successful mission. After adding some additional code in the php to create an email confirmation, I'm now noticing that a number "1" has been inserted in the line after my success message - see below:
Any ideas on how to make that number 1 go away? Code below:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: Page Name';
$to = 'email#mysite.com';
$subject = 'Service Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Company: $company\n
Message:\n $message";
// Confirmation email.
$conf_subject = 'Your recent inquiry';
$conf_sender = 'MY SITE <no-reply#mysite.com>';
$msg = $_POST['name'] . ",\n\nThank you for your recent inquiry. A member of
our team will respond to your message as soon as possible.\n\nThanks,\n\nMy
Company Team";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $phone != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thanks for your inquiry, we will get back to you as soon
as we can!</p>';
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender
));
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!</p>';
}
}
?>
Thanks everyone!
This line:
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender));
You are echoing out the result of your call to the mail function. Since the mail was successfully handed over to the server it returns true. When you echo out a boolean true it gets converted to an integer which is 1. That's why you see that in your code.
Remove the echo to remove the 1 from being displayed in your output.
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));
This code is causing the echo, its echoing 1 as the mail() function is returning true. I'm not sure why you're echoing the mail function here any way, just remove the echo and all is good.
That is because of this code:
echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));
You "echo" the result of the mail function. "1" is equal to "true" on mail sending success.

PHP Contact Form return code [duplicate]

This question already has answers here:
PHP Page shows up raw code
(3 answers)
Closed 8 years ago.
I have the following PHP script that runs when a send button is clicked.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit']) {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
I am using MAMP to host the website locally. The problem occurs when the send button is hit, instead of the email being sent a page with the code is shown.
What is wrong in my code and how may I fix it?
Thank You.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'shanaywork#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else{
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
There are lots of error in your code
The condition in if and in else if are same so the else if condition never execute.
While checking submit from post value use isset($_POST['NAME']).

Redirect after form submit PHP [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Im trying to redirect the user after submitting a form. Im currently using Header("Location:http://www.google.com") as a test, but it stays on my site after submitting.
What am I doing wrong?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Website';
$to = 'contact#joakimsorensen.se';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://www.google.com");
echo 'great';
} else {
echo '<span style="color:red"><p>Something went wrong, go back and try again!</p></span>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<span style="color:red"><p>You answered the anti-spam question incorrectly!</p></span>';
}
?>
It just prints out "great" from the echo.
I think the problem is that the header is already sent. Try to use
?>
<script type="text/javascript">
window.location="http://www.google.com";
</script>
<?php
instead of
header("Location: http://www.google.com");

PHP form redirect (header) in external php file not working

I have a form on my homepage, which when submitted runs an external form.php file containing the code below. I am testing on MAMP and the header redirect doesn't seem to be working the url is just stuck on the form.php url? I have previously had an echo function which worked fine!? What am I doing wrong? Please help, many thanks in advance
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$robots = $_POST['robots'];
$from = 'From: Blah Register Form';
$to = 'sofi.smith#blah.com';
$subject = 'Blah Lead';
$body = "From: $name\n E-Mail: $email\n company: $company\n ";
if ($_POST['submit'] && $robots == '') {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://google.com");
exit;
}
else {
echo '<p>Something went wrong, please try again</p>';
}
}
else if ($_POST['submit'] && $robots != '') {
echo 'Sorry, we don\'t like spammers here!';
}
?>
I can see a space before the first php tag, and you shouldn't close php tags either. That space means that content is sent before header.
However, your header statement can be failing due to several reasons (headers already sent, warning, etc). One quick dirty workaround is to use javascript for that matter:
if (mail ($to, $subject, $body, $from)) {
echo '<script>location.href="http://www.google.com";</script>';
exit;
}
You're condition will not always resolve to true in both scenarios. So unless you know for sure that the email is being sent, you don't know if any of the code is being called. This tightens it's up a bit:
if($_POST['submit']) {
if ($robots == '') {
if (mail($to, $subject, $body, $from)) {
header("Location: http://google.com");
} else {
echo 'Something went wrong, please try again';
}
} else {
echo 'Sorry, we don\'t like spammers here!';
}
} else {
echo "submit was not set";
}
Because right now you have:
if ($_POST['submit'] && $robots == '') {
// send the email or whatever
} else if ($_POST['submit'] && $robots != '') {
echo 'Sorry, we don\'t like spammers here!';
}
which means that if $_POST['submit'] is not set, neither condition would be true.
First of all write error_reporting(E_ALL); then you can see the exact error on page.
Also as i can see mail function it should be mail( $to , $subject , $message, $additional_headers )
I guess there is error in mail function thats why the header function is not working
Hope this works:
<?php
if($_POST) {
$name = mysql_real_escape_string(strip_tags($_POST['name']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$company = mysql_real_escape_string(strip_tags($_POST['company']));
$robots = mysql_real_escape_string(strip_tags($_POST['robots']));
$from = 'From: Blah Register Form';
$to = 'sofi.smith#blah.com';
$subject = 'Blah Lead';
$body = "From: $name\n E-Mail: $email\n company: $company\n ";
if($robots == '') {
$mail = mail($to, $subject, $body, $from);
if($mail) {
header('Location: http://google.com');
exit();
} else {
echo '<p>Something went wrong, please try again</p>';
}
} else if($robost != '') {
echo 'Sorry, we don\'t like spammers here!';
}
}
?>

Categories