0

My PHP script for sending email suddenly stopped working, it has worked all along and now when i was just trying it out again it instead showed the fault code. I see no syntax errors and im totally stumped.

Anyone who can figure out where the fault is here?

<?php
$namn = $_POST['namn'];
$epost = $_POST['epost'];
$tfn = $_POST['tfn'];
$msg = $_POST['msg'];
$from = 'B&P Contact';
$to = 'xxx@xxxxx.com';
$subject = 'Kontakt';

//message body
$body = "From: $name\n E-post: $epost\n Telefonnummer: $tfn\n Meddelande:\n $msg";
?>

<?php
//When send is clicked, execute function.
if ($_POST['send']) {
   if (mail ($to, $subject, $body, $from)) {
      echo '<p class="wtext" style="color:red;margin-left:25px;"> Message sent!</p>';
   }
 //error if somethings not working
   else {
      echo '<p class="wtext"> Something is wrong, try again.</p>';
   }
}
?>

I also have this code, but this has nothing to do with this not working, i have tried removing it etc. This code is for storing each msg in a database and then printing out the total number of posts in the db each time a msg is sent. (This is just for a school assignment).

<?php 
$conn = mysqli_connect('xxxxx', 'xxxxx', 'xxxx')
    or die('Could not establish connection to MySQL');
    $db_connected = mysqli_select_db($conn, 'xxxxx');
    //
    //
    $sql = "INSERT INTO Epostlist (namn, epost, tfn, msg) VALUES ('$namn', '$epost', '$tfn', '$msg')";
    //
    // Exikvera
    $status = mysqli_query($conn, $sql) or die (mysqli_error($conn));
    //
    //
    mysqli_close($conn);
?>

 <?php
      //Räkna meddelanden i databas
  $con=mysqli_connect("xxxx","xxxx","xxxxx","xxxxxx");
      // Check connection
      if (mysqli_connect_errno())
      {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      //
      $sql="SELECT msg FROM Epostlist ORDER BY ID";
      //
      if ($result=mysqli_query($con,$sql))
      {
      // Return the number of rows in result set
      $rowcount=mysqli_num_rows($result);
      //
      echo "<p class='wtext'>We have been sent a total of $rowcount messages.\n</p>";
      // Free result set
      //
      mysqli_free_result($result);
      }
      //
      mysqli_close($con);
?>

The code above also worked properly with the code above just yesterday.

*Form

<form method="post" action="send.php">
   Namn:<br><input type="text" name="namn" id="name"><br>
   E-post:<br><input type="text" name="epost" id="epost"><br>
   Telefonnummer:<br><input type="text" name="tfn" id="tfn"><br>
   Meddelande:<br><textarea name="msg" id="msg"></textarea><br>
   <input type="submit" name="send" value="Skicka" id="send"><br><br>
</form>
  • Have you checked the error logs? – Jay Blanchard Jan 12 '16 at 18:16
  • 1
    Did you change/update PHP or any other components on your server? And what exactly is not working? – E_p Jan 12 '16 at 18:16
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 12 '16 at 18:16
  • I agree with @E_p if you reallly changed none of your code, then I think it must be something with your mail server configuration. What are you using for your mail server anyway? – Wold Jan 12 '16 at 18:23
  • Instead of showing 'Message sent' it shows the error 'Something is wrong...'. I am using one.com's mail server so couldnt have changed anything there, im just curious about why the code itself does not work. – Douglas Pettersson Jan 12 '16 at 18:24
  • This piece of code is not working then.. if ($_POST['send']) { if (mail ($to, $subject, $body, $from)) { echo '

    Message sent!

    '; }
    – Douglas Pettersson Jan 12 '16 at 18:26
  • @DouglasPettersson You could not but one.com could :). Try code without any ifs and variables if it does not work contact one.com's support – E_p Jan 12 '16 at 18:29
  • Okay, i downloaded my files to my harddrive and tried running the code, (now without being connected to any mailserver) and it works properly. With the assignment we have we do not need to be connected to any mail server, just show that the code works, but i tried removing the connection to one.com's server, but the code still does not work then. Not sure why. – Douglas Pettersson Jan 12 '16 at 18:34
  • *"PHP send()"* ?? post your HTML form in your question and NOT in comments. I'll bet your form is failing you. – Funk Forty Niner Jan 12 '16 at 18:40
  • and your question looks an awful lot like your other previously posted http://stackoverflow.com/q/34664641/ – Funk Forty Niner Jan 12 '16 at 18:43
  • @Fred-ii- Why would my form be failing me since it has worked all along and i havent changed a thing? The code also successfully stores all information put in the form in the database, so its not the form. Posting it anyway. – Douglas Pettersson Jan 12 '16 at 18:46
  • please get more info about error from you error logs or get this info by method `error_get_last()`, most probably it's a problem with connection to sendmail server – manRo Jan 12 '16 at 18:54
  • http://php.net/manual/en/mail.configuration.php Check php.ini for configuration for both your computer and server – E_p Jan 12 '16 at 18:57

0 Answers0