2017-05-09 87 views
0

据我所知,我已经在表单中正确提到了所有内容,但无法访问我指定的任何电子邮件。请你能指导我吗?在PHP中创建联系人表单并发送电子邮件

<?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 protected] ; // this is where i put my email addres where i want 
to rec. the mail. 
$message = FROM: .$name. Email: .$email. Message: .$message; 
$headers = From: [email protected] . "\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."; 
} 

?> 
+0

这是不是你真正的代码,是吗?有一堆缺少的引号。如果你想看看发布的内容,只需'var_dump($ _ POST);'你会看到它包含的内容。 –

+0

我没有关于PHP的知识,但我正在尝试。如果你真的指导我了解这段代码有什么问题,那会很棒! –

+4

您应该首先了解关于PHP的基础知识,而不是要求我们为您调试代码。你也应该有显示错误,所以你看到所有的错误,这将帮助你。以下是方法:http://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings –

回答

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 protected]'; 
    $message = "FROM: {$name} Email: {$email} Message: {$message}\n\n{$data}"; 
    $subject='New message'; 
    $headers = "From: [email protected]\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; 
?> 
相关问题