0

I have this code:

<?php 
$result_array1 = array();
$sql1 = "SELECT * FROM  `test_thing`";
$result1 = mysql_query("$sql1") or die($sql1.mysql_error());
while($row1 = mysql_fetch_array($result1))
    {
       $result_array1[] = $row1['email'];
    }
$sentmail = mail($result_array1,$subject,$message,$header);
?>

My problem is that if I place the mail function inside the loop, the user receives many emails instead of one, and if I place it out of the loop, no email is sent. My aim is to send only 1 email to each user, but that email should containt in it many mixed emails. I have read in other forums that it can be done using arrays. Maybe there is a mistake with the codes. Any suggestions?

Thanks

2
  • Shouldn't it be mysql_fetch_assoc instead of mysql_fetch_array? Commented May 5, 2012 at 18:46
  • 1
    @Shedal It could be, but by default mysql_fetch_array() gets both associative and numeric keys so this works fine. Commented May 5, 2012 at 18:46

5 Answers 5

1

try this

<?php 
$result_array1 = array();
$sql1 = "SELECT * FROM  `test_thing`";
$result1 = mysql_query("$sql1") or die($sql1.mysql_error());
while($row1 = mysql_fetch_array($result1))
    {
       $result_array1[] = $row1['email'];
    }
$email = implode(",",$result_array1); // $email = "[email protected],[email protected],[email protected]"
$sentmail = mail($email,$subject,$message,$header);
?>

The first parameter of the mail can be a single email id or email ids separated by commas. Which means you can send the email to multiple users just using one function call.

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

7 Comments

in question it is writter "My aim is to send only 1 email to each user"
@MoyedAnsari: It will send only one email to each user
what if the query returns the two exactly same email addresses ?
@MoyedAnsari: what are you trying to prove ? try to understand the need of the questioner. Dont make silly aruguments
what im saying that $result_array1 may has two identical emails, since there is no distinct or group by in query.
|
0

Just use the following MySQL query:

SELECT DISTINCT email FROM  `test_thing`

It will return only unique e-mail addresses from the table. That way you can use the mail() in the loop and not worry about duplicated e-mail addresses.

Comments

0

According to php.net: http://php.net/manual/en/function.mail.php

mail accepts a string for the to address. You could always implode(',', $result_array1)

Comments

0

IMHO $to parameter in mail function just one email support .if you want send to many email, change your $headers and set Bcc; sample code:

$headers.="Bcc: "; 
while($count < $count_recip){ 
$headers.=$recip[$count].", "; 
$count ++; 
} 

Comments

0

This worked for me:

$result_array1 = array();
$sql1 = "SELECT DISTINCT * FROM  `test_thing` GROUP BY `user_id`";
$result1 = mysql_query("$sql1") or die($sql1.mysql_error());

while($row1 = mysql_fetch_array($result1))
{
    $result_array1[] = $row1['EmailAddress'];
}

$email = implode(",",$result_array1);

I modified some of the answers together.

There will be no duplicate emails either, atleast there wasn't for me.

1 Comment

If you're only using EmailAddress from the result set, then you can just SELECT DISTINCT EmailAddress FROM ...., which will guarantee that you don't get duplicate entries for EmailAddress.

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.