Drupal自定义PHPMailer邮件插件发送异常:邮件成功送达但报错且重复收到错误提示邮件
Hey there! Let's dig into the weird behavior you're seeing—where emails land in Mailtrap successfully but Drupal throws an error, plus those 4 extra error emails. Here are the most likely culprits to check:
1. Your Plugin Isn't Returning the Correct Success Status to Drupal
Drupal’s email system relies on the return value of your mail plugin’s mail() method to determine if sending succeeded. Even if PHPMailer sends the email perfectly, if your function doesn’t explicitly return TRUE after a successful send, Drupal will assume it failed and show that "Unable to send email" warning.
Double-check your code:
public function mail(array $message) { // ... your PHPMailer setup code ... try { $mail->send(); // Make sure you return TRUE here! return TRUE; } catch (Exception $e) { // Log the error properly instead of letting Drupal catch it unexpectedly \Drupal::logger('your_module')->error('Email failed: @error', ['@error' => $e->getMessage()]); return FALSE; } }
2. Missing Reply-To Header Triggering Your Error Logger (4 Extra Emails)
You mentioned you built an email logger to send error messages—that’s exactly where those 4 emails are coming from. The error text "Error sending email (from xxx to yyy with reply-to not set)" indicates your logger is detecting that the outgoing email lacks a Reply-To header, and it’s firing off an error notification each time this happens.
Fix this by explicitly adding the Reply-To header in your plugin:
- Either set it in the Drupal message array before passing to PHPMailer:
$message['headers']['Reply-To'] = $message['from']; - Or use PHPMailer’s native method:
list($fromEmail) = explode(' ', $message['from'], 2); $mail->addReplyTo($fromEmail);
Also, check if your logger is set to trigger multiple times (e.g., on multiple hook invocations) which would explain the 4 duplicate error emails.
3. Partial Override of Drupal’s Default Email System
If your plugin isn’t registered as the default mail system for Drupal, some parts of the email process might still be falling back to the native PHP mail() function—which you know doesn’t support HTML headers properly. This could cause Drupal to detect a failure from the default system, even though your PHPMailer flow worked.
Verify your hook_mail_system() implementation is correctly setting your plugin as the default:
function your_module_mail_system() { return [ 'default-system' => '\Drupal\your_module\Plugin\Mail\YourCustomMailer', ]; }
Make sure no other modules (like core’s mail module or third-party email modules) are overriding this setting in the site configuration.
4. Unhandled PHPMailer Warnings/Non-Fatal Errors
Even if the email sends successfully, PHPMailer might be generating warnings (like debug output or minor header issues) that Drupal’s error handler picks up, leading to the false failure message.
- Disable SMTP debug mode if it’s enabled:
$mail->SMTPDebug = 0; // Set to 0 to turn off debug output - Ensure you’re catching all PHPMailer exceptions (including
PHPMailer\PHPMailer\ExceptionandPHPMailer\PHPMailer\SMTPException) to prevent them from bubbling up to Drupal’s error system.
内容的提问来源于stack exchange,提问作者Mind Optimizer

