2013-03-13 212 views
0

當一個人註冊到我的網站時,我試圖將註冊添加到數據庫,然後通過電子郵件向他們發送確認。數據庫添加工作,但我的程序不會給他們發電子郵件。任何想法,我要去哪裏錯了?註冊確認php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 
<body> 
<div id="main"> 

<?php 
{ 
    include "base.php"; //connect to database 
    if(isset($_POST['register'])){ 
    $email = mysqli_real_escape_string($_SESSION['base'], $_POST['email']); 
    $password = md5(mysqli_real_escape_string($_SESSION['base'], $_POST['password'])); 
    $firstname = mysqli_real_escape_string($_SESSION['base'], $_POST['firstName']); 
    $lastname = mysqli_real_escape_string($_SESSION['base'], $_POST['lastName']); 
    $addressline1 = mysqli_real_escape_string($_SESSION['base'], $_POST['addressLine1']); 
    $addressline2 = mysqli_real_escape_string($_SESSION['base'], $_POST['addressLine2']); 
    $city = mysqli_real_escape_string($_SESSION['base'], $_POST['city']); 
    $county = mysqli_real_escape_string($_SESSION['base'], $_POST['county']); 
    $postcode = mysqli_real_escape_string($_SESSION['base'], $_POST['postCode']); 
    $phoneno = mysqli_real_escape_string($_SESSION['base'], $_POST['phoneNo']); 

    $checkemail = mysqli_query($_SESSION['base'], "SELECT * FROM Users WHERE Email = '".$email."'"); 
    } 

    if(mysqli_num_rows($checkemail) == 1) { 
?> 
     <h1>Register</h1> 

       <p>Please enter your details below to register.</p> 

       <form action='register.php' method='post' name='registerform' id='registerform'> 
        <label for='email'>Email:</label><input type='text' name='email' id='email' /><br /> 
        <label for='password'>Password:</label><input type='password' name='password' id='password' /><br /> 
        <label for='firstname'>First Name:</label><input type='text' name='firstName' id='firstName' /><br /> 
        <label for='lastname'>Last Name:</label><input type='text' name='lastName' id='lastName' /><br /> 
        <label for='addressline1'>Address Line 1:</label><input type='text' name='addressLine1' id='addressLinel' /><br /> 
        <label for='addressline2'>Address Line 2:</label><input type='text' name='addressLine2' id='addressLine2' /><br /> 
        <label for='city'>City:</label><input type='text' name='city' id='city' /><br /> 
        <label for='county'>County:</label><input type='text' name='county' id='county' /><br /> 
        <label for='postcode'>Postcode:</label><input type='text' name='postCode' id='postCode' /><br /> 
        <label for='phoneno'>Phone no:</label><input type='text' name='phoneNo' id='phoneNo' /><br /> 
        <input type='submit' name='register' id='register' value='Register' /> 
       </form> 
       <?php 
    echo "<h1>Error</h1>"; 
    echo "<p>Sorry, that Email is taken. Please go back and try again.</p>"; 
} 
else 
{ 
    $registerquery = mysqli_query($_SESSION['base'], "INSERT INTO Users (Email, Password, FirstName, LastName, AddressLine1, AddressLine2, City, County, PostCode, PhoneNo) 
    VALUES('".$email."', '".$password."', '".$firstname."', '".$lastname."', '".$addressline1."', '".$addressline2."', '".$city."', '".$county."', '".$postcode."', '".$phoneno."')"); 
    if($registerquery) 
{ 
$to = "email"; 
$subject = "Email Verification mail"; 
$headers = "From: [email protected] \r\n"; 
$headers .= "Reply-To: [email protected] \r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

$message = '<html><body>'; 
$message.='<div style="width:550px; background-color:#CC6600; padding:15px; font- weight:bold;">'; 
$message.='Email Verification mail'; 
$message.='</div>'; 
$message.='<div style="font-family: Arial;">Confiramtion mail have been sent to your email  id<br/>'; 
$message.='hello cameron '; 
$message.='</div>'; 
$message.='</body></html>'; 

mail($email,$subject,$message,$headers); 
} 

     echo "<h1>Success</h1>"; 
     echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>"; 

    } 
    else 
    { 
     echo "<h1>Error</h1>"; 
     echo "<p>Sorry, your registration failed. Please enter your details again. </p>"; 
    }  
} 
} 
else{ 
?> 
<h1>Register</h1> 

<p>Please enter your details below to register.</p> 

    <form action='register.php' method='post' name='registerform' id='registerform'> 
     <label for='email'>Email:</label><input type='text' name='email' id='email' /><br /> 
     <label for='password'>Password:</label><input type='password' name='password' id='password' /><br /> 
     <label for='firstname'>First Name:</label><input type='text' name='firstName' id='firstName' /><br /> 
     <label for='lastname'>Last Name:</label><input type='text' name='lastName' id='lastName' /><br /> 
     <label for='addressline1'>Address Line 1:</label><input type='text' name='addressLine1' id='addressLinel' /><br /> 
     <label for='addressline2'>Address Line 2:</label><input type='text' name='addressLine2' id='addressLine2' /><br /> 
     <label for='city'>City:</label><input type='text' name='city' id='city' /><br /> 
     <label for='county'>County:</label><input type='text' name='county' id='county' /><br /> 
     <label for='postcode'>Postcode:</label><input type='text' name='postCode' id='postCode' /><br /> 
     <label for='phoneno'>Phone no:</label><input type='text' name='phoneNo' id='phoneNo' /><br /> 
     <input type='submit' name='register' id='register' value='Register' /> 
    </form> 
<?php 
?> 
<html> 
<?php 
} 
?> 
</div> 
</body> 
</html> 
+2

您是在像WAMP或MAMP這樣的本地服務器上開發的嗎? – phpisuber01 2013-03-13 18:27:03

+1

你不應該在會話中存儲你的數據庫句柄。這沒有意義。至於電子郵件,你是否檢查了mail()調用的返回值。如果它是錯誤的,那麼PHP甚至不能將電子郵件交給交付系統。如果這是真的,那麼請檢查您的郵件服務器的日誌,以瞭解在PHP交付後電子郵件發生了什麼。 – 2013-03-13 18:27:20

+0

您需要一個有效的'from'地址指向您要發送的服務器。這是最重要的先決條件。 – 2013-03-13 18:28:41

回答

0

你試圖從你自己的網絡服務器,與本地主機和所有的電子郵件?因爲如果你想發送電子郵件,你需要有一個郵件服務器。對我而言,我不需要本地郵件服務器,因此我創建了該功能並在該站點的產品服務器上進行測試。

+0

我只是想通過電子郵件發送我的hotmail帳戶,所以它會和外部web服務器 – 2013-03-13 18:34:55

+0

從你的本地服務器?這是行不通的。您需要使用郵件服務器(SMTP服務器)配置Apache。 您可以在這裏閱讀更多一點:http://answers.oreilly.com/topic/3403-how-to-use-email-with-apache-and-php-on-localhost/ – Treps 2013-03-13 18:42:26

+0

感謝Treps,我將考慮到這一點! – 2013-03-13 18:45:55