0

I have index.html with the form :

<form method="post" action="mail.php">
                <div class="col-sm-7">
                    <div class="row">
                        <div class="col-sm-6 form-group">
                            <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>

                            </div>

                        <div class="col-sm-6 form group">
                            <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>

                        </div>
                    </div>

                    <textarea class="form-control" id="message" name="message" placeholder="Comment" rows="5"></textarea>

                    <div class="row">         
                        <div class="col-sm-12 form-group">
                            <button class="btn btn-default pull-right" type="submit">Send</button>
                        </div>
                    </div>
                </div>
                </form>

and mail.php:

     <?php

 // Check for header injection
 function has_header_injection($str){
 return preg_match("/[\r\n]/", $str);
 }

 if(isset($_POST['submit'])){

 $name = trim($_POST['name']);
 $email = trim($_POST['email']);
 $msg = $_POST['message'];


 // Check to see if $name or $email have header injections
 if(has_header_injection($name) || has_header_injection ($email)){
     die(); //if true kill the script
 }

 if(!$name || !$email || !$msg){
     echo '<h2>All fields required</h2><a href="mail.php" class="button block">Go back and try again</a>';
     exit;
 }


 // Add the recipient email to a variable
 $to = "minusemp@gmail.com";

 // Create a subject
 $subject = "$name sent you a message via your website";

 //Construct the message
 $msg  = "Name: $name\r\n";
 $msg .= "Email: $email\r\n";
 $msg .= "Message:\r\n$msg";

 $msg = wordwrap($msg, 72);

 // Set the mail headers into a variable
 $headers  = "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
 $headers .= "From: $name <$email>\r\n";
 $headers .= "X-Priority: 1\r\n";
 $headers .= "X-MSMail-Priority: High\r\n\r\n";


 // Send the email
 mail($to, $subject, $msg, $headers)
 }

 ?>

 <script language="javascript"> window.location="index.html";</script>

What im doing wrong? I tested this on localhost and on a hosted site and the same. When i submit with the form completed it just send me to a blank page mail.php, no error, with the script below php it sends me back to the main page (thats bc i want to). i tested adding:

if (mail($to, $subject, $body, $headers)===false) { echo "Not sent!"; } else { echo "Sent!"; }

but no error, it doesnt show me that it was sent or not either... so what am i doing wrong? Someone told me that i cant send myself emails so i put someone else to send an email for me... no luck, still no email sent.

Pls help im stuck at this for more than 3 days already :\

minus emp
  • 1
  • 1
  • 1
    `if(isset($_POST['submit'])){...}` will never happen; it's an undefined index. – Funk Forty Niner Jul 27 '16 at 14:30
  • need to install sendmail in your webserver. – Naisa purushotham Jul 27 '16 at 14:30
  • 3
    *"but no error"* - that's because you're not checking for them. – Funk Forty Niner Jul 27 '16 at 14:30
  • did you configure an smtp ? – Freelancer Jul 27 '16 at 14:30
  • 1
    `mail()`'s job is the equivalent of walking your envelope down to the street corner and tossing it into the mailbox. that's it. If the envelope drops into the box, mail's job is done and it returns boolean true. If the mailbox then gets destroyed by a drunk driver, that's not php's problem. you need to check your mail server's logs. 99.9999% likely your mail is getting rejected by the destination as spam. – Marc B Jul 27 '16 at 14:32
  • ok thanks, im just a newb at php, i will try your replies in order to fix it, i will come back if another error apears or if is still doesnt work. – minus emp Jul 27 '16 at 15:07
  • @Freelancer I set it now check if its ok pls `isSMTP(); $m->SMTPAuth = true; $m->SMTPDebug = 2; $m->Host = 'smtp.gmail.com'; $m->Username = 'minusemp@gmail.com'; $m->Password = ''; $m->SMTPSecure = 'ssl'; $m->Port = 465; $m->From = 'email'; $m->FromName = 'name'; $m->addAddress('minusemp@gmail.com', 'Liviu'); $m->isHTML(true); $m->Subject = 'Here is an email'; $m->Body = '

    This is the body of an email

    '; if($m->send()){ echo 'Email sent.'; }else { echo $m->ErrorInfo; }`
    – minus emp Jul 28 '16 at 07:26
  • But now i get : `Invalid address: (punyEncode) email Invalid address: (punyEncode) email` its not connected to index.html if i put action="mail.php" and inside .php to set from to 'emai' that i set in the .html file? or what im doing wrong? – minus emp Jul 28 '16 at 07:27

2 Answers2

0

index.html

<form method="post" action="mail.php">
<div class="col-sm-7">
    <div class="row">
        <div class="col-sm-6 form-group">
            <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>

        </div>

        <div class="col-sm-6 form group">
            <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>

        </div>
    </div>

    <textarea class="form-control" id="message" name="message" placeholder="Comment" rows="5"></textarea>

    <div class="row">
        <div class="col-sm-12 form-group">
            <button class="btn btn-default pull-right" type="submit" name="submit">Send</button>
        </div>
    </div>
</div>
</form>

mail.php

<?php

// Check for header injection
function has_header_injection($str){
    return preg_match("/[\r\n]/", $str);
}

if(isset($_POST['submit'])){

    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $msg = $_POST['message'];


    // Check to see if $name or $email have header injections
    if(has_header_injection($name) || has_header_injection ($email)){
        die(); //if true kill the script
    }

    if(!$name || !$email || !$msg){
        echo '<h2>All fields required</h2><a href="mail.php" class="button block">Go back and try again</a>';
        exit;
    }


    // Add the recipient email to a variable
    $to = "minusemp@gmail.com";

    // Create a subject
    $subject = "$name sent you a message via your website";

    //Construct the message
    $msg  = "Name: $name\r\n";
    $msg .= "Email: $email\r\n";
    $msg .= "Message:\r\n$msg";

    $msg = wordwrap($msg, 72);

    // Set the mail headers into a variable
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= "From: $name <$email>\r\n";
    $headers .= "X-Priority: 1\r\n";
    $headers .= "X-MSMail-Priority: High\r\n\r\n";


    // Send the email
    mail($to, $subject, $msg, $headers);
 }

?>

<script language="javascript"> window.location="index.html";</script>

You were missing a ; in the end of your mail() function. Aswell $_POST['submit'] is always undefined, so we had to add name="submit" to the button in index.html

Xatenev
  • 6,383
  • 3
  • 18
  • 42
0

I don't think your POST array contains a 'submit' key.

if(isset($_POST['submit'])){

So your entire script skips.