-4
<?php

if(isset($_POST['email'])) {
    $user_email = $_POST['email'];          // this is just grabbed form the user email field
    $email_to   = "btndeals@btn.deals";     // where is the email going?
    $fullname   = $_POST['fname'];          // get the full name from hte $_POST data
    $subject    = $_POST ['subject'];       // what is the email subject?
    $message    = $_POST['message'];        // get the message from the $_POST data

    // ...geting data
    if(mail($email_to, $fname, $subject, $message, "From:" . $email)) {
        //the email got sent!!!
        //you can echo html out here :)
        echo "<strong>Sucessfully sent</strong>";
    } else {
        //email didn't work!
        //you can out html here :)
        echo "Error";
    }
}

?>

when running code on the server it keeps coming up with error could someone please help me, im new to php so im still learing how to use it.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Its possiblet hat your web provide disabled mail sending, you should check that out. – BlackNetworkBit Sep 05 '18 at 14:57
  • What server are you using? I am asking because in some servers you have to enable mail service – Krishanu Sep 05 '18 at 14:59
  • Hi Caolan, please provide more relevant information, for example what is the error message – Mario Sep 05 '18 at 15:00
  • I used liquid server if that helps – Caolan Hunt Sep 05 '18 at 15:00
  • 1
    You're using the `mail` function wrong. It takes four parameters, not five. Please check the function specification in the manual here: http://php.net/manual/en/function.mail.php EDIT: My mistake, it does take five parameters, two of them optional, but the parameter order you're using is wrong. The second parameter should be the subject and the third parameter should be the message. You're putting the subject and message as the third and fourth parameters respectively, so obvioulsy it's not working. – José A. Zapata Sep 05 '18 at 15:01
  • @José A. Zapata the amount of parameters is correct. We just need to know what happends ? does an php error appear or does the echo "Error"; print out ? – BlackNetworkBit Sep 05 '18 at 15:03
  • @José A - mail function can take 5 parameters - mail (to,subject,message,headers,parameters) – Fergal Andrews Sep 05 '18 at 15:04
  • $fname is not existing you mean $fullname thats the problem here... – BlackNetworkBit Sep 05 '18 at 15:05

2 Answers2

1

You're using the mail function wrong. The second and third parameters should be the subject and the message, respectively. The fourth (additional headers) and fifth (additional parameters) parameters are optional. Here's the code you need:

    if(isset($_POST['email'])) {
        $user_email = $_POST['email'];          //this is just grabbed form the user email field
        $email_to   = "btndeals@btn.deals"; //where is the email going?
        $fullname   = $_POST['fname'];         // get the full name from hte $_POST data
        $subject    = $_POST ['subject'];     //what is the email subject?
        $message    = $_POST['message'];        //get the message from the $_POST data

        $headers = "From:  $fullname <$user_email>";

        // ...geting data
        if(mail($email_to, $subject, $message, $headers)){
            //the email got sent!!!
            //you can echo html out here :)
            echo "<strong>Sucessfully sent</strong>";
        }
        else {
            //email didn't work!
            //you can out html here :)
            echo "Error";
        }
    }

If that still doesn't work, then please post the error message you're getting. Also remember you need to validate your $_POST['email'] parameter to ensure it's valid.

José A. Zapata
  • 1,187
  • 1
  • 6
  • 12
-1

This works I tested . The problem was sending, just make headers or no headers:-

       <?php
         if(isset($_POST['email'])) {
        $user_email = $user_email = $_POST['email']; ;          //this is just grabbed form the user email field
        $email_to   = "btndeals@btn.deals"; //where is the email going?
        $fullname   = $_POST['fname']; ;         // get the full name from hte $_POST data
        $subject    = $_POST ['subject']; ;     //what is the email subject?
        $message    = $_POST['message'];        //get the message from the $_POST data

        // ...geting data
        if(mail($email_to,$fullname, $subject, $message, $email)){
            //the email got sent!!!
            //you can echo html out here :)
            echo "<strong>Sucessfully sent</strong>";
        }
        else {
            //email didn't work!
            //you can out html here :)
            echo "Error";
        }
   } 
JeffB
  • 191
  • 1
  • 15
  • Wonder why downvote – JeffB Sep 05 '18 at 15:16
  • 1
    (wasnt me) I think the downvote was because you still have the parameters wrong in your `mail` example. – IncredibleHat Sep 05 '18 at 15:19
  • But the email was sent when I tried. – JeffB Sep 05 '18 at 15:20
  • Unsure how ;) Check the manual: http://php.net/manual/en/function.mail.php .. you have fullname in the subject, and subject in the message, and message as headers (which should explode at that point). – IncredibleHat Sep 05 '18 at 15:21
  • Maybe PHP gone crazy and sent it. Just fix it if you have any modification for it. – JeffB Sep 05 '18 at 15:21
  • There's no way PHP sent an email with the correct body and subject using this code. The fifth argument to `mail` is a list of additional CLI parameters to pass to sendmail (etc) - using an unescaped user-defined field for that is incredibly risky (although the fact `$email` isn't actually defined properly minimises that). There's nothing in this code that would stop an email being sent - it'll just have the wrong subject, body, and a potential massive security hole if the variable name typo gets fixed later. – iainn Sep 05 '18 at 15:24
  • @iainn I would appreciate if you would update the code as per the correct arguments. – JeffB Sep 05 '18 at 15:26