电子邮件只发送一次

问题描述:

我问了一个question yesterday关于每三天发送一封电子邮件已回复,所以我不确定是否应该创建一个新的或添加到旧的。电子邮件只发送一次

检查工作正常,因为我在屏幕上显示正确的电子邮件,但是当我尝试发送电子邮件时,它只发送一个结果。我已经尝试了foreach循环,把邮件功能放在不同的地方,这些地方都不起作用。我的代码如下,我删除了大部分消息部分,因为它们只是长表。

**更新****

我添加值前添加收件人数组while循环,也可以设置$主题,$头空值。这工作。

$sql = "SELECT * FROM bookings " . 
     "WHERE DATE(date) > DATE(NOW()) " . 
     "AND dateofquote != '' " . 
     "AND email != '' " . 
     "AND confirmed = 0"; 

$result = mysql_query($sql); 
$num_rows = mysql_numrows($result); 

$today = date('Y-m-d'); 

if ($num_rows) { 
    while ($row = mysql_fetch_array($result)) { 
       $recipients = array(); // Updated 
     $id = $row['id']; 
     // rest of rows from db   

     $date_time1 = new DateTime($today); 
     $date_time2 = new DateTime($date_of_quote); 
     $interval = $date_time1->diff($date_time2); 
     $diff = $interval->format('%a'); 

     if ($diff % 3 == 0) { 

      if ($type == 'W') { 
       $message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">'; 
       // rest of table 

       echo '<h1>Weddding Email</h1>'.$message.'<br />End Wedding<br /><br /><hr>'; 
       // tried to send email from here for this option 
      } 
      elseif ($type == 'D') {    
       $message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">'; 
       // rest of table 

       echo '<h1>Debs Email</h1>'.$message.'<br />End Debs<br /><br /><hr>'; 
       // tried to send email from here for this option 
      } 
      elseif ($type == 'CR') { 
       $message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">'; 
       // rest of table 

       echo '<h1>Country Run Email</h1>'.$message.'<br />End Country Run<br /><br /><hr>'; 
       // tried to send email from here for this option 
      } 
      elseif ($type == 'H') { 

       $message = '<table width="95%" border="0" cellspacing="0" align="center" style="border:1px solid #999;">'; 
       // rest of table 

       echo '<h1>Hourly Email</h1>'.$message.'<br />End Hourly<br /><br /><hr>'; 
       // tried to send email from here for this option 
      } 
     } else { 
      echo 'something went wrong'; 
     } 

     $recipients[] = $email; 
       $subject = ''; // Updated 
       $headers = ''; // Updated 
     $subject .= "An $type_value Enquiry has been received from company.ie"; 
     $headers .= 'MIME-Version: 1.0' . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
     $headers .= 'From: Name <[email protected]>' . "\r\n"; 

     foreach($recipients as $to){ 
      if(mail($to, $subject, $message, $headers)) { 
       echo "E-Mail Sent to "; 
       echo $to.'<br />'; 
      } else { 
       echo "There was a problem"; 
      } 
     } 
    } 
} 
+0

您的代码已弃用功能,更新到新的PHP版本,不要使用已弃用的功能。 $电子邮件的定义在哪里 –

+0

电子邮件是在这里定义的 -//来自数据库的其余行。我只是将它排除在外,因为列表太长了 –

尝试这样while循环中。您的邮件内容位于while循环内,但您正在尝试使用while循环外的邮件功能,因此它将最后一个内容发送到$ recipients数组中的所有邮件。

$subject=''; 
    $headers=''; 
    $subject .= "An $type_value Enquiry has been received from company.ie"; 
    $headers .= 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $headers .= 'From: Absolute Limos <[email protected]>' . "\r\n"; 
    mail($email, $subject, $message, $headers); 
+0

你的意思是在我的if循环中?该电子邮件是在while循环中发送的 –

您的问题就在这里:

$recipients[] = $email; 

您创建电子邮件阵列的外循环,这样你只能得到最后的电子邮件地址值。

把它放在你

while ($row = mysql_fetch_array($result)) { 
+0

实际上它在while循环中! – wesamly