简体   繁体   中英

Email is only being sent once

I had asked a question yesterday about sending an email every three days which was answered so I'm not sure if I should create a new one or add to the old.

The check is working fine as I get the correct ouptut on screen, but when I try to send the email it is only sending one result. I've tried foreach loops, putting the mail function in different places none of which seem to work. My code is below, I've removed most of the message parts as they are just long tables.

**UPDATE ****

I've added the recipients array into the while loop and also set $subject and $headers to empty values before adding values. This has worked.

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

your problem lies here:

$recipients[] = $email;

You create your email array outside your loop, thus you only get the last email address value.

Put it in your

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

Try like this inside your while loop. Your mail content is inside the while loop, but you are trying mail function outside the while loop, so it is sending last content to all your mails in $recipients array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM