0

我在注册过程中有以下功能,将在完成注册过程后向用户发送电子邮件。我希望电子邮件的文本根据他们是什么类型的用户而有所不同。我有商业用户和个人用户,它们在数据库中被标识为 1 或 0。这就是我到目前为止所拥有的。

 function SendBusWelcomeEmail(&$user_rec)
    {
 $result = mysql_query("SELECT business FROM users")
               or die(mysql_error());

                while ($row = mysql_fetch_array($result))
            {

            $actiontype = $row['business'];
            switch ($actiontype)
{
case "0":
        $mailer = new PHPMailer();

        $mailer->CharSet = 'utf-8';

        $mailer->AddAddress($user_rec['email'],$user_rec['name']);

        $mailer->Subject = "Welcome to ".$this->sitename;

        $mailer->From = $this->GetFromAddress();        

        $mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n".
        "Welcome! Your registration  with ".$this->sitename." is completed.\r\n".
        "\r\n".
        "Regards,\r\n".
        "Webmaster\r\n".
        $this->sitename;

        if(!$mailer->Send())
        {
            $this->HandleError("Failed sending user welcome email.");
            return false;
        }
        return true;
break;
case "1":
  $mailer = new PHPMailer();

        $mailer->CharSet = 'utf-8';

        $mailer->AddAddress($user_rec['email'],$user_rec['name']);

        $mailer->Subject = "Welcome to ".$this->sitename;

        $mailer->From = $this->GetFromAddress();        

        $mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n".
        "Welcome! thanks for your business etc etc with ".$this->sitename." is completed.\r\n".
        "\r\n".
        "Regards,\r\n".
        "Webmaster\r\n".
        $this->sitename;

        if(!$mailer->Send())
        {
            $this->HandleError("Failed sending user welcome email.");
            return false;
        }
        return true;
    }
4

1 回答 1

0

可能是这样的。

function SendBusWelcomeEmail(&$user_rec)
{
    $mailer = new PHPMailer();
    $mailer->CharSet = 'utf-8';
    $mailer->AddAddress($user_rec['email'], $user_rec['name']);
    $mailer->Subject = "Welcome to ".$this->sitename;
    $mailer->From = $this->GetFromAddress();        

    $result = mysql_query("SELECT business FROM users") or die(mysql_error());
    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_assoc($result);

        $actiontype = $row['business'];
        switch ($actiontype)
        {
            case "0":
                $mailer->Body = "Hello ".$user_rec['name']."\r\n\r\n"."Welcome! Your registration  with ".$this->sitename." is completed.\r\n"."\r\n"."Regards,\r\n"."Webmaster\r\n".$this->sitename;
                break;

            case "1":
                $mailer->Body = "Hello ".$user_rec['name']."\r\n\r\n"."Welcome! thanks for your business etc etc with ".$this->sitename." is completed.\r\n"."\r\n"."Regards,\r\n"."Webmaster\r\n".$this->sitename;
                break;
        }

        if (!$mailer->Send())
        {
            $this->HandleError("Failed sending user welcome email.");
            return false;
        }
    }

    return true;
}

我假设查询应该包含某种 where 子句,这样您就不会每次都查询整个用户表。我删除了while循环,因为您在发送电子邮件后返回。

于 2013-02-21T21:29:20.723 回答