1

What is the problem with this code that can't send emails for me ?

if (isset($_POST['name']) and (isset($_POST['email'])) and (isset($_POST['text']))) {
     $name = $_POST['name'];
     $email = $_POST['email'];
     $text = $_POST['text'];


if (!empty($name) and !empty($email) and !empty($text)) {

$to = 'info@test34.com';
$subject = 'Testing';
$body = $text;
$headers = 'From : test@test.com';

if(mail($to, $subject,$body,$headers)) {
    echo 'Email Has bin sent.';
}
else echo "Can't Send mail";


}


}

I Have tried many times but the code is not working.

And the HTML is :

<html>
<form action="" method="POST">


Name : <br>
<input type="text" name="name">
<br><br>

Email : <br>
<input type="email" name="email">
<br><br>


Message : <br>
<textarea name="text" rows="6" cols="30"></textarea> 
<Br><br>
<input type="submit" value="Send">
</form>
</html>

What should i do now ? Please Help me :(

Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131

2 Answers2

3

Tested (use exactly as posted)

The problem seems to be caused by the space between From and the colon : in

$headers = 'From : test@test.com';
                ^-- right there

Upon testing, Email was not received till I removed the space between them.

I also added an extra conditional statement for the submit button adding name="submit".

Using the following, Email was successfully sent and received.

<?php
if (isset($_POST['submit'])){

if (isset($_POST['name']) && (isset($_POST['email'])) && (isset($_POST['text']))) {
     $name = $_POST['name'];
     $email = $_POST['email'];
     $text = $_POST['text'];

if (!empty($name) && !empty($email) && !empty($text)) {

$to = "info@test34.com";
$subject = 'Testing';
$body = $text;
$headers = 'From: test@test.com';

if(mail($to, $subject,$body,$headers)) {
    echo 'Email has been sent.';
    }
else { echo "Can't Send mail"; }

    }

    }

} // closing brace for if (isset($_POST['submit']))

?>

<html>
<form action="" method="POST">

Name : <br>
<input type="text" name="name">
<br><br>

Email : <br>
<input type="email" name="email">
<br><br>

Message : <br>
<textarea name="text" rows="6" cols="30"></textarea> 
<Br><br>
<input type="submit" name="submit" value="Send">
</form>
</html>

Footnotes: If you don't want the form to reappear after the user clicks submit, you can use the following:

if(mail($to, $subject,$body,$headers)) {
    echo 'Email has been sent.';
echo "<br>";
echo '<a href="index.php">Click here</a> to return to our Website.';
exit; // this is the command to use in order not to show the form after
}
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
1

try this one. change and to AND

if (isset($_POST['name']) AND (isset($_POST['email'])) AND (isset($_POST['text']))) {
     $name = $_POST['name'];
     $email = $_POST['email'];
     $text = $_POST['text'];


if (!empty($name) AND !empty($email) AND !empty($text)) {

$to = 'info@test34.com';
$subject = 'Testing';
$body = $text;
$headers = 'From : test@test.com';

if(mail($to, $subject,$body,$headers)) {
    echo 'Email Has bin sent.';
}
else echo "Can't Send mail";


}


}
Mohammad Nurdin
  • 20,761
  • 38
  • 117
  • 280