0

Hei. I tried very hard to make this form send a email but cant make it work. I want the form to send email when the fields are filled correctly. I am not very good with PHP and found most code from others. I press the submit button and get comment under the form but no email. Tried error checking but i could not find out how to do it.

<?php
header('Content-Type: text/html; charset=ISO-8859-1');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//NB"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="nb" xml:lang="nb">

This part will check forms filled and submit it. It will publish comment on the site, store it in kommentarer.txt and supposed to send email with the comment detail.

<?php
$nameErr = $comErr = $emailErr = "";
$name = $comment = $email = "";
$server = 0;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (empty($_POST['email'])) {
        $emailErr = "Fyll ut din e-post adresse!";
        $server += 1;
        die ("Fyll ut din e-post adresse!");
    } else {
        $email = test_input($_POST['email']);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Skriv inn en e-post adresse!";
            $server += 1;
            die ("Skriv inn en e-post adresse!");
        }
    }
    if (empty($_POST['name'])) {
        $nameErr = "Fyll ut ditt navn, behøver ikke være ditt ekte!";
        $server += 1;
        die ("Fyll ut navne feltet!");
    } else {
        $name = test_input($_POST['name']);
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Bare bokstaver og mellomrom!";
            $server += 1;
            die ("Bare bokstaver og mellomrom!");
        }
    }
    $comment = test_input($_POST['comment']);
    if (empty($_POST['comment'])) {
        $comErr = "<br />Skriv din kommentar!";
        $server += 1;
        die ("Skriv din kommentar!");
    }

    $email = $_POST['email'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    if (!$email == "" && !$name == "" && !$comment == "") {
    $EmailSubject = 'something';
    $Email = 'alben_94@hotmail.com';
    $date=date("Y-m-d H:i:s");

    $EmailField = $_POST['email'];

$Body = <<<EOD

$name har kommentert på nettsiden din! http://xn--nes-entreprenr- 2qb.no/Kommenter.php

Kommentar: $comment
Navn: $name
Email: $EmailField
Dato: $date\r\n \r\n
EOD;

    $headers = "From: $EmailField\r\n";
    $headers .= "Content=type: text/html\r\n";
    $success = mail($Email, $EmailSubject, $Body, $headers);
    } else if ($server == 3){
        die ("Fyll ut feltene markert med *");
    }
}

   function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

HTML

<head>
    <!-- CSS sheets -->
    <link href="./csssheets/indexsheet.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="container">

<h1>NES-ENTREPRENØR A/S</h1>

<div id="nav">
<ul>
    <li><a href="./index.php">Hjem</a></li>
    <li><a href="./Om%20Oss.html">Om Oss</a></li>
    <li><a href="./Tjenester.html">Tjenester</a></li>
    <li><a href="./Priser.html">Priser</a></li>
    <li><a href="./Kontakt%20Oss.html">Kontakt Oss</a></li>
    <li><a href="./Kommenter.php">Kommenter</a></li>
    <li><a href="./Galleri.html">Galleri</a></li>
</ul>
</div>

<div id="inhold">
<h2>Legg gjerne dine kommentarer her!</h2>
<hr />
<div id="inhold2">
<div id="php">

<script>
function clearContents(element) {
element.value = '';
}
</script>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label> Din E-mail: <br><input type="text" name="email" id="email" onfocus="focusFunction()" onblur="blurFunction()" maxlength="50" size="30"></label>
<span class="error">* <?php echo $emailErr;?></span><br>

<label> Ditt Navn: <br><input type="text" id="clearinput" name="name" maxlength="20" onfocus="focusFunction2()" onblur="blurFunction2()" maxlength="30" size="30" value="<?php echo $name;?>"></label>
<span class="error">* <?php echo $nameErr;?></span>
<br>

<label> Din Kommentar: <br><textarea name="comment" id="comment" class="comment" rows="10" cols="50" maxlength="1000" style="font-family: arial; resize: none;" onfocus="focusFunction3()" onblur="blurFunction3()"><?php echo $comment;?></textarea><label>
<span class="error">* <?php echo $comErr;?></span>
<br>

<input id="button" type="submit" name="post" value=" Send ">
</form>

This will post comment on site and store it in txt file.

<?php
$post = $_POST["post"];

if($post){

$write = fopen("Kommentarer.txt", "a+");
$date=date("Y-m-d H:i:s");
fwrite($write, "<b>Navn:</b> $name<br /><b>Kommentar:</b><br>$comment<br><b>Sendt:</b> $date\r\n \r\n<hr style='width: 500px; background: #175B7D; padding-top: 1px; padding-bottom: 1px;'>");
fclose($write);

$read = fopen("Kommentarer.txt", "r+t");
echo "";

while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
else{
$read = fopen("Kommentarer.txt", "r+t");
echo "";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
?>

</div>
</div>
</div>
</div>
</body>
</html>

Thank you for all help if any!

Playhouse
  • 41
  • 4
  • 1
    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 25 '17 at 10:19
  • Does the code run without error? I don't get where the `if` is for `} else if ($server == 3){`. Add which error you get to the question. – Adder Aug 25 '17 at 10:19
  • The code run without error. – Playhouse Aug 25 '17 at 10:29
  • 1
    in that case, please describe what behaviour you're getting, vs what you expected. You haven't actually told us what goes wrong. There's a lot of code here. Presumably you've debugged/logged/stepped through the code to identify the moment at which the variable values/flow of control/output deviates from what you intended? Just from reading, I note that you never check whether the mail function returned a success value or not, so that could be one issue. – ADyson Aug 25 '17 at 10:36
  • Also it's worth understanding that _sending_ an email doesn't guarantee _receiving_ one, so you can't test your code fully just by waiting for the email to arrive. It's possible for your code to have worked perfectly and sent the email via `mail`, but for it still to never arrive at the destination for other reasons. If that happens you would need to understand your mail config and check server logs etc etc to try and find the fault. – ADyson Aug 25 '17 at 10:40
  • http://nes-entreprenør.no/Kommenter.php This is the behavius i get, its my first post in entire life sorry. i submit the form with the button and it doesnt send email like i want it to do. I get a comment posten under the form like i want but thats all. – Playhouse Aug 25 '17 at 10:48
  • We already know it doesn't send the email, that's in the question. What we need is debugging info and sample data. Have you stepped through the code to see whether the mail function gets called with the correct parameters? We don't know what data you're entering into it and what effect that could have. You can easily use an IDE to step through it, or if you haven't got that, do some var_dump commands to see the status of your variables at each stage, and to reveal what path the code is taking. – ADyson Aug 25 '17 at 12:50
  • Nono, i said i am not very good with php and other languages for that matter. I ask if it was possible for get some help around the code, if its not possible without debug something then its fine. Thank you for help – Playhouse Aug 25 '17 at 20:55
  • sorry, since you say it runs through without error, and apart from what I pointed out earlier about not checking the return value from `mail()`, there's nothing obvious wrong with the code, we would need sample input and debug info in order to help. – ADyson Aug 26 '17 at 07:35
  • could you please explain how i could implement that into this code? – Playhouse Aug 31 '17 at 15:01

0 Answers0