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