如何通过电子邮件发送数组


How to send array via email?

我在通过电子邮件从数据库发送数组时遇到问题。我相信也许人们也有同样的问题。我不知道如何解释更多,但这里有一些脚本。

$sql="SELECT * FROM tb_xxx WHERE id_prd = '$ref_id_prd'";
        $result=mysql_db_query($dbname,$sql);
        while ($rs=mysql_fetch_array($result)) {
            $ref_id_prd=$rs[ref_id_prd];
                        $prd=$rs[prd];
                        $price=$rs[price];
text="$prd <br>$price";
}

$recipient = $iemail;
$subject = "my subject";
$headers = "From: xxx@xxx.com 'n";
$headers .= "Reply-To: xxx@xxx.com 'n";
$headers .= "MIME-Version: 1.0 'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 'n";

   $msg = $text;
   mail($recipient, $subject, $message, $headers);

您需要将所有数组值累积到消息正文的单个字符串中。

$text = "";
while ($rs = mysql_fetch_array($result)) {
        $ref_id_prd=$rs[ref_id_prd];
        $prd=$rs[prd];
        $price=$rs[price];
        // Use .= to append to the current value
        $text .= "$prd <br>$price'n";
}

要发送,请使用$text作为消息主体:

mail($recipient, $subject, $text, $headers);

注意,您需要在下面的行中进行额外的格式化,以使其在HTML电子邮件中看起来像您想要的那样:

$text .= "$prd <br>$price'n";

例如,您可以列出产品列表:

$text = "<ul>'n";
// inside the while loop...
$text .= "<li>$prd: $price</li>'n";
// After the loop, close the list
$text .= "</ul>"

更新:如何将其构建为HTML表:

$text = "";
// Open the table:
$text .= "<table>'n";
// Table header...
$text .= "<thead><tr><th>Product</th><th>Price</th></tr></thead>'n";
// Open the table body
$text .= "<tbody>'n";
while ($rs = mysql_fetch_array($result)) {
        $ref_id_prd=$rs[ref_id_prd];
        $prd=$rs[prd];
        $price=$rs[price];
        // Build table rows...
        $text .= "<tr><td>$prd</td><td>$price</td></tr>";
}
// Close the table
$text .= "</table>";

$msg 替换$message变量

mail($recipient, $subject, $msg, $headers);