1

I am trying to send the contents of an array through mail, I have tried the print_r method however do not really like the way it is formatted in the email, so I tried the implode, however currently it is not actually sending the content, rather just "Array Array Array"

This is my code:

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

$recipient_email = "[email protected]";

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

Does anyone know what is going wrong?

5 Answers 5

3

When you want to mail an array with print_r, fill the second parameter with true:

print_r($array, true); 

See the following link for more information: http://php.net/manual/en/function.print-r.php

Sign up to request clarification or add additional context in comments.

Comments

1

It's because the implode function is only imploding your top-level array which is $subscribers. It's not going down to the next level array data.

You could try this:

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

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

Comments

0

Before your $headers variables, use this code:

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

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

So your end code should look like this:

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

1 Comment

Phil this is great thanks, however it is not actually putting it on a new line, how would I get a new line between each subscriber for the email?
0

The reason it's not working as you'd like is because implode() does not work with associative arrays the way you think it would. I'd recommend:

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

Then when you parse this array with implode, with a newline as the delimiter, you will see

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

Which I assume is what you're going for. Cheers!

Comments

0

It is very nice you send that array in key value pairs, instead of sending only value..

convert that using following script and send it via your email.

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

send via php mail function..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.