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

10
  • $row is your row, not $result. Also you overwrite on every iteration. Commented Feb 24, 2017 at 4:29
  • because result isn't normal array, you are fetching result element as array in row. result is resource. Commented Feb 24, 2017 at 4:29
  • 2
    Please trash mysql_ functions when you next get the chance; there are better options available. Commented Feb 24, 2017 at 4:32
  • 1
    @XmasterOfficial Your process seems strange, please offer your table structure and a couple of rows so we can give clear guidance. Commented Feb 24, 2017 at 4:33
  • @mickmackusa I know... unfortunately, to do that, I need to edit the entire website. Commented Feb 24, 2017 at 4:33

2 Answers 2

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`!='[email protected]'

Or omit multiple email address with:

SHOW COLUMNS FROM `Actions` WHERE `Field` NOT IN('[email protected]','[email protected]')
Sign up to request clarification or add additional context in comments.

4 Comments

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)
$csv_emails can be crafted however you wish. Happy to help get you moving forward. Thanks for accepting.
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.
@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.
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

2 Comments

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

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.