0

I need to run a for loop to go through all the users on my site and send any that are applicable an email. A user can have several categories on their profile, let's say 4. If the customer has elected to receive emails, it then loops through their 4 categories. I want to send them a unique email for each category.

This function works without the for loop. If I remove it, I successfully receive an email for the first category. As soon as I add that loop in, it fails and I don't get any email.

So two questions really - any suggestions on how to get the for loop to work? And is there a log file for Cron jobs that I would be able to review? I checked some plug-ins but can't find what I'm looking for.

Thank you!!

<?php
function check_user_tasks_week_before() {

    function set_html_content_type() {
        return 'text/html';
    }

    add_filter( 'wp_mail_content_type', 'set_html_content_type' ); //switches the emails to html
    $Number_of_Categories = 4;
    $user_info = get_userdata(1);

    if ($user_info->email_option == "Yes") {
        for ($z = 1; $z <= $Number_of_Categories; $z++) { //loop through each category      
            $subject = '';
            $message = '';
            $headers = 'From: Me <test@email.com>' . "\r\n";
            $subject = 'Email Reminder: ' . $user_info->category_1_step_1_category;
            ob_start();
            ?>
            <p>this is some info</p>
            <h1>this is a header</h1>
            <p>Here is a nice reminder for you <?php echo $user_info->user_login ?></p>
            <?php

            $message = ob_get_contents();

            ob_end_clean();

            wp_mail( 'test@email.com', $subject, $message, $headers);

        } //end for ($z = 1; $z <= $Number_of_Categories; $z++)
    } //end ($user_info->email_option == "Yes")

    remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); //switches the emails to text to avoid issues with other wp_mail() calls

} //end check_user_tasks_week_before
?>
  • You should debug the code by writing the log your self to a file and then verify if cron is running + loop is working + wp_email has correct email addresses etc. – M-R Nov 10 '15 at 17:36
  • Thank you for the suggestion M-R! How do I go about doing that? – Justin Nov 10 '15 at 17:56
  • Just for an idea, you can use file_put_contents(date('Y-m-d').'.txt',$LOG_MESSAGE, FILE_APPEND). – M-R Nov 11 '15 at 13:50
0

For whatever reason, this part:

for ($z = 1; $z <= $Number_of_Categories; $z++)

is the offender. even though $Number_of_Categories was defined above where it is used, the value wasn't holding. I had to build a function instead, and do this:

function get_Number_of_Categories(){
  return "2";
}

for ($z = 1; $z <= get_Number_of_Categories(); $z++)

once I did that, the loop worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.