将单个行数据从mysql发送给各个用户

时间:2013-06-13 14:29:09

标签: php mysql email

我有10行,其中包含用户信息及其ID和电子邮件。单个用户有多行。现在我想将邮件发送给所有用户,这些用户在一个邮件中包含他们的行数据

假设

id  email           item    balance
1   1@example.com   car 200
2   2@example.com   bike    400
3   3@example.com   iron    100
1   1@example.com   cd  500
3   3@example.com   dvd 200
1   1@example.com   books   300

现在我使用下面的脚本向所有行发送多封电子邮件给不同的用户。 这是脚本为单个用户发送所有行的单独邮件。

我希望id为1@example.com的user1只接收包含与他相关的所有行数据的单个邮件。

<?php
$con=mysqli_connect("localhost","root","xx","card");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result2 = mysqli_query($con,"SELECT * FROM mailtest");
while($row = mysqli_fetch_array($result2))
{
$to = $row['email'];
$subject = "subject";
$emailBody="";
$emailBody .= "ID: ".$row['id']."; Item: ".$row['item']."; Balance:      ".$row['balance']."; Email: ".$row['email']." \n";

$headers = 'From: Work Pass Notification System <no-reply@someemailaddress>' . "\r\n" .
    'Reply-To: someemailaddress' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }

}

mysqli_close($con);
?>

3 个答案:

答案 0 :(得分:2)

您可以对要合并的行使用GROUP_CONCAT()。这会为group_concat

的每一列返回一个csv
SELECT id, email, GROUP_CONCAT(item) as item,  GROUP_CONCAT(balance) as balance FROM mailtest GROUP BY id

返回

id  email           item            balance
1   1@example.com   car,cd,books    200,500,300
2   2@example.com   bike            400
3   3@example.com   iron,dvd        100,200

然后你可以使用explode()来获取php中的每个值,或者使用任何其他函数来处理返回的行数据。

您甚至可以将其与SUM()混合以合并balance

SELECT id, email, GROUP_CONCAT(item) as item,  SUM(balance) as balance FROM mailtest GROUP BY id

返回

id  email           item            balance
1   1@example.com   car,cd,books    1000
2   2@example.com   bike            400
3   3@example.com   iron,dvd        300

修改
这是一种方法。

<?php
$con=mysqli_connect("localhost","root","xx","card");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result2 = mysqli_query($con,"SELECT id, email, GROUP_CONCAT(item) as item,  GROUP_CONCAT(balance) as balance FROM mailtest GROUP BY id");
while($row = mysqli_fetch_array($result2))
{
$to = $row['email'];
$subject = "subject";
$emailBody="";
// explode the items/balances on the comma's
$items = explode(',',$row['item']);
$balance = explode(',',$row['balance']);
// Create a line for each item/balance
foreach($items as $key => $item){
    $emailBody .= "ID: ".$row['id']."; Item: ".$items[$key]."; Balance:      ".$balance[$key]."; Email: ".$row['email']." \n";}
// add a Total Balance line
$emailBody .= "Total Balance: ".array_sum($balance)."\n";
$headers = 'From: Work Pass Notification System <no-reply@someemailaddress>' . "\r\n" .
    'Reply-To: someemailaddress' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }

}

mysqli_close($con);
?>

答案 1 :(得分:0)

为什么不将所有用户的信息都放入数组然后循环?

<?php

$con=mysqli_connect("localhost","root","xx","card");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result2 = mysqli_query($con,"SELECT * FROM mailtest");

while($row = mysqli_fetch_array($result2)){

    $send_details[$row['email']] .= "ID: ".$row['Stu_id']."; Pin No: ".$row['Pin_number']."; name:      ".$row['name']."; Email: ".$row['email']." \n";

}

foreach($send_details as $email => $body){
   // Send the mail
}

答案 2 :(得分:0)

你可以做两个嵌套查询(伪代码)

"SELECT DISTINCT email FROM items;"
foreach $email do:
    "SELECT item FROM items WHERE email = $email;"
    clear($all_items)
    foreach $item do:
        $all_items[] = $item
    mail(... $all_items ...)

作为替代方法,使用GROUP_CONCAT将所有项目放在一个字段中:

SELECT email, GROUP_CONCAT(item) all_items FROM items GROUP BY email

然后将all_items字段放入您的电子邮箱中。