使用PHP从MYSQL数据库向多个地址发送电子邮件时出错

时间:2012-10-28 16:22:53

标签: php arrays email variables recordset

我正在尝试向多个电子邮件地址发送电子邮件,这些电子邮件地址包含在数据库中并分类到记录集中...记录集有多个列,但我只需要一个:" Email"。我知道如果我将它们放在一个数组中,我可以将它们内部删除并用逗号分隔它们,但我不确定如何使用记录集列来完成它。谁知道怎么样?顺便说一下,我知道我已经注释掉了邮件功能......回声返回空...

继承我尝试过的代码:

    $colname_rsAllLeads = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsAllLeads = $_SESSION['MM_Username'];
}
mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);
$query_rsAllLeads = sprintf("SELECT Email FROM Leads WHERE `User` = %s ORDER BY FullName ASC", GetSQLValueString($colname_rsAllLeads, "text"));
$rsAllLeads = mysql_query($query_rsAllLeads, $myBackOfficeConn) or die(mysql_error());
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
$totalRows_rsAllLeads = mysql_num_rows($rsAllLeads);



$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
} 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {

    $startcode = $_POST['messagefield'];
    $replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
    echo $replaced;
    $collectedleads = implode(',', $row_rsAllLeads['Email']);
    echo $collectedleads;

    /*
 $to = $collectedleads;
 $subject = $_POST['subjectfield'];
 $body = $replaced;
 $headers = "MIME-Version: 1.0\r\n"; 
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
 $headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
 if (mail($to, $subject, $body, $headers)) {
  } else {
   echo("<p>Message delivery failed...</p>");
  }
  */

  $insertSQL = sprintf("INSERT INTO PendingEmails (`to`, subject, message) VALUES (%s, %s, %s)",
                       GetSQLValueString($row_rsAllLeads['Email'], "text"),
                       GetSQLValueString($_POST['subjectfield'], "text"),
                       GetSQLValueString($_POST['messagefield'], "text"));

  mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);
  $Result1 = mysql_query($insertSQL, $myBackOfficeConn) or die(mysql_error());

  $insertGoTo = "Email Sent.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

当我使用var_dump($row_rsAllLeads['Email'])时,它输出字符串(16)&#34; acorso@gmail.com" 但我知道我的SQL查询中没有错误,因为当我把它们放在一个选择框中,它们都出现了......

1 个答案:

答案 0 :(得分:0)

首先,您似乎只从结果中获得第一行。所以你必须得到所有这些:

$collectedleads = array();

while($row = mysql_fetch_assoc($rsAllLeads)) {
   $collectedleads[] = $row['Email'];
}

之后,您必须使用comma分隔每个电子邮件地址。 尝试使用implode方法:

$collectedleads = implode(',', $collectedleads);
echo $collectedleads;