我目前正在使用下面的代码从php中的mysql表中邮寄数据,它工作得很好,但问题是它将每行数据作为单个邮件发送,我想在一封邮件中发送所有数据行,如何合并所有数据行并将其作为单个邮件发送?请帮帮我!!
<?php
include('header.php');
session_start();
?>
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
$sql="SELECT * FROM products";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$to = "xyz@gmail.com";
$subject = "d2dn-viewcart";
$id_s = $row["id"];
$name_s = $row["description"];
$price_s = $row["price"] ;
$message = $id_s . " " . $name_s . " " . $price_s." ";
$header = "From:d2dn";
$retval = mail ($to,$subject,$message,$header);
}
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
<?php
include('footer.php'); ?>
答案 0 :(得分:1)
您只需连接内容并发送一次电子邮件:
<?php
include('header.php');
session_start();
?>
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
$sql="SELECT * FROM products";
$result = mysql_query($sql);
$to = "xyz@gmail.com";
$subject = "d2dn-viewcart";
$header = "From:d2dn";
$message = '';
while($row = mysql_fetch_array($result)){
$id_s = $row["id"];
$name_s = $row["description"];
$price_s = $row["price"] ;
$message .= $id_s . " " . $name_s . " " . $price_s." ";
}
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
<?php
include('footer.php'); ?>
答案 1 :(得分:0)
定义第一个外部循环$message = '';
然后你需要追加像
这样的字符串$message .= $id_s . " " . $name_s . " " . $price_s." ";
然后保持mail()
外部循环
$retval = mail ($to,$subject,$message,$header);
答案 2 :(得分:0)
<?php
include('header.php');
session_start();
?>
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
$sql="SELECT * FROM products";
$result = mysql_query($sql);
$to = "xyz@gmail.com";
$subject = "d2dn-viewcart";
while($row = mysql_fetch_array($result)){
$id_s = $row["id"];
$name_s = $row["description"];
$price_s = $row["price"] ;
$message .= $id_s . " " . $name_s . " " . $price_s." \r\n";
}
$header = "From:d2dn";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
<?php
include('footer.php'); ?>
这应该有效