如何在用户执行特定操作时向用户朋友发送电子邮件?

时间:2011-10-10 17:28:26

标签: php events email

我正在使用下面的这个脚本,它有效。唯一的问题是它会向它收到的第一个电子邮件ID发送一封电子邮件。其余的都被抄袭了。那么我如何向所有电子邮件发送电子邮件。当我测试只是拉动电子邮件的脚本时,它可以工作,它会显示所有电子邮件。

代码:

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

4 个答案:

答案 0 :(得分:2)

每次循环都会覆盖$email变量。相反,您希望创建一个数组,并在每次循环时将电子邮件添加到数组中。完成后,在发送邮件之前使用逗号加入阵列中的电子邮件地址。

所以在循环之前(可能在$rows = ...语句之后),初始化一个新数组,如下所示:

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

然后每次循环,将电子邮件添加到阵列:

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

最后,在电子邮件发送中加入逗号:

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

或者,您可以向每个用户发送一封单独​​的电子邮件,如下所示:

foreach ($emails as $email) {
    mail($email, "Subject: $subject", $message, $headers);
}

答案 1 :(得分:0)

试试这个:

<?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']
}

的变化:

  • 清理了PHP代码(删除了未使用的指令,删除了没有做任何事情的第二个查询,删除了"中的字符串变量等。)
  • 更正了电子邮件中的HTML链接(href属性上没有引号)
  • 循环显示结果,以便为每个$row
  • 发送电子邮件

答案 2 :(得分:0)

这里我附上了发送多封邮件的代码,并将其与现有代码集成。

您只需要复制粘贴此页面中的代码并将其上传到某个服务器上以测试其可用性。我已经在我的服务器上进行过测试,它会发送多封邮件。

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

如果您仍然发现任何问题,请将其还原。

答案 3 :(得分:0)

您需要通过移动循环中的最后一个代码块来进行一些小修改。 试试这个:

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