php - 将单个行数据从 mysql 通过电子邮件发送给单个用户

标签 php mysql email

嗨,我有 10 行,其中包含用户信息及其 ID 和电子邮件。单个用户拥有多行。现在我想向所有用户发送邮件,其中在一封邮件中包含其行数据

假设

id  email           item    balance
1   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="142554716c75796478713a777b79" rel="noreferrer noopener nofollow">[email protected]</a>   car 200
2   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d4f3d18051c100d1118531e1210" rel="noreferrer noopener nofollow">[email protected]</a>   bike    400
3   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d3e4d68756c607d6168236e6260" rel="noreferrer noopener nofollow">[email protected]</a>   iron    100
1   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="764736130e171b061a135815191b" rel="noreferrer noopener nofollow">[email protected]</a>   cd  500
3   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="724132170a131f021e175c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>   dvd 200
1   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fbcabb9e839a968b979ed5989496" rel="noreferrer noopener nofollow">[email protected]</a>   books   300

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

我想要 id 为 [email protected] 的 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);
?>

最佳答案

您可以在要合并的行上使用 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   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2c3b2978a939f829e97dc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>   car,cd,books    200,500,300
2   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="645624011c05091408014a070b09" rel="noreferrer noopener nofollow">[email protected]</a>   bike            400
3   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0e390b5a8b1bda0bcb5feb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a>   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   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c4d3c19041d110c1019521f1311" rel="noreferrer noopener nofollow">[email protected]</a>   car,cd,books    1000
2   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4f684a1bca5a9b4a8a1eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>   bike            400
3   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ae9deecbd6cfc3dec2cb80cdc1c3" rel="noreferrer noopener nofollow">[email protected]</a>   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);
?>

关于php - 将单个行数据从 mysql 通过电子邮件发送给单个用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17089686/

相关文章:

php - 将 index.php 重定向到 root 不适用于 SSL

php - 使用多个条件过滤 MySQL 查询

email - Paypal 沙箱帐户电子邮件确认

php - 联系表单 PHP 重定向不起作用

email - Google Suite 应用脚本电子邮件发送限制

php - 使用 PHP 和 JQuery 执行作为字段值存储在数据库表中的查询/存储过程?

php - jqGrid 设置带有参数化查询的 Select 函数

php - Session 在用户认证/登录中的作用?

python - 加快 Django 表单上传大型(500k obs)CSV 文件到 MySQL 数据库

c# - 将 winrt sqlite 与在线 mysql 数据库同步