1

In my database, there are only columns as email (the columns are the emails) and inside each column, there is data about that email (name of the person, etc) but this doesn't matter. I try to get all the columns as array then using implode() to convert the array to string, so I can use the string to send an email to all columns.

This is what I tried:

$address = array();
$connection = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $connection);
$result = mysql_query("SHOW COLUMNS FROM `Emails`");
while($row = mysql_fetch_array($result))
{
$address = implode(',', $row);
}

If I try this: echo $row['Field']."<br>"; it shows all the columns (emails) stored in the array but when I try $address = implode(',', $result); it's not working. This is where the string $address is used: mail($address,$subject,$message,$headers);

Sumutiu Marius
  • 421
  • 4
  • 18

2 Answers2

1

Build the array of emails in the while loop, then implode them into a string of comma separated values or whatever you need.

<?php
$result = mysql_query("SHOW COLUMNS FROM `Actions`");
while($row=mysql_fetch_assoc($result)){
    $emails[]=$row["Field"];
}
//$csv_emails=implode(", ",$emails);
//echo $csv_emails;
$headers.="Bcc: ".implode(", ",$emails)."\r\n";  // blind carbon copy all emails
mail('',$subject,$message,$headers);  // no $to, all recipients in $headers
?>

--START EDIT

I have adjusted my snippet to show how you can use mail() just once -- not making n number of mail() function calls. I have not tested this snippet, so I recommend trialing it on two email addresses that you have access to. If any readers find an error with this snippet, please let me know and I'll update it.

Regardless of if you chose to batch the mail out or not, do not use two loops -- either solution can be done in just one loop. (I mean, if you want to call mail() multiple times, and aren't using $emails[] for anything else later in the code, you can just put your mail() inside the while loop and don't bother creating the $email[] array.)

An Aside: As for maximum number of recipients per mail() function, there are MANY questions on SO on this topic. Here is just one: Is there a limit when using php mail function?

END EDIT--

In case you need to filter the email list in the future:

Omit one email address with:

SHOW COLUMNS FROM `Actions` WHERE `Field`!='bad@email.com'

Or omit multiple email address with:

SHOW COLUMNS FROM `Actions` WHERE `Field` NOT IN('bad1@email.com','bad2@email.com')
Community
  • 1
  • 1
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Thanks so much. It worked. By the way, here: `$csv_emails=implode(",",$emails);` Should be `$csv_emails=implode(", ",$emails);` (with space after the comma to separate the emails) – Sumutiu Marius Feb 24 '17 at 05:15
  • 1
    $csv_emails can be crafted however you wish. Happy to help get you moving forward. Thanks for accepting. – mickmackusa Feb 24 '17 at 05:22
  • Followup: Instead of implode() I used foreach() `foreach ($addresses as $to) {mail($to,$subject,$message,$headers);}` This bypasses the SMTP anti spam filters and it shouldn't be a problem since I can edit the PHP timeout to whatever value I want. – Sumutiu Marius Feb 24 '17 at 17:17
  • 1
    @XmasterOfficial I don't use mail() for any of my projects because PHPMailer is far more trustworthy. It will take a moment to setup, but will payoff in the long run. I have adjusted my snippet to advise about single versus multiple mail() calls. Give a shout if you have any trouble. – mickmackusa Feb 24 '17 at 22:25
0
$address = array();
$connection = mysql_connect($YOURHOST, $USER, $PASS);
mysql_select_db($DBNAME, $connection);
$result = mysql_query("SHOW COLUMNS FROM `Emails`");
while($row = mysql_fetch_array($result))
{
    $address = implode(',', $row);
    var_dump($address);
}

1) it is working, in your question you have userd variable $result in implode function this is working as my end

2) In you edit version of question you have asked that you want to mail using this $result data, You can not do it, because with query you can get only columns name not its value.

that is reason it is not working

Instead you query you can use below query SELECT emailids FROM Emails WHERE 1

Use this query and you can sent mail using this result set of this query

Shishil Patel
  • 3,449
  • 2
  • 12
  • 16
  • I don't want to get columns value. I want to get columns name since columns name ARE emails. Columns ARE the emails. The value inside the columns doesn't matter. – Sumutiu Marius Feb 24 '17 at 05:00
  • Then you have specify email address in `mail()` to sent on atleast on mail address. and create msg body using data set then you can do – Shishil Patel Feb 24 '17 at 05:02