Bug – NULL email_address causing database error in wp_transmail_failed_emails
-
Hello TransMail team,
I’m experiencing a critical database error that’s breaking email functionality on my WordPress site. The error occurs when the plugin tries to log failed emails but the email_address field is NULL.
Error Message:
WordPress database error Column ’email_address’ cannot be null for query INSERT INTOwp_transmail_failed_emails(headers,email_address,email_subject,email_body,attachments,error_code,error_description,attachment_files) VALUES (”, NULL, ‘[Site Name] Password Changed’, ‘…’, 0, ‘SM_113’, ‘Invalid email address’, ”)Root Cause:
The issue is in the wp_mail() function around line 1397 in transMail.php. When logging failed emails, the code calls:
insert_failed_email($from_email,$to[0], $subject, $message, $responseArray[‘error’][‘details’], $attachment_paths_json);However, there’s no validation to ensure $to[0] exists or isn’t NULL.
Suggested Fixes:
- Add validation before accessing $to[0]:
// Instead of directly using $to[0], validate first:
$to_email = (!empty($to) && is_array($to) && isset($to[0]) && !empty($to[0])) ? $to[0] : ”;
if (empty($to_email)) {
error_log(‘TransMail: Failed email had no valid recipient address’);
return false; // Skip logging if no valid email
}
insert_failed_email($from_email, $to_email, $subject, $message, $responseArray[‘error’][‘details’], $attachment_paths_json); - Add NULL check in insert_failed_email() function (around line 860):
function insert_failed_email($from_address, $email_address, $email_subject, $email_body, $responseArray, $attachments = array()) {
global $wpdb; // Skip logging if email address is invalid
if (empty($email_address) || $email_address === null) {
error_log(‘TransMail: Skipping failed email log – no valid email address provided’);
return false;
} $table_name = $wpdb->prefix . ‘transmail_failed_emails’;
// … rest of function
} - Improve email validation in the processing loop (around line 1250):
foreach($to as $t) {
// … existing code …
$validated_email = transmail_validate_email(sanitize_email($t));
if (!$validated_email) {
error_log(‘TransMail: Invalid email address detected: ‘ . $t);
continue; // Skip invalid emails instead of processing them
}
$toa[‘address’] = $validated_email;
// … rest of loop
}
- Add validation before accessing $to[0]:
You must be logged in to reply to this topic.