2017-04-10 65 views
0

我試圖從提交到電子郵件地址的表單輸入字段發送數據,同時表單數據被插入到數據庫表中。我已成功地設法將表單數據提交到數據庫中,我現在正努力將表單數據作爲電子郵件發送。這是我的下面的PHP代碼。所有的PHP代碼都在一個文件中。我試圖將數據發送到的電子郵件地址不是Gmail,Hotmail等電子郵件,它是公司自己的郵寄地址。發送申請表並提交併插入到數據庫中

<?php 
$conn = mysqli_connect("", "", "", ""); --> deleted this because of this post 

if (!$conn) { 

die ("Connection failed: ". mysqli_connect_error()); 

} 

$course = $_POST ['course']; 
$firstname = $_POST ['firstname']; 
$surname = $_POST ['surname']; 
$DOB = $_POST ['DOB']; 
$gender = $_POST ['gender']; 
$address1 = $_POST ['address1']; 
$address2 = $_POST ['address2']; 
$city = $_POST ['city']; 
$postcode = $_POST ['postcode']; 
$mobile = $_POST ['mobile']; 
$home = $_POST ['home']; 
$email = $_POST ['email']; 
$residency = $_POST ['residency']; 
$learning = $_POST ['learning']; 
$qualifications = $_POST ['qualifications']; 

$sql = "INSERT INTO form (course, firstname, surname, DOB, gender, address1, address2, city, postcode, mobile, home, email, residency, learning, qualifications) VALUES ('$course', '$firstname', '$surname', '$DOB', '$gender', '$address1', '$address2', '$city', '$postcode', '$mobile', '$home', '$email', '$residency', '$learning', '$qualifications')"; 

$result = mysqli_query($conn, $sql); 

以上是代碼來獲取表單數據到數據庫中。下面的代碼是我嘗試將信息發送到一個電子郵件地址。

if(isset($_POST["submit"])){ 
// Checking For Blank Fields.. 
if(
    $_POST["course"]==""||          $_POST["firstname"]==""||          
    $_POST["surname"]==""|| 
    $_POST["DOB"]==""|| 
    $_POST["gender"]==""|| 
    $_POST["address1"]==""|| 
    $_POST["address2"]==""|| 
    $_POST["city"]==""|| 
    $_POST["postcode"]==""|| 
    $_POST["mobile"]==""|| 
    $_POST["home"]==""|| 
    $_POST["email"]==""||              
    $_POST["residency"]==""||              
    $_POST["learning"]==""||              
    $_POST["qualifications"]=="") 
{ 
    echo "Please ensure all fields are filled in."; 
} else { 
// Check if the "Sender's Email" input field is filled out 
$email=$_POST['email']; 
// Sanitize E-mail Address 
$email =filter_var($email, FILTER_SANITIZE_EMAIL); 
// Validate E-mail Address 
$email= filter_var($email, FILTER_VALIDATE_EMAIL); 
if (!$email){ 
echo "Invalid Email"; 
} 
else{ 

$course = $_POST['course']; 
$firstname = $_POST['firstname']; 
$surname = $_POST['surname']; 
$DOB = $_POST['DOB']; 
$gender = $_POST['gender']; 
$address1 = $_POST['address1']; 
$address2 = $_POST['address2']; 
$city = $_POST['city']; 
$postcode = $_POST['postcode']; 
$mobile = $_POST['mobile']; 
$home = $_POST['home']; 
$email = $_POST['email']; 
$residency = $_POST['residency']; 
$learning = $_POST['learning']; 
$qualifications = $_POST['qualifications']; 

$headers = 'From:'. $email . "\r\n"; // Sender's Email 
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender 

// Send Mail By PHP Mail Function 
mail("[email protected]", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications); 
echo "Your mail has been sent successfuly! We will get back to as soon as we can!"; 
     } 
    } 
} 

header("Location: apply.php"); 

以下是隻是在我的HMTL標籤的第一部分:

<form class="form" id="contact_form" action="submitApplication.php" method="post"> 

提交按鈕:

<button type="submit" name="submit" class="btn btn-primary btn-lg outline hvr-grow">Submit Application</button> 

一些補充,當我嘗試提交申請將上面的所有代碼都放在一個文件中,在提交時,沒有數據被髮送到數據庫,同時在同一個文件中使用電子郵件的代碼。如果我刪除同一文件中的電子郵件的代碼,則將申請表提交給數據庫。我必須儘可能簡潔,並希望有人能幫助我!謝謝!

+0

這表明你在電子郵件代碼語法錯誤。打開PHP錯誤報告並查看那裏顯示的內容。 –

+0

此外,您的代碼易受SQL注入的影響。請使用mysqli –

+0

準備好的語句[Little Bobby](http://bobby-tables.com/)表示*** [您的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/) 60174/how-can-i-prevent-sql -injection-in-php)***瞭解[MySQLi](http:/ /)的[prepared](http://en.wikipedia.org/wiki/Prepared_statement) /php.net/manual/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

回答

0

按這條線:

mail("[email protected]", $course, $firstname, $surname, $DOB, $gender, $address1, $address2, $city, $postcode, $mobile, $home, $email, $residency, $learning, $qualifications); 

這不是如何mail()工作http://php.net/manual/en/function.mail.php

的語法是:

布爾郵件(字符串$到,串$主題,串$ message [,string $ additional_headers [,string $ additional_parameters]])

  • 對於mail(),您使用的參數太多,應該只有四個參數。

爲所有要發送的變量指定一個變量,並將其設置爲「body」參數並使用手冊中的標題。

即:

$to = "[email protected]"; 
$subject = "Mail form submitted"; 

$headers = 'From:'. $email . "\r\n"; // Sender's Email 
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender 

$body = " 

$course \n $firstname \n $surname \n $DOB \n $gender \n 
$address1 \n $address2 \n $city \n $postcode \n $mobile \n 
$home \n $email \n $residency \n $learning \n $qualifications 

"; 

if(mail($to, $subject, $body, $headers)){ 

    echo "Mail sent"; 
} 
else { 
    echo "Error, check your logs"; 
    // header("Location: apply.php"); exit; 
    // use echo OR header, not both and uncomment/comment out the one you want. 
} 

您的代碼也是開放的SQL注入,用事先準備好的聲明:

同時確保所有POST數組是正確的並保持價值。

錯誤報告會的幫助下,應該有你的PHP任何錯誤:

你可能頭之前也可以輸出。

要麼刪除echo並將其替換爲標頭,反之亦然;你不能同時使用兩者。

如果您仍然無法發送郵件的問題,請諮詢以下是問答&答:

相關問題