Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

php - How to send email to multiple email addresses?

I have this form that is working perfectly except that it won't send emails to multiple people. It only sends it to one person. How do i fix this? I want to be able to send each individual a individual email with their email address in the to field regardless if that same email was sent to other users.

Update: I did a print $email; and if i select only one email it would print it and then if i more than 1 then it wouldn't print anything. So that means its not detecting more than 1 email.

     $sql = "SELECT email from
    friend_email_ids WHERE my_id='$id'";
    $result = mysql_query($sql);

    $query = mysql_query($sql) or die ("Error: ".mysql_error());

    if ($result == "")
    {
    echo "";
    }
     echo "";


   $rows = mysql_num_rows($result);
   $emails = array();

   if($rows == 0)
   {
   print("");

    }
   elseif($rows > 0)
   {
    while($row = mysql_fetch_array($query))
   {

   array_push($emails, $row['email']);


  print("");
  }

  }


$headers = "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$headers .= "From: $usermail
";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
 foreach ($emails as $email) {
mail($email, "Subject: $subject", $message, $headers);
   }


   echo "";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use MySQL's GROUP_CONCAT to join the emails together separated by commas. By populating a comma-separated value in the "to" field it will automatically send to multiple recipients:

SELECT GROUP_CONCAT(DISINCT `email` SEPARATOR ',') from
    friend_email_ids WHERE my_id='$id'"
    GROUP BY `email;

Group Concat MySQL:

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

PHP Doc explaining multiple email recipients in mail() function:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...