0

I have written and re-written this code several times. I've tried removing the wp_mail function from the loop and putting it in it's own function. I've moved all the variables in to the loop. At one point I had another loop that went through the gravityforms entries and built another array of just emails and messages, and then looped through that to send the emails. This code is run as a cron job once per week. I'll add sanitation and validation once I get the basic functionality working. I've scoured the the internet and have only found solutions that don't seem to fit this situation.

I am trying to achieve dynamic content in the messages so just compiling an array of emails to throw in the "To" parameter is not really an option.

It is only sending maybe 10 emails max, so I know I'm not running into any quotas or limits. Can anyone spot what might not be working?

<?php

require('../../../../wp-load.php');

global $wpdb;

function submissions_set_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','submissions_set_content_type' );


function sendDMVdocumentRequestNotification(){

    $search_criteria = array(
        'status'        => 'active',
        'field_filters' => array(
            'mode' => 'all',
            array(
                'key'   => '94',
                'value' => '1'
            ),
            array(
                'key'   => '26',
                'operator' => 'IS NOT',
                'value' => 'Canceled'
            )
        )
    );

    $entries = GFAPI::get_entries(19, $search_criteria);

    //wp_mail(array( '[email protected]' ), "TEST DATA", var_export($entries, true), array('Content-Type: text/html; charset=UTF-8'));

    foreach($entries as $key=>$entry){

        $email = array( '[email protected]' );
        $subject = "Documents Requested";
        $headers = array('Content-Type: text/html; charset=UTF-8');

        $message = getDMVemailMessage($entries[$key]['id']);

        $sent = sendDMVNotifyEmail($email, $subject, $message, $headers);

 
    }

};

function getDMVemailMessage($id){
    $site = get_site_url();
    return "<img style='width: 350px;' src='https://staging.plus.corp-smart.com/wp-content/uploads/2020/10/corpsmart_logo.png'/><p>You are receiving this email as a reminder that order <a href='" . $site . "/view/dmv-history-corpsmart-side/entry/" . $id . "'>#" . $id . "</a> has outstanding documents that have not been uploaded to your checklist. Please upload the required documents at your earliest convenience so that we can process your request.</p><br><br>Thank You.<br>CORPSMART&trade;, DMV Team";

}

function sendDMVNotifyEmail($emails, $sub, $message, $head){

    if(wp_mail($emails, $sub, $message, $head)){

        return true;
    };

}

sendDMVdocumentRequestNotification();

1 Answer 1

0

Sometimes the mail port can get clogged up, so I usually add a brief sleep(10) in the for-loop. The 10 seconds is arbitrary, but it works for my purposes.

foreach($entries as $key=>$entry){

    $email = array( '[email protected]' );
    $subject = "Documents Requested";
    $headers = array('Content-Type: text/html; charset=UTF-8');

    $message = getDMVemailMessage($entries[$key]['id']);

    $sent = sendDMVNotifyEmail($email, $subject, $message, $headers);

    sleep(10); //Delay for 10 seconds
}

Alternatively, I've also used WP-Cron to process in groups of X at a time, especially if you have hundreds, the sleep() function may cause you to time out. Let me know if you'd like that example code too.

Your Answer

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

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