Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send emails to a users friends when the user takes a specific action?

Tags:

php

email

events

I am using this script below, which works. The only thing is it sends an email to the first email id it gets. The rest are being ingnored. So how do i send email to all the emails. When i tested just the script that is pulling emails, it works, it shows all the emails.

Code:

    $sql = "SELECT STRAIGHT_JOIN DISTINCT 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);

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

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

   $email = $row['email'];


  print("");
  }

  }

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$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>";
mail("$email", "Subject: $subject",
$message, "$headers" );
echo "";
like image 217
AAA Avatar asked Oct 10 '11 17:10

AAA


People also ask

Why do we send emails A to reach on time B to share documents and files C to talk to each other D to meet each other?

The correct response to the given question is option b: send documents and files. Emails are a type of electronic communication that allows communicating essential information or messages to single or several recipients at the same time.


1 Answers

You are overwriting your $email variable each time through the loop. Instead, you want to create an array, and add the email to the array each time through the loop. When finished, join the email addresses in the array with commas before sending the mail.

So before your loop (maybe after the $rows = ... statement), initialize a new array, like this:

$rows = mysql_num_rows($result);
$emails = array(); /* Add this line */

Then each time through the loop, add the email to the array:

while($row = mysql_fetch_array($query)) {
    array_push($emails, $row['email']);
    /* ... */
}

Finally, join them with commas in your email send:

mail(implode(',', $emails), "Subject: $subject", $message, $headers);

Alternatively, you can send one separate email to each user like this:

foreach ($emails as $email) {
    mail($email, "Subject: $subject", $message, $headers);
}
like image 83
Ben Lee Avatar answered Sep 28 '22 01:09

Ben Lee