0

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

无法添加收件人:@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>";
}
/*    
4

2 回答 2

1

用这个

$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_*函数 usemysqli_*PDOas mysql_*is are depreciated

于 2012-11-17T06:26:14.230 回答
0

我认为 @localhost 已添加到 Mail.php 中的 $email 变量中

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

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

于 2012-11-17T06:38:27.543 回答