2

嗨,我有 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);
?>
4

3 回答 3

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);
?>
于 2013-06-13T14:33:46.423 回答
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字段放入您的电子邮件中。

于 2013-06-13T14:39:27.387 回答
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
}
于 2013-06-13T14:33:18.963 回答