簡體   English   中英

PHP使用Mail發送數組()

[英]PHP Sending Arrays with Mail()

我試圖通過郵件發送數組的內容,我嘗試了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);
 ?>

有誰知道出了什么問題?

如果要使用print_r郵寄數組,請使用true填充第二個參數:

print_r($array, true); 

有關更多信息,請參閱以下鏈接: http//php.net/manual/en/function.print-r.php

這是因為內implode函數只會破壞您的頂級數組,即$subscribers 它不會下降到下一級數組數據。

你可以試試這個:

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

$content = implode("\n", $dataset);
mail($recipient, $subject, $content, $headers);

$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";

它不能像你想的那樣工作的原因是因為implode()不能像你想象的那樣使用關聯數組。 我建議:

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

然后,當您使用implode解析此數組時,使用換行符作為分隔符,您將看到

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

我認為這就是你想要的。 干杯!

您在鍵值對中發送該數組非常好,而不是僅發送值..

使用以下腳本轉換它並通過您的電子郵件發送。

$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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM