I have a blog website on WordPress that sends emails to subscribers which are of their interests, emails are sent automatically if the post title contains any of the keywords that are predefined by subscribers. As I started facing issues sending massive emails out (limited to 400 emails per hours on SiteGround) I thought of storing the emails into a temporary table and then send them in batches.
Also, I can group the posts in one single email and send them to the subscribers rather than sending individual email for each matching post. Below is my full code which I kept under Astra Child Theme (Function.php), the code does not write anything to the table, and I am unable to figure out the issue.
I am a programmer from the old golden days (Fortran, C++), and fairly new to PHP, but can digest things quickly. I am using TablePress plugin to create the table, and I am using it without the header so I am assuming column names are the default A, B, C....etc.
$custom_post_types = array('general_vacancy', 'mol_vacancies');
// Hook into the transition_post_status action
add_action('transition_post_status', 'send_email_notifications', 10, 3);
// The function to send email notifications and write to the custom table
function send_email_notifications($new_status, $old_status, $post) {
global $custom_post_types; // Make $custom_post_types variable accessible within the function
global $wpdb; // Make $wpdb variable accessible within the function
// Get the post type and URL of the published post
$post_type = get_post_type($post);
$post_url = get_permalink($post);
// Check if the post type is in the array of custom post types
if (in_array($post_type, $custom_post_types) && $new_status == 'publish' && $old_status != 'publish') {
// Retrieve the post title
$post_title = get_the_title($post);
// Get all users
$users = get_users();
// Loop through each user
foreach ($users as $user) {
// Get the user's ID
$user_id = $user->ID;
// Get the user's predefined keywords from the "keyword_1" user meta field as a string
$user_keywords_string = get_user_meta($user_id, 'keyword_1', true);
// Split the string into an array of keywords using the comma as the delimiter
$user_keywords = explode(',', $user_keywords_string);
// Trim any whitespace around each keyword
$user_keywords = array_map('trim', $user_keywords);
// Check if the user's predefined keywords are found in the post title
$match_found = false;
foreach ($user_keywords as $keyword) {
$regex_pattern = '/\b' . preg_quote($keyword, '/') . '\b/u';
if (preg_match($regex_pattern, $post_title)) {
$match_found = true;
break;
}
}
// If a match is found, send the email
if ($match_found && !empty($user->keyword_1)) {
// Customize the email subject with the post title and post type
$email_subject = 'New Vacancy Published, an exciting job Opportunity awaits!!!';
// Customize the email body with the post URL
$email_body = 'A new job vacancy has been published, it might be of your interests: ' . "\n\n"
. 'Vacancy Title: ' . $post_title . "\n"
. 'View it here: ' . $post_url . "\n\n"
. 'Wishing you the very best of luck with the new job opportunity.' . "\n\n"
. 'Bahrain JOBS' . "\n\n"
. 'Note: You are receiving this email notification because the job vacancy matches one of your "Predefined Keywords". You can always modify the list for better results by editing your profile on our Website www.bahrain-jobs.com';
// Set the recipient's email address
$recipient_email = $user->user_email;
// Send the email
wp_mail($recipient_email, $email_subject, $email_body);
// Get the table ID of the "AnwarLog" table in TablePress
$table_id = 1; // Replace 123 with the actual table ID of the "AnwarLog" table
// Get the table name using the table ID
$table_name = $wpdb->prefix . 'tablepress_' . $table_id;
// Insert the email details into the "AnwarLog" table
$wpdb->insert(
$table_name,
array(
'A' => $email_subject,
'B' => $email_body,
'C' => $recipient_email,
'D' => $post_title,
'E' => $post_url
)
);
}
}
}
}