向每个符合条件的用户发送电子邮件

时间:2013-08-30 18:45:58

标签: php

由于评论中有以下建议,我已更新了我的代码。现在的问题是我得到一个错误。这是新代码:

<?php
require_once "config.php";
$idspam = "1";
$sql = "SELECT * FROM company WHERE ID = '$idspam'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
    $companyname = $row['CompanyName'];
}
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
foreach($rows as $row)
{
    $Agent = $row['username'];
    $fullname = $row['fullname'];
    $phone = $row['phone'];
    $Agentmail = $row['mail'];
    $checkstat = "New customer";
    $result = mysql_query("SELECT * FROM data WHERE Status = '$checkstat' AND Agent = '$Agent'");
    $rows = mysql_fetch_array($result);
    foreach($rows as $row)
        {
        $LastName = $row['LastName'];
        if (empty($row['ProductPriceUSD'])) {
            $ProductPriceEUR = $row['ProductPriceEUR'];
            $symbol = "&#8364;";
            $Productprice = ($ProductPriceEUR . $symbol);
        }
        if (empty($row['ProductPriceEUR'])) {
            $ProductPriceUSD = $row['ProductPriceUSD'];
            $symbol = "$";
            $Productprice = ($symbol . $ProductPriceUSD);
        }
        $CustomerPriceRAW = $row['CustomerPrice'];
        $eur = "&#8364;";
        $email = $row['Email']; 
        $a = mt_rand(100000,999999);
        $MD5Pass = md5($LastName . $a);
        $type = "Initial";
        $sql = "SELECT * FROM emails WHERE AddedBy = '$Agent' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $content = $row['Content'];
            $subject = $row['Subject'];
        }
        $Newstatus = "Contacted";
        $Newline = "<br>";
        $replaceWord = array(

        "[name]" => $row['FirstName'],
        "[lastname]" => $row['LastName'],
        "[product]" => $row['ProductName'],
        "[username]" => $row['CustomerUser'],
        "[agent]" => $row['Agent'],
        "[productssold]" => $row['ProductsSold'],
        "[customerprice]" => ($CustomerPriceRAW . $eur),
        "[password]" => ($LastName . $a),
        "[productprice]" => $Productprice,
        "[newline]" => $Newline,
        );
        ob_start();
        include("./email/mailtemplate.php");
        $sendmsg = ob_get_clean();
        $message = $sendmsg;
        $body = strtr($content, $replaceWord);
        $headers = "From:".$companyname." ".$Agentmail."\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $contactdate = date("Y-m-d");
        $sevendays = date('Y-m-d', strtotime("+7 days"));
        $todo = "Followup";
        mysql_query("UPDATE data SET CustomerPass = '$MD5Pass', Status = '$Newstatus', Contacted = '$contactdate', SevenDays = '$sevendays', ToDo = '$todo'");
        mail($email, $subject, $body, $headers);
    }
}

?>

这是我得到的错误

Warning: Invalid argument supplied for foreach() in /home/user/public_html/cronmailinitial.php on line 22

为什么呢?我想挑选所有员工(第一个foreach)并联系他们的每个客户。所以一个例子是: 代理人“约翰” 选择由“John”添加的所有客户 对于他们每个人选择一个不同的电子邮件模板 发送

这就是它无论如何都要做的事情。提前致谢

1 个答案:

答案 0 :(得分:1)

两件事:

  1. 此API已弃用
  2. 更改所有

    while($ row = mysql_fetch_array($ result))

  3. 更像是

    $rows = mysql_fetch_array( $result );
    foreach( $rows as $row )
    {
      .. do something with $row....
    }