This commit is contained in:
Javier Casares 2026-02-18 16:08:14 +00:00
commit fd33d35baf
5 changed files with 100 additions and 82 deletions

View file

@ -130,92 +130,94 @@ if ( $plugin_manually_loaded ) {
$queue = \Robotstxt_SMTP_Newsletter\Queue::get_instance();
$stats = \Robotstxt_SMTP_Newsletter\Statistics::get_instance();
// Process batch.
$items = $queue->get_and_lock_batch( $batch_size, $worker_id );
$total_success = 0;
$total_failure = 0;
if ( empty( $items ) ) {
echo "No items to process\n";
exit( 0 );
}
// Continuous loop: keep processing batches until the queue is empty.
while ( true ) {
$items = $queue->get_and_lock_batch( $batch_size, $worker_id );
echo sprintf( "Worker %s: Processing %d emails...\n", $worker_id, count( $items ) );
if ( empty( $items ) ) {
break;
}
$success_count = 0;
$failure_count = 0;
foreach ( $items as $item ) {
try {
// Build headers array.
$headers = array();
foreach ( $items as $item ) {
try {
// Build headers array.
$headers = array();
// Add From header if specified.
if ( ! empty( $item->from_email ) ) {
$from = ! empty( $item->from_name )
? sprintf( '%s <%s>', $item->from_name, $item->from_email )
: $item->from_email;
$headers[] = 'From: ' . $from;
}
// Add From header if specified.
if ( ! empty( $item->from_email ) ) {
$from = ! empty( $item->from_name )
? sprintf( '%s <%s>', $item->from_name, $item->from_email )
: $item->from_email;
$headers[] = 'From: ' . $from;
}
// Add Reply-To header if specified.
if ( ! empty( $item->reply_to ) ) {
$headers[] = 'Reply-To: ' . $item->reply_to;
}
// Add Reply-To header if specified.
if ( ! empty( $item->reply_to ) ) {
$headers[] = 'Reply-To: ' . $item->reply_to;
}
// Add Content-Type header (prefer HTML if available).
if ( ! empty( $item->body_html ) ) {
$headers[] = 'Content-Type: text/html; charset=UTF-8';
}
// Add Content-Type header (prefer HTML if available).
if ( ! empty( $item->body_html ) ) {
$headers[] = 'Content-Type: text/html; charset=UTF-8';
}
// Decode and add custom headers from JSON.
if ( ! empty( $item->headers_json ) ) {
$custom = json_decode( $item->headers_json, true );
if ( is_array( $custom ) ) {
foreach ( $custom as $key => $value ) {
$headers[] = sprintf( '%s: %s', $key, $value );
// Decode and add custom headers from JSON.
if ( ! empty( $item->headers_json ) ) {
$custom = json_decode( $item->headers_json, true );
if ( is_array( $custom ) ) {
foreach ( $custom as $key => $value ) {
$headers[] = sprintf( '%s: %s', $key, $value );
}
}
}
// Select body (prefer HTML over text).
$body = ! empty( $item->body_html ) ? $item->body_html : $item->body_text;
if ( empty( $body ) ) {
throw new \Exception( 'Email body is empty' );
}
// Send via wp_mail (uses core SMTP configuration).
$result = wp_mail(
$item->to_email,
$item->subject,
$body,
$headers
);
if ( $result ) {
$queue->mark_sent( $item->id );
$total_success++;
echo sprintf( " ✓ [%s] Sent: %s\n", $worker_id, $item->to_email );
} else {
$queue->mark_failed( $item->id, 'wp_mail returned false' );
$total_failure++;
echo sprintf( " ✗ [%s] Failed: %s (wp_mail returned false)\n", $worker_id, $item->to_email );
}
} catch ( \Exception $e ) {
$queue->mark_failed( $item->id, $e->getMessage() );
$total_failure++;
echo sprintf( " ✗ [%s] Error: %s - %s\n", $worker_id, $item->to_email, $e->getMessage() );
}
// Select body (prefer HTML over text).
$body = ! empty( $item->body_html ) ? $item->body_html : $item->body_text;
if ( empty( $body ) ) {
throw new \Exception( 'Email body is empty' );
}
// Send via wp_mail (uses core SMTP configuration).
$result = wp_mail(
$item->to_email,
$item->subject,
$body,
$headers
);
if ( $result ) {
$queue->mark_sent( $item->id );
$success_count++;
echo sprintf( " ✓ Sent: %s\n", $item->to_email );
} else {
$queue->mark_failed( $item->id, 'wp_mail returned false' );
$failure_count++;
echo sprintf( " ✗ Failed: %s (wp_mail returned false)\n", $item->to_email );
}
} catch ( \Exception $e ) {
$queue->mark_failed( $item->id, $e->getMessage() );
$failure_count++;
echo sprintf( " ✗ Error: %s - %s\n", $item->to_email, $e->getMessage() );
}
// Sync statistics after each batch.
$stats->sync_from_queue();
}
// Sync statistics after batch processing.
$stats->sync_from_queue();
echo sprintf(
"Worker %s: Done. Success: %d, Failed: %d\n",
$worker_id,
$success_count,
$failure_count
);
if ( 0 === $total_success && 0 === $total_failure ) {
echo sprintf( "Worker %s: No items to process\n", $worker_id );
} else {
echo sprintf(
"Worker %s: Done. Success: %d, Failed: %d\n",
$worker_id,
$total_success,
$total_failure
);
}
exit( 0 );