功能发送邮件两次

时间:2009-06-23 12:08:59

标签: php

我有以下功能两次发送电子邮件(我相信如果($ result)两次运行)。

在单独的页面上调用它:

<?php $User = new User();
  $User->ValidReg();
  $valid = $User->ValidReg();
  if ($valid === false) {

以下是同类中的函数:

public function ValidReg() {
  if (  !empty($_POST['username'])
     && !empty($_POST['password'])
     && !empty($_POST['email']) 
     && !empty($_POST['state'])) {

    //valid ?
    $valid = true;

    $username = mysql_real_escape_string($_POST['username']);
    $password = md5(mysql_real_escape_string($_POST['password']));
    $email = mysql_real_escape_string($_POST['email']);
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname = mysql_real_escape_string($_POST['lastname']);
    $state = mysql_real_escape_string($_POST['state']);

    $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");

    if(mysql_num_rows($checkusername) == 1) {
      echo "<div id='shopperlogin1'><p>Sorry, that username is taken.<br /> Please go back and try again.</p></div>";
    }
    else {
      //test
      $confirm_code=mysql_real_escape_string(md5(uniqid(rand())));

      $sql="INSERT INTO temp_users (
              confirm_code, Username, Password, 
              EmailAddress, FirstName, LastName, State) 
            VALUES (
              '$confirm_code', '$username', '$password', 
              '$email', '$firstname', '$lastname', '$state')";
      $result=mysql_query($sql) 
        or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

      // if suceesfully inserted data into database, send confirmation link to email
      if ($result) {

        // send e-mail to ...
        $to=$email;

        // Your subject
        $subject="Your confirmation link here";

        // From
        $header="blahblah@blahbalh.com";

        // Your message
        $message="Your Comfirmation link \r\n";
        $message.="Click on this link to activate your account \r\n";
        $message.="http://www.employeediscounted.com/secret/login.php?passkey=$confirm_code";

        // send email
        $sentmail = mail($to,$subject,$message,$header);
      }

      // if not found
      else {
        echo "<div id='emailmsg'>Not found your email in our database.</div>";
      }

      // if your email succesfully sent
      if($sentmail){
        echo "<div id='emailmsg'>Your Confirmation link Has Been Sent To Your Email Address.</div>";
      }
      else {
        echo "<div id='emailmsg'>Cannot send Confirmation link to your e-mail address.</div>";
      }
    }
  }
  else {
     $valid = false;
  }
  return $valid;
}

1 个答案:

答案 0 :(得分:6)

要么我缺少某些东西,要么只是简单地调用该函数两次:

$User->ValidReg();
$valid = $User->ValidReg();

所以,是的,你会发两封电子邮件!

(您是否希望第二次调用失败,因为用户已经存在?它不会因为您使用两个不同的表,userstemp_users。)