0

I have mentioned everything properly in the form as far as I know, but it's not able to go to any of the emails that I specify. Please could you guide me?

<?php 
$name = $_POST[ name ]; 
$email = $_POST[ email ]; 
$message = $_POST[ message ]; 
$MobileNumber = $_POST[ MobileNumber ]; 
$AccountHolderName = $_POST[ AccountHolderName ]; 
$BankName = $_POST[ BankName ]; 
$BankAccountNumber = $_POST[ BankAccountNumber ]; 
$BranchAddressWithPinCode = $_POST[ BranchAddressWithPinCode ]; 
$IFSC = $_POST[ IFSC ]; 

$to = email@domain.com ; // this is where i put my email addres where i want 
to rec. the mail. 
$message = FROM: .$name. Email: .$email. Message: .$message; 
$headers = From: youremail@domain.com . "\r\n"; 

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we 
have a valid email address 
mail($to, $subject, $message, $headers); //This method sends the mail. 
echo "Your email was sent!"; // success message 
}else{ 
echo "Invalid Email, please provide a correct email."; 
} 

?>
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • This isn't your real code, is it? There's a bunch of missing quotes. And if you want to see what's posted, just `var_dump($_POST);` and you will see what it contains. – M. Eriksson May 09 '17 at 06:35
  • i have no knowledge about php but yet i'm attempting. it'd be great if you'd actually guide me on what's wrong with this code! – Ruhshad Daruwalla May 09 '17 at 06:37
  • 4
    You should _really_ learn the basics about PHP first, instead of asking us to debug your code for you. You should also have display errors on so you see all errors, which will help you. Here's how: http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – M. Eriksson May 09 '17 at 06:39
  • Short answer though, you need to quote strings and array keys. – M. Eriksson May 09 '17 at 06:41
  • Thank you Magnus. I got on stack overflow to know where I was going wrong cause some site really (may be Boasted) about the best people being on here but I wasn't aware that i'm going to be told to go back and learn php. – Ruhshad Daruwalla May 09 '17 at 06:43
  • Well, you have to make an effort by trying to learn and solve your issues yourself first. We will gladly help you when you get stuck, but we're not here to do all the work for you. We're all (most of us, anyway) here during our spare time. – M. Eriksson May 09 '17 at 06:48
  • Magnus! I'm not here for any kind of a project or something. I'm here cause a web developer in my area bluffed me on making my website but leaving my form incomplete. This isn't my line of business and I'm merely trying to get this contact form for my own company website live and running. I really think I did a big mistake trusting STACK OVER FLOW with this cause on the ADVERTISEMENT they never mentioned that you guys are here during spare time! Thanks, but no thanks! God Bless. – Ruhshad Daruwalla May 09 '17 at 06:52
  • Consider that SO is a community driven site (that means that I and 99.9% of all the members are simply users like you) and that it's 100% free, I'm not really sure what you are upset about. Are you angry because other people don't want to do the work for you for free? If your car breaks down, do you expect a mechanic to fix it for free? The fact that we still _will_ help you for free if you just put the effort in yourself first is actually pretty great. – M. Eriksson May 09 '17 at 06:57
  • Putting things across to you is like pouring water on a ducks back! I'm off this conversation and I'd be glad if anyone can give me a perfect answer for this. Thanks. – Ruhshad Daruwalla May 09 '17 at 07:00
  • 1
    Answer: Hire a PHP developer. – M. Eriksson May 09 '17 at 07:01

1 Answers1

0
<?php 
    /*
        unless you are using constants as the field value in the $_POST 
        you need to quote the field. Most of the variables here were never
        used by the mail script so are they important?
    */
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    $MobileNumber = $_POST['MobileNumber']; 
    $AccountHolderName = $_POST['AccountHolderName']; 
    $BankName = $_POST['BankName']; 
    $BankAccountNumber = $_POST['BankAccountNumber']; 
    $BranchAddressWithPinCode = $_POST['BranchAddressWithPinCode']; 
    $IFSC = $_POST['IFSC'];

    /*
        as above comment - many variables not used so here include
        everything from the POST request as a "belt & braces" approach
    */
    $data = array();
    foreach( $_POST as $key => $value )$data[]="{$key}: {$value}";
    $data=implode( PHP_EOL, $data );

    /*
        As with the other strings they need to be quoted. Single quotes do not
        allow you to include PHP variables within the string, double quotes do.
        Often useful when quoting php variables within a string is to use 
        curly braces around the variable - as seen here.
    */
    $to = 'email@domain.com';
    $message = "FROM: {$name} Email: {$email} Message: {$message}\n\n{$data}";
    $subject='New message';
    $headers = "From: youremail@domain.com\r\n"; 


    if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {

        /* capture the return value from `mail` to determine if the mail was sent correctly */
        $status = mail( $to, $subject, $message, $headers );
        $message = $status ? "Your email was sent!" : "There was a problem sending your message";

    }else{ 
        $message = "Invalid Email, please provide a correct email."; 
    } 

    echo $message;
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46