仅针对选定的复选框值发送密件抄送电子邮件

时间:2014-05-21 09:51:18

标签: php email

我正在尝试仅向选定的用户发送相同的电子邮件。我正在从表中打印值,并希望选择特定用户来发送电子邮件。

<form name="unos" action="mail-proizvodi.php" method="post">

<?
    echo "<table border='5'>
    <tr>
    <th> </th>
    <th>ID</th>
    <th>NAZIV</th>
    <th>ADRESA</th>
    <th>DRZAVA</th>
    <th>GRAD</th>
    <th>EMAIL</th>
    </tr>";
    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo '<td><input type="checkbox" name="email[]" value="' . $row['ID'] . '"></td>';
    echo "<td>" . $row['ID'] . "</td>"; 
    echo "<td>" . $row['NAZIV'] . "</td>";
    echo "<td>" . $row['ADRESA'] . "</td>";
    echo "<td>" . $row['DRZAVA'] . "</td>";
    echo "<td>" . $row['GRAD'] . "</td>";
    echo "<td>" . $row['EMAIL'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
?>
<input type="submit" name="submit" value="submit">
 </form> 

我的mail-proizvodi.php代码

$mail=$_POST['email'];

echo "Dzenad catic";

$query=  "SELECT `EMAIL` FROM `clanovi` WHERE ID='$mail[0]'";
if(sizeof($mail)>1)
{
        for($i=1; $i<sizeof($mail); $i++)
        {
            $query.=" OR ID = '$mail[$i]' ";
        }
}
$result=mysqli_query($con,$query);

while(FALSE!==($row=mysqli_fetch_row($result))) {
  $bccfields[] = $row['EMAIL'];
}
echo sprintf("<a href=mailto:test@test.ba?bcc=%s />\n", 
             urlencode(implode(',',$bccfields))); 

echo "Send" ;

我收到的帖子是一个数组。当我做var_dump($ mail)时,我得到了 阵列

0 => string '20' (length=2)
  1 => string '30' (length=2) 

感谢任何帮助或建议。提前致谢。

1 个答案:

答案 0 :(得分:0)

我正在为我遇到的问题发布解决方案,以防其他人遇到类似的错误。

$mail=$_POST['email'];
$query=  "SELECT `EMAIL` FROM `clanovi` WHERE ID ='$mail[0]'"; 

if(sizeof($mail)>1)
{
        for($i=1; $i<sizeof($mail); $i++)
        {
            $query.=" OR ID = '$mail[$i]' ";
        }
}

$result=mysql_query($query);

if (!$result) {
    echo "Could not successfully run query ($query) from DB: " . mysql_error();
    exit;
}
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while(FALSE!==($row=mysql_fetch_assoc($result))) {
  $bccfields[] = $row['EMAIL'];
}
echo sprintf("<a href=mailto:prodaja@alternativa.ba?bcc=%s />\n", 
             urlencode(implode(',',$bccfields))); 

echo "Send" ;

mysql_free_result($result);