开发者

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

开发者 https://www.devze.com 2023-04-11 20:12 出处:网络
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

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";
$s开发者_如何学Goubject = "$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 "";


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


Try this:

<?php

$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>";

$sql = "SELECT DISTINCT email from friend_email_ids WHERE my_id='$id'";
$result = mysql_query($sql) or die ("Error: ".mysql_error());

while($row = mysql_fetch_array($result))
{
   mail($row['email'], $subject, $message, $headers);
   // for debugging purposes, uncomment the following line
   // echo $row['email']
}

Changes:

  • cleaned up PHP code (removed unused instructions, remove second query which doesn't do anything, removed string vars in " etc.)
  • corrected HTML link in the email message (it had no quotes on the href attribute)
  • loop through results to send email for each $row


Here i am attaching code for sending multiple mails and integrating it with your existing code.

You just need to copy paste this code in your page and upload it on some server to test for its workability. I have already tested on my server and it gives multiple mails.

$email = array();

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

while($row = mysql_fetch_assoc($query))
{
    $email[] = $row['email'];
}


/*
echo "<pre>";
print_r($email);
exit;
*/
$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 this mail<br><br>";
$message .= "<a href='www.aadinathtech.com/index.php'>Click here.</a><br><br>";
$message .= "</body></html>";

foreach($email as $key=>$val) {
    mail($val, "Subject: $subject", $message, "$headers" );
    echo "<br />Mail has been sent to $val<br /><br />";
}

Please revert me back if you still find any problem.


You need to do a small modification by moving the last block of code inside your loop. Try this:

$rows = mysql_num_rows($result);

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

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

   $email = $row['email'];
   $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" );

  print("");
  }

  }


echo "";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号