1
<?php
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $contact = $_POST['num'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $ToEmail = 'info@kesems.com';
    $EmailSubject = 'School Enquiry';
    $mailheader = "From: ".$_POST["email"]."\r\n";
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
    $MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
    $MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
    header('Location:contact-us.php');
}
?>

contact-us.php

<form role="form" action="contact-us.php" id="main-contact-form" class="contact-form" name="contact-form" method="post">
    <div class="row ml0">
        <div class="form-group">
            <input type="text" class="form-control" name="name" required="required" placeholder="Name">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="num" required="required" placeholder="Contact number">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="email" required="required" placeholder="Email address">
        </div>
        <div class="form-group">
            <textarea name="message" id="message" required="required" name="message" class="form-control" rows="3" placeholder="Any Queries/suggestions" style="resize:none"></textarea>
        </div>
        <div class="form-group">
            <input type="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg"/>
        </div>
    </div>
</form>

Simple script that sends email headers...still not working.

is code is correct?

any particular solution for this? any particular solution for this? thank you in advance.

  • Are you sending it from server or local machine? – Pupil Nov 28 '14 at 12:36
  • In each case, you need to configure SMTP ports. – Pupil Nov 28 '14 at 12:36
  • I'm not putting in an answer for this, useless. Here, you've a missing concatenate for `$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";`. Debug your code with error reporting. – Funk Forty Niner Nov 28 '14 at 12:37
  • yes on server. I'm a beginner in this case. can you help me out, how can I configure that? – Chinmay Joshi Nov 28 '14 at 12:37
  • @Fred-ii- But still the mail can be sent. @Chinmay can you put a debug message after `if(isset($_POST['submit']))`? –  Nov 28 '14 at 12:38
  • This `$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";` is supposed to be `$MESSAGE_BODY .= "Phone: ".$_POST["num"]."\r\n";` @PHPWeblineindia that's why the headers are broken. – Funk Forty Niner Nov 28 '14 at 12:42
  • @Fred-ii- That is fine, but my point is: you can send anything in mail. Thus, no matter what the body is, the mail should have been sent. –  Nov 28 '14 at 12:44
  • 1
    @PHPWeblineindia OP's going to have to check for errors then. I see questions about forms everyday; 99% of the time, is because people either don't bother checking for errors, or don't know how to debug code. – Funk Forty Niner Nov 28 '14 at 12:46
  • @ChinmayJoshi make your error report on. And put debug point before mail function. What does it say? –  Nov 28 '14 at 12:48
  • Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Nov 28 '14 at 12:50
  • **1)** Has your mail server been started? **2)** Have you looked at the `maillog`? – ʰᵈˑ Nov 28 '14 at 12:55
  • Not answer to you question, but you also might improve your form using `` for email and `` for the contact number. See [link](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input) – John Nov 28 '14 at 12:55
  • 1
    Isnt the problem simply youre mailing to: `$ToEmail = 'info@example.com';` instead of `$_POST['email'];` ? – John Nov 28 '14 at 13:04

2 Answers2

0

Try this it worked for me

// To send HTML mail, the Content-type header must be set

                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= "From: yourmail@gmail.com" . "\r\n" .
                        "Reply-To: no-reply@gmail.com" . "\r\n" .
                        "X-Mailer: PHP/" . phpversion();
                $to = "tomail@gmail.com";
                $subject = "This is subject";
                $from = "yourmail@gmail.com";//optional
                $message = ' this is my message hello';
                if (mail($to, $subject, $message, $headers, 'ADMIN')) {
                 echo "mail sent"
                 }
              else{
               echo "error cannot send mail";
              }
0

Check whether all the arguments are actually set (using ternary), just because the submit is set does not mean that all the arguments are set:

$name = isset($_POST['name']) ? $_POST['name'] : "";
$contact = isset($_POST['num']) ? $_POST['num'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";

Check these values and make a condition that you display some error if something is not set.

If you're certain the values are set then you need to change

$ToEmail = 'info@example.com';

to

$ToEmail = $email;

Otherwise nothing's going to happen.

Finally, I recommend identifying your html inputs with a proper id, instead of a name.

Unrelated to the error but yet important to you

Like already pointed out by @Fred -ii-, a proper concatenation (.=) is required instead of overwriting (=) in the second header assignment of $MESSAGE_BODY:

MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";

Should be:

$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
Jonast92
  • 4,964
  • 1
  • 18
  • 32