-1

My website allows the users to send an email to the email-address given which is saved in my database. Now that my website is live, I'm testing stuff like this, and it turns out that my contact.php is not working. I always get the message Sorry, there was an error sending your message. when I try to send an email.

My Contact.php has the following code

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'dbcontroller.php';
if (isset($_POST['sendMessage'])) {
    if(empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['message'])) {
        echo "<script type='text/javascript'>alert('Please fill all the fields.');window.location.href='contact.php'</script>";
    }
    else{
        $sql = "SELECT * from contact";
        $result = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($result);
        $to = $row['email'];
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $mailfrom = $_POST['email'];
        $message = $_POST['message'];
        $headers = "From:" . $firstname . " " . $lastname . "\r\n" . $mailfrom;
    if (mail($to, $message, $headers)) {

     echo "<script type='text/javascript'>alert('Your email has been sent! Thank you for contacting us!');window.location.href='contact.php'</script>";
    }
    else{
        echo "<script type='text/javascript'>alert('Sorry there was an error sending your message.');window.location.href='contact.php'</script>";
        }
}
} 
?>

And this is my contact form which is in the same page with the PHP code.

<form method="post" action"">

    <label>First Name </label>
<input type="text" class="form-control" placeholder="Firstname" name="firstname" required="required" />

    <label>Last Name</label>
<input type="text" class="form-control" placeholder="Lastname" name="lastname" required="required"/>

    <label>Email-address</label>
<input type="email" class="form-control" placeholder="Email" name="email" required="required" maxlength="200"/>

    <label>Message </label>
<textarea name="message" class="form-control" required="required" rows="4" cols="50" maxlength="500"></textarea>

    <input type="submit" name="sendMessage" value="Send Email" class="btn btn-primary pull-right"/>
</form>
Felix
  • 85
  • 1
  • 10
  • Hard to say. There could be more reasons. Are you testing on your personal computer, or on a server? Because you may not have a SMTP server installed and therefore you cannot send mails. – Ibrahim Apr 16 '16 at 10:35
  • I am testing on a SMTP server which is installed in my pc. @Ibrahim – Felix Apr 16 '16 at 10:38
  • I think I've spotted an error in your code. The php mail function takes parameters in the following order: to, subject, message, headers. Your code sends in this order: to, message, headers. You have omitted the subject. – Ibrahim Apr 16 '16 at 10:40
  • your query is $sql = "SELECT * from contact"; and this will select all rows, meaning that you are getting a multidimensional array as a response from the database. – Marc Steven Plotz Apr 16 '16 at 10:40
  • Oh, okay. So it is required to include a subject in sending mail? @Ibrahim – Felix Apr 16 '16 at 10:41
  • Yes, the subject is mandatory. You can give an empty string, but you must give the parameter. – Ibrahim Apr 16 '16 at 10:42
  • I thought so too but when I tried `SELECT email from contact;` nothing shows on my contact page. It just says that `this page cannot handle the request.` @MarcStevenPlotz – Felix Apr 16 '16 at 10:43
  • Even that query will select all rows, you need to specify a WHERE clause in your query to select only one row. – Marc Steven Plotz Apr 16 '16 at 10:44
  • Okay, will try that one @Ibrahim i hope it'll work. :) – Felix Apr 16 '16 at 10:45
  • and even then, you would have to use $result[0]['email'] or loop through the results. – Marc Steven Plotz Apr 16 '16 at 10:46
  • I'm not really sure how to do that because the user who is sending the mail can be unregistered from the website. Or should I not allow that? @MarcStevenPlotz – Felix Apr 16 '16 at 10:47
  • I did what you said but it's still the same. thanks tho :) @Ibrahim – Felix Apr 16 '16 at 10:51
  • Why do you need to get the email address out of the database then? Are you trying to send their email back to them? Or to you? – Marc Steven Plotz Apr 16 '16 at 10:52
  • Try accessing the SMTP server logs and see what actually failed since it's not so obvious what is the error. – Ibrahim Apr 16 '16 at 10:53
  • I need to get the email from the database because that's where the mail should be sent. I'm trying to send their email to me/to the email address from the database @MarcStevenPlotz – Felix Apr 16 '16 at 10:54
  • Can you tell me how can I do that? i'm sorry, I've never done this before. @Ibrahim – Felix Apr 16 '16 at 10:56
  • You have to find out where are the log files saved and open them. This depends from server to server. If you tell me what server are you using, I can look up for you. – Ibrahim Apr 16 '16 at 11:00
  • okay, will look for that @Ibrahim – Felix Apr 16 '16 at 11:07
  • didn't notice your next statement. I am using WinSCP btw @Ibrahim – Felix Apr 16 '16 at 11:15

1 Answers1

0

At first, what I can see is, that $fn and $ln is not filled in any place, so they are blank values. This could be one cause. The other thing I see is the headers. I think they are missing some minimal things. The following is what I always use:

[snipp]

$headers = "From: $botmail\n"; // Who the email was sent from
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "X-Priority: $priority\n"; // The priority of the mail

$success = mail($receivermail, $subject, $message, $headers);
Hans Meiser
  • 110
  • 7
  • fn and ln was the first variables i used, i forgot to change that, thanks for pointing that out, i'll try what your answer. thank you :) – Felix Apr 16 '16 at 11:21
  • Tried this but the result is still the same. I think I need to figure out how to use the right query on this situation. Thanks anyway sir. – Felix Apr 16 '16 at 11:32
  • To solve the problem, I suggest to first make sure that you get all the needed info from the database. Then, build the mail with the headers and stuff. Maybe you find the issue on the way. – Hans Meiser Apr 17 '16 at 00:21