PhP邮件程序 - 向多个收件人发送多个邮件

时间:2017-08-03 10:24:54

标签: php html email

我正在使用phpmailer。我想生成多条消息并将它们发送给多个收件人。但是,generateMessage函数()只是发送给收件人列表中的第一个收件人。 这是我的代码,任何帮助都是值得欣赏的。

require_once("class.phpmailer.php");

set_time_limit(0);

$wh = implode("', '", $abc);
echo "aaa" . $wh;
//echo $email;
$db = DB::getInstance();
$query1 = $db->query("SELECT * FROM tablea WHERE Location IN ('$wh')");
$result1 = $query1->results();
$query2 = $db->query("SELECT * FROM tableb WHERE Location IN ('$wh')");
$result2 = $query2->results();
$query3 = $db->query("SELECT * FROM tablec");
$result3 = $query3->results();
foreach ($result3 as $b) {
  $username = $b->email;
  $password = $b->password;
  $Times = $b->P2_Time;
}
$minutePerDay = (24 * 60) / $Times;
echo $minutePerDay;
foreach ($result1 as $r) {
  $TimeP2SendE = $r->Timea;
  break;
}
foreach ($result2 as $r) {
  $TimeP2SendG = $r->Timea;
  break;
}
$currentTime = date('Y-m-d H:i:s');
$TimeDifferentE = (strtotime($currentTime) - strtotime($TimeP2SendE)) / 60;
$TimeDifferentG = (strtotime($currentTime) - strtotime($TimeP2SendG)) / 60;
if ($TimeDifferentE >= $minutePerDay or $TimeDifferentG >= $minutePerDay) {
  $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->Host = "smtp.gmail.com";
  $mail->Port = 465;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'ssl';
  $mail->Username = $username;
  $mail->Password = $password;
  $mail->AltBody = " ";
  $mail->From = $username;
  $mail->FromName = 'Noti';
  $mail->clearAddresses();
  $mail->AddAddress($email);
  $mail->Subject = $title;
  $mail->IsHTML(true);
  $mail->Body = '<html><body>';
  $mail->Body .= "<h3>Information  </h3>";
  $mail->Body .= '<table class="table table-border">';
  $mail->Body .= "</table>";
  $mail->Body .= "</body></html>";

  if (!$mail->Send()) {
    echo "Error sending: " . $mail->ErrorInfo;
  } else {
    echo "Letter is sent";
  }
}

我已经运行此功能4次以发送给4个不同的收件人,但它只是发送给第一个收件人。

2 个答案:

答案 0 :(得分:0)

您可以构建一个联系对象数组并循环数组以添加每个联系人:

foreach($contacts as $contact){
    $mail->AddAddress($contact->email);
}

这将添加每个联系人的电子邮件并构建邮件列表。

对于每个收件人的自定义正文,我通常使用以下方法。

function sendEmail($email, $name, $body, $altBody = null) {
        $mail = new PHPMailer;
        $mail->IsSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->Username = $username;
        $mail->Password = $password;
        $mail->From = $username;
        $mail->FromName = 'Noti';
        $mail->clearAddresses();
        $mail->Subject = $title;
        $mail->IsHTML(true);
        $mail->addAddress($email, $name);     // Add a recipient    
        $mail->Body = $message;
        $mail->AltBody = $altBody;

        if (!$mail->send()) {
            error_log($mail->ErrorInfo);
            return false;
        } else {
            return true;
        }
    }

foreach($contacts as $contact){
    //create message here
    sendEmail($contact->email, $contact->name, $message);
}

答案 1 :(得分:0)

    require_once("class.phpmailer.php");

    set_time_limit(0);

    $wh = implode("', '", $abc);
    echo "aaa" . $wh;
    //echo $email;
    $db = DB::getInstance();
    $query1 = $db->query("SELECT * FROM tablea WHERE Location IN ('$wh')");
    $result1 = $query1->results();
    $query2 = $db->query("SELECT * FROM tableb WHERE Location IN ('$wh')");
    $result2 = $query2->results();
    $query3 = $db->query("SELECT * FROM tablec");
    $result3 = $query3->results();
    $currentTime = date('Y-m-d H:i:s');
    foreach ($result3 as $b) {
      $username = $b->email;
      $password = $b->password;
      $Times = $b->P2_Time;
    }
    $minutePerDay = (24 * 60) / $Times;
    echo $minutePerDay;
    foreach ($result1 as $r) {
      $TimeP2SendE = $r->Timea;
      $TimeDifferentE = (strtotime($currentTime) - strtotime($TimeP2SendE)) / 60;
if($TimeDifferentE >= $minutePerDay){
    sendemail($username,$password,$email,$title);
}
    }
    foreach ($result2 as $r) {
      $TimeP2SendG = $r->Timea;
     if($TimeDifferentG >= $minutePerDay){
    sendemail($username,$password,$email,$title);
}
    }

public function sendEmail($username,$password,$email,$title){
 $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->Host = "smtp.gmail.com";
  $mail->Port = 465;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'ssl';
  $mail->Username = $username;
  $mail->Password = $password;
  $mail->AltBody = " ";
  $mail->From = $username;
  $mail->FromName = 'Noti';
  $mail->clearAddresses();
  $mail->AddAddress($email);
  $mail->Subject = $title;
  $mail->IsHTML(true);
  $mail->Body = '<html><body>';
  $mail->Body .= "<h3>Information  </h3>";
  $mail->Body .= '<table class="table table-border">';
  $mail->Body .= "</table>";
  $mail->Body .= "</body></html>";

  if (!$mail->Send()) {
    echo "Error sending: " . $mail->ErrorInfo;
  } else {
    echo "Letter is sent";
  }
}