验证为请求密码而输入的电子邮件是否存在于数据库中

时间:2012-11-17 06:20:12

标签: php mysql database smtp echo

我有以下代码从数据库中检索用户的密码并通过电子邮件发送给他。如果用户的电子邮件存在于数据库中,我可以成功地向用户发送密码。但是,如果电子邮件不存在,我希望代码回应一下,数据库中不存在特定用户的电子邮件。如果在表单中输入了无效的电子邮件,我的代码会向我提供以下结果:

无法添加收件人:@localhost [SMTP:从服务器收到无效的响应代码(代码:555,响应:5.5.2语法错误.v9sm2318990paz.6)]

为此,我尝试使用if-else语句。这是我写的代码:

<?php

//Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

// email value sent from HTML form
$email_to=$_POST['email'];

// table name
$tbl_name="registration";

if($mysql1 = "SELECT ID,Email,Password FROM $tbl_name WHERE Email='$email_to' ORDER BY ID DESC ")
{
$selectemail = mysql_query($mysql1);

$shah       =   mysql_fetch_array($selectemail);
$EMAIL      =   $shah['Email'];
$UID        =   $shah['ID'];
$password           =       $shah['Password'];

require_once "/home/computat/php/Mail.php";

$from = "abhishekagrawal.988@gmail.com";
$to = $EMAIL;  

$subject = "Your password for www.computationalphotography.in";
$body    = "Your password for logging on to our website www.computationalphotography.in is:\n$password\r\nIf you have any additional queries, kindly write to us at abhishekagrawal.988@gmail.com\r\n\nThanks & Regards\nThe Computational Photography Team\n";   

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "abhishekagrawal.988@gmail.com";  //
    $password = "*********";

    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
     } else {
      echo("<p></p>");
     }
}
else{
echo "<b><center>Email not found in the database</center></b>";
}
/*    

2 个答案:

答案 0 :(得分:2)

使用此

$mysql1 = mysql_query("SELECT ID,Email,Password FROM $tbl_name WHERE Email='$email_to' ORDER BY ID DESC ");
if(mysql_num_rows($mysql1)){
      //exists in db
}
else{//does not exist}

避免使用mysql_*函数使用mysqli_*PDO,因为mysql_*正在折旧

答案 1 :(得分:0)

我认为@localhost已添加到Mail.php

中的$ email变量中

尝试更改脚本中$ email变量的名称。

你也可以使用die($ email);在您的脚本中测试各个阶段的电子邮件的价值。