电子邮件只发送一次

时间:2014-02-13 12:49:50

标签: php email

我曾经问question yesterday每三天发送一封电子邮件,这已经回答了,所以我不确定是应该创建一个新邮件还是添加旧版本。

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

**更新****

我已将recipients数组添加到while循环中,并在添加值之前将$ subject和$ headers设置为空值。这已经奏效了。

$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@email.com>' . "\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";
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你的问题在于:

$recipients[] = $email;

您在循环外创建电子邮件阵列,因此您只能获得最后一个电子邮件地址值。

把它放在你的

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

答案 1 :(得分:1)

在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@email.com>' . "\r\n";
    mail($email, $subject, $message, $headers);