mail()函数发送2个相同的电子邮件

时间:2013-09-06 19:31:18

标签: php email

我有一个用户激活脚本,可以发送电子邮件来激活他的帐户。我的问题是服务器发送相同的电子邮件2次。有没有什么办法解决这一问题?这是我的剧本

<?php
 include ('global.php');

 if($_POST['submit']){
$username = addslashes(strip_tags($_POST['username']));
$password = addslashes(strip_tags($_POST['password']));
$email = addslashes(strip_tags($_POST['email']));
$enpass = $_POST['password'];
$ip = $_SERVER['REMOTE_ADDR'];
if(empty($username) || empty($password) || empty($_POST['email']) || $_POST['test'] == 'pcs' || strlen($username) < 6 || strlen($username) > 25 || strlen($password) < 6 ){
echo "<center>";
if(empty($username)){
echo "<font color='color' size='25'>*</font>Username is required."."<br>";
}

                if(empty($password)){
                    echo "<font color='color' size='25'>*</font>Password is required."."<br>";
                }

                if(empty($_POST['email'])){
                    echo "<font color='color' size='25'>*</font>Email is required."."<br>";
                }

                if($_POST['test'] == 'pcs'){
                    echo "<font color='color' size='25'>*</font>Please choose something."."<br>";
                }
                if(strlen($username) < 6 || strlen($username) > 25){
                    echo "<font color='color' size='25'>*</font>Username must be between 6 and 25 characters!"."<br>";
                }

                if(strlen($password) < 6){
                    echo "<font color='color' size='25'>*</font>Password must be atleast 6 characters!"."<br>";
                }
    }else{
        $password = md5($password);

        $check = mysql_query("SELECT * FROM users WHERE username='$username'");
    if(mysql_num_rows($check)>=1)
            echo "User already taken";
        else{
        $code = rand(11111111, 99999999);
        //apostoli email
            $email = $_REQUEST['email'] ;
                    $subject = "Email activation";
                    $headers = "From: noreply_bbf-activation";
                    $headers .= "CC: susan@example.com\r\n";
                    $headers .= "MIME-Version: 1.0\r\n";
                    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

                    $message = "You have successully registered to Block Builder Fighter! \nTo activate  your account click the link below or paste it into the URL bar of your broswer:\n\n\nhttp://bbf-gima.hostoi.com/user/active.php?code=$code\n\n<?php echo 'works'; ?>";
                    mail($email, $subject, $message, "From: no-reply_BBF");
                if(!mail($email, $subject, $message, "From: no-reply_BBF"))
                    echo "We couldn't sign you up at this time. Please try again later.";
                else{
                    //register ton xristi
                    $query = mysql_query("INSERT INTO users (`id`, `username`, `password`, `email`, `registered_ip`, `code`, `active`) VALUES ('', '".$username."', '".$password."', '".$email."', '".$ip."', '".$code."', '0')");
                    echo "You have been registered successfully! Please check your email($email) to activate your account. If you don't see any email check your spam folder.";
                }
        }
    }
 }
 ?>

这是一张图片:

3 个答案:

答案 0 :(得分:2)

删除标记的行:                    $ message =“您已成功注册到Block Builder Fighter!\ n要激活您的帐户,请单击下面的链接或将其粘贴到您的broswer的URL栏中:\ n \ n \ nhttp://bbf-gima.hostoi.com/ ?用户/ active.php代码= $区号\ n \ n“个; / *删除或标记为评论邮件($ email,$ subject,$ message,“From:no-reply_BBF”); * /                 if(!mail($ email,$ subject,$ message,“From:no-reply_BBF”))

再试一次

答案 1 :(得分:1)

您正在拨打mail()两次:

mail($email, $subject, $message, "From: no-reply_BBF");
if(!mail($email, $subject, $message, "From: no-reply_BBF"))

应该是

$sent = mail($email, $subject, $message, "From: no-reply_BBF");
if(!$sent)

答案 2 :(得分:1)

更改以下行:

        mail($email, $subject, $message, "From: no-reply_BBF");
        if(!mail($email, $subject, $message, "From: no-reply_BBF"))

要:

            $isSent = mail($email, $subject, $message, "From: no-reply_BBF");
            if(!isSent)

应该解决问题。

另外,作为旁注,您应该使用mysqli代替mysql