0

I have the following code that was written to send an email reminder to users, but it only sends an email to the first user in the list and the remaining users are not emailed. The query does work in SQL to pull all of the users

$users = array(mysql_fetch_array(mysql_query("SELECT DISTINCT wp_users.* FROM wp_users LEFT JOIN (SELECT lead_id, MAX(case when field_number = 7 then value end) AS email, MAX(case when field_number = 8 then value end) AS tournament_id FROM wp_rg_lead_detail GROUP BY lead_id) AS t1 ON t1.email = wp_users.user_email AND t1.tournament_id = '$tournament_id' WHERE t1.lead_id IS NULL AND id !=1")));


// Create the replacements array
$replacements = array();
foreach ($users as $user) {
$replacements[$user['user_email']] = array (
'{user_login}' => $user['user_login']
);
echo $user['user_email'] . "<br />" . $user['user_login']; 
}

// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();

// Create an instance of the plugin and register it
$plugin = new Swift_Plugins_DecoratorPlugin($replacements);
$mailer = Swift_Mailer::newInstance($transport);
$mailer->registerPlugin($plugin);

// Create the message
$message = Swift_Message::newInstance();
$message->setSubject("Pure Fantasy Golf Pick Reminder for {user_login}");
$message->setBody("{user_login}, this is a reminder that you need to submit a pick for this weeks tournament " . $tournament_name . ". Please go to http://dev.purefantasygolf.com to make your pick now.");
$message->setFrom("[email protected]", "Pure Fantasy Golf");

// Send the email
foreach($users as $user) {
$message->setTo($user["user_email"], $user["user_login"]);
$mailer->send($message);
}
2
  • var_dump($users), does this contain what you want? Commented Mar 5, 2014 at 16:14
  • you mean you did not even print_r($users) to see if you had all your rows? Commented Mar 5, 2014 at 16:23

3 Answers 3

2

mysql_fetch_array does not fetch an array containing all rows. It fetches one row at a time in an array with the value in each column in an array element.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at the documentation of mysql_fetch_array.

You need to loop over your results like this:

$result = mysql_query("SELECT DISTINCT wp_users.* FROM wp_users LEFT JOIN (SELECT lead_id, MAX(case when field_number = 7 then value end) AS email, MAX(case when field_number = 8 then value end) AS tournament_id FROM wp_rg_lead_detail GROUP BY lead_id) AS t1 ON t1.email = wp_users.user_email AND t1.tournament_id = '$tournament_id' WHERE t1.lead_id IS NULL AND id !=1");

$replacements = array();
while($user = mysql_fetch_array($result)) {
    $replacements[$user['user_email']] = array (
        '{user_login}' => $user['user_login']
    );
    echo $user['user_email'] . "<br />" . $user['user_login'];
}

Comments

0

I don't think that's a good idea to send N number of emails in loop. This could cause performance issues (if there is more than 10 users in mailing list). I think the better solution here is to add all users as BCC and send only one email.

1 Comment

Good idea, but it should be the bcc or you will have a serious issue with privacy protection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.