WordPress中wp_mail邮件发送异常问题求助
Hey Kevin, let's work through each of your email issues step by step—most of these are tied to a missing function that's corrupting your email headers, plus small code inconsistencies. Here's how to fix everything:
1. Fix the Missing wpb_sender_email Warning (Root Cause of Most Issues)
That PHP warning is the big culprit here. WordPress is trying to run a function called wpb_sender_email (probably from a filter hook in your theme or a custom plugin) that doesn't exist. This messes up your email headers, causing weird formatting like the sender showing up in the subject line.
To fix this:
- Check your theme's
functions.phpfile or any custom plugins for lines like this:add_filter( 'wp_mail_from', 'wpb_sender_email' ); add_filter( 'wp_mail_from_name', 'wpb_sender_name' ); - Option 1: Remove those lines if you don't need custom sender settings.
- Option 2: Define the missing functions to make the hooks work properly:
function wpb_sender_email( $original_email ) { return 'kevin@warenghem.com'; } function wpb_sender_name( $original_name ) { return 'Kevin Brosseau'; }
2. Fix Subject/From Display & HTML Formatting Issues
Your current code has two problems causing formatting bugs:
- You're using HTML entities (
<,>) instead of raw HTML tags in your email body—this makes email clients render them as text instead of parsing them as HTML. - You didn't include code to actually receive and parse the incoming JSON data (so
$emailis undefined, which breaks the$tovariable).
Update your email content and add JSON handling:
// Add this at the top to receive and validate JSON input $json_data = file_get_contents('php://input'); $data = json_decode($json_data, true); // Get and validate the email address $email = $data['email'] ?? ''; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Invalid email address']); exit; } // Fix the HTML body (use raw tags, not entities) $body = '<html><body><h1>Hello World!</h1></body></html>';
3. Fix Email Delivery Problems
Delivery issues usually stem from one of three things:
- Invalid recipient emails: The validation step above will prevent sending to bad addresses.
- Server mail configuration: Many shared hosts restrict PHP's built-in
mail()function (whichwp_mailuses by default). Ask your host ifmail()is enabled, or switch to SMTP for more reliable delivery (you can use a plugin like WP Mail SMTP, or add SMTP settings via code). - DNS records: Make sure your domain has valid SPF and DKIM records set up—these tell email providers your messages are legitimate and not spam.
Full Corrected Code
Here's the complete, fixed version of your script with all the changes above:
// Prevent loading WordPress themes define('WP_USE_THEMES', false); // Load WordPress Core (simplified path—adjust if your script is in a different folder) require_once(dirname(__DIR__) . '/wp-load.php'); // Receive and parse incoming JSON $json_data = file_get_contents('php://input'); $data = json_decode($json_data, true); // Validate email address $email = $data['email'] ?? ''; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Invalid email address']); exit; } // Define sender functions to fix the hook warning function wpb_sender_email( $original_email ) { return 'kevin@warenghem.com'; } function wpb_sender_name( $original_name ) { return 'Kevin Brosseau'; } add_filter('wp_mail_from', 'wpb_sender_email'); add_filter('wp_mail_from_name', 'wpb_sender_name'); // Email settings $to = $email; $subject = 'Arbre planté'; $body = '<html><body><h1>Hello World!</h1></body></html>'; $headers = [ 'Content-Type: text/html; charset=UTF-8', 'From: Kevin Brosseau <kevin@warenghem.com>' ]; // Send email and return status $mail_sent = wp_mail($to, $subject, $body, $headers); if ($mail_sent) { echo json_encode(['status' => 'success', 'message' => 'Email sent successfully']); } else { http_response_code(500); echo json_encode(['status' => 'error', 'message' => 'Failed to send email']); }
Give this a shot—most of your issues should disappear once the missing function is fixed and the HTML/JSON handling is corrected. For persistent delivery problems, checking your host's mail settings and DNS records is the next step.
内容的提问来源于stack exchange,提问作者Kevin

