简体   繁体   中英

wp_mail breaking in loop

I need to send a daily email containing updated site content to about 200 users. The way I've set it up is to send one email to every 20 users (all BCC'd), so only about 10 total emails get sent. I'm using the Wordpress wp_mail() function to do the sending along with the WP-Mail-SMTP plugin so that emails are sent using a pre-configured gmail account. Everything was working fine until about a week ago. Now, all of a sudden the script breaks after 5 emails are sent.

//SEND THE EMAIL TO 20 USERS AT A TIME
    foreach ($users as $user) {
        array_push($mailgroup, $user->user_email);
        if(count($mailgroup) == 20) {
            $headers = $headersBase . 'Bcc: '. implode(',', $mailgroup);
            wp_mail('', 'Email Update', $strBody, $headers);
            $sentEmailCount++;
            $headers = '';
            $mailgroup = array();
        }
        $sentUserCount++;
    }

I believe the reason for this is that an email address in the final message sent is getting bounced and then for whatever reason the loop stops. Is there a way to disregard any errors and just continue looping regardless of whether the email message bounces or not?

This ended up being an issue with bumping into a quota set by gmail for outgoing emails. I was using a plugin that set the wp_mail() function to use an SMTP account for outgoing mail. In my case I was using a gmail account. It looks as if gmail does not allow more than 100 outgoing messages to be sent within a certain period of time. I am sending 1 email to every 20 users (all users bcc'd) in each email. I only made it through the email loop 5 times (100 users) before it bombed. Damn gmail.

The fix for me was to set up a Mandrill.com account (free), install the Mandrill Wordpress plugin (free), configure it, and that's it. Now my wp_mail() function uses Mandrill to send messages and there does not seem to be a limit. No modifications to my script were required.

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