簡體   English   中英

制作聯系表格並使用PHP發送電子郵件

[英]Make Contact Form and Send Email in 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@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."; 
} 

?>
<?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;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM