0

I have a function that sends a notification to my subscribers when publish_post is triggered.

However, when I click “publish”, WP stays on the admin page processing all 1095 emails.

Below is my function.

How can I change it so the processing goes on in the background so I can continue working on another post etc.?

I'm thinking the loop below the SQL may be the problem since it reads an address, sends an email, reads an address, sends an email, repeat. Can I somehow put the entire list in Bcc and send one email? Bad idea? What’s the best way get the function to “let go” of the new post page? All solutions welcome!

BTW, sending through AWS, not Wordpress built-in mail function.

function new_post_notify($ID, $post){
    global $full_site_url;//used in email link.
    global $site_title;;//used in email subject.
    global $domain;//used in email message.
    global $wpdb;//make wordpress db class available to function.

    if ( $post->post_date != $post->post_modified ){
        //THIS IS A POST UPDATE, DO NOTHING
    } else {
        $title = $post->post_title;
        $permalink = get_permalink( $ID );
        $table = $wpdb->prefix . "dw_subscribe"; //Define table for sql query.
        //Get the distinct email address for each subscriber. (should be no duplicates but distinct serves as insurance.)
        $subscribers = $wpdb->get_results(
            "SELECT DISTINCT email
            FROM $table
            ORDER BY email ASC");
        foreach($subscribers as $val) { //loop through array. For each address, send the email.
            $subscriber = ($val->email);

            //Assign email to, subject, message to variables. Call email sending function.
            $to = $subscriber;
            $subject = 'New Sketch Posted at ' . $site_title . ' by Don West • ' . $title . ' •';
            $message = '<p>Howdy Friend!</p>';
            $message .= "<p>I just wanted to let you know I've posted a new sketch for you over at " . $domain . ".</p>";
            $message .= '<h3 style="text-align: center;"><a href="' . $permalink . '">' . $title . '</a></h3> ';
            $message .= 'Best regards,<br><img align="left" src="' . $full_site_url . '/wp-content/uploads/2018/02/dwsignature.png" alt="Don West" width="200" height="135">';
            $message .= '<div style="margin: 275px auto 0 auto;text-align: center;"><a href="' . $full_site_url . '/unsubscribe/?email=' . $subscriber . '">unsubscribe</a></div>';
            dw_send_email($to, $subject, $message);
        }
    }
}
  • to do send e-mails in the background, you can use WP cron : developer.wordpress.org/plugins/cron – mmm Apr 25 '18 at 20:07
  • 1
    That's a lot of emails to send, and there's a good chance your server will get marked as spam. You should consider a service such as mailchimp etc if you want reliable fast email. It also saves coding up the unsubscribe and data compliance parts if a service can provide those for you – Tom J Nowell Apr 25 '18 at 20:15
  • If you're going to stick with quick and dirty emails straight from your own server, BCC is just fine. Send it to everyone at once rather than mailing over and over. But as Tom mentioned, this is a good way to have those emails end up in junk/spam folders, so an ESP would be preferable if these are not people on your team who you can instruct to whitelist your server's email address and who won't be wanting to unsubscribe. – WebElaine Apr 25 '18 at 21:32
  • @WebElaine - BCC'ing 1095 emails is not recommended at all. Many email providers will simply see this as spam and block. – Mat Apr 25 '18 at 23:37
  • 1
    No, don't do that.When you publish a post call the publish_postaction hook to schedule a cron job so that procesing can go on its own... – John Zenith Apr 26 '18 at 1:57

Your Answer

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

Browse other questions tagged or ask your own question.