1

我正在尝试通过邮件发送数组的内容,我尝试了 print_r 方法但是不太喜欢它在电子邮件中的格式,所以我尝试了内爆,但是目前它实际上并没有发送内容,而是只是“阵列阵列”

这是我的代码:

<?php 
$con = mysqli_connect("localhost", "user","pw", "db");
if (!$con)
{
 die('Could not connect: ' . mysqli_error($con));
}

$recipient_email = "email@email.com";

$result= mysqli_query($con, "SELECT * FROM subscribers WHERE datetime_registered >=       DATE_SUB(NOW(), INTERVAL 9 DAY)")or die(mysqli_error($con));
$subscribers = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $subscribers[] = array(
    "uid" => $row['id'],
    "name" => $row['name'],
    "email" => $row['email'],
    "ip" => $row['ip'],
    "date_registered" => $row['datetime_registered']
    );


 }


 $headers  = 'MIME-Version: 1.0' . "\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
 $headers .= 'From: '.$recipient_email.''."\n";

 $subject = "Subject";
 $recipient = $recipient_email;
 $content = implode("\n", $subscribers);
 mail($recipient, $subject, $content, $headers);
 ?>

有谁知道出了什么问题?

4

5 回答 5

3

当你想用 print_r 邮寄一个数组时,用 true 填充第二个参数:

print_r($array, true); 

有关详细信息,请参阅以下链接:http: //php.net/manual/en/function.print-r.php

于 2013-06-14T08:57:03.040 回答
1

这是因为该implode函数只会破坏您的顶级数组,即$subscribers. 它不会下降到下一级数组数据。

你可以试试这个:

foreach($subscribers as $subscriber_data){
    $dataset[] = implode(', ', $subscriber_data);
}

$content = implode("\n", $dataset);
mail($recipient, $subject, $content, $headers);
于 2013-06-01T20:25:56.153 回答
0

$headers变量之前,请使用以下代码:

$newSubscribers = array();
foreach($subscribers as $data)
{
    $newSubscribers[] = implode(', ', $data);
}

$newSubscribers = implode("<br>\n", $newSubscribers);
$subscribers = $newSubscribers;

所以你的最终代码应该是这样的:

...
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

$subscribers[] = array(
"uid" => $row['id'],
"name" => $row['name'],
"email" => $row['email'],
"ip" => $row['ip'],
"date_registered" => $row['datetime_registered']
);


}

$newSubscribers = array();
foreach($subscribers as $data)
{
    $newSubscribers[] = implode(', ', $data);
}

$newSubscribers = implode("\n<br>", $newSubscribers);
$subscribers = $newSubscribers;


$headers  = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From: '.$recipient_email.''."\n";
于 2013-06-01T20:24:05.520 回答
0

它没有按您希望的那样工作的原因是因为 implode() 不能以您认为的方式与关联数组一起使用。我建议:

$subscribers[] = array(
"uid: {$row['id']}",
"name: {$row['name']}",
...
);

然后当你用 implode 解析这个数组时,用换行符作为分隔符,你会看到

"uid: someid
 name: somename
 ....
"

我认为这就是你想要的。干杯!

于 2013-06-01T20:30:18.800 回答
0

很高兴您以键值对的形式发送该数组,而不是仅发送值..

使用以下脚本进行转换并通过您的电子邮件发送。

$data = '';
foreach ($subscribers as $key=>$value){
    $data .= $key.'-------'.$value;
    $data.= "\n";
}

通过php邮件功能发送..

$headers  = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From: '.$recipient_email.''."\n";

$subject = "Subject";
$recipient = $recipient_email;
$content = $data;
mail($recipient, $subject, $content, $headers);
于 2015-01-09T05:43:26.960 回答