If you want to or need to replace wp_mail() smtp with php mail() smtp this filter can solve that for you.
<?php
/**
* Force WordPress to use native PHP mail() function with SMTP configuration
* Supports: to, subject, message, headers, and attachments
*/
add_filter('pre_wp_mail', 'force_native_mail', 10, 2);
function force_native_mail($null, $atts) {
$smtp_host = "mail.yoursite.com";
$smtp_port = 587;
$smtp_user = "webmaster-smtp@yoursite.com";
$smtp_pass = "rT85S2q65b26EUo2";
$smtp_security = "tls";
$smtp_from = "dev-site-webmaster-smtp@yoursite.com";
$smtp_from_name = "Your Site Name";
$to = $atts['to'];
$subject = $atts['subject'];
$message = $atts['message'];
$headers = isset($atts['headers']) ? $atts['headers'] : '';
$attachments = isset($atts['attachments']) ? $atts['attachments'] : array();
// Convert to array to string if needed
if (is_array($to)) {
$to = implode(', ', $to);
}
// Convert headers array to string if needed
if (is_array($headers)) {
$headers = implode("\r\n", $headers);
}
// Check if we need to handle attachments
if (!empty($attachments)) {
// Generate boundary for multipart email
$boundary = md5(time());
// Add From header
$from_header = "From: " . $smtp_from_name . " <" . $smtp_from . ">\r\n";
$from_header .= "Reply-To: " . $smtp_from . "\r\n";
$from_header .= "MIME-Version: 1.0\r\n";
$from_header .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\r\n";
// Add custom headers if any
if (!empty($headers)) {
$from_header .= $headers . "\r\n";
}
// Build multipart message
$body = "--{$boundary}\r\n";
// Determine content type from headers or default to text/html
$content_type = "text/html";
if (strpos($headers, 'Content-Type: text/plain') !== false) {
$content_type = "text/plain";
}
$body .= "Content-Type: {$content_type}; charset=UTF-8\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $message . "\r\n\r\n";
// Handle attachments
if (!is_array($attachments)) {
$attachments = array($attachments);
}
foreach ($attachments as $attachment) {
if (file_exists($attachment)) {
$filename = basename($attachment);
$file_content = file_get_contents($attachment);
$file_content = chunk_split(base64_encode($file_content));
// Detect MIME type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $attachment);
finfo_close($finfo);
$body .= "--{$boundary}\r\n";
$body .= "Content-Type: {$mime_type}; name=\"{$filename}\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
$body .= $file_content . "\r\n";
}
}
$body .= "--{$boundary}--";
// Send email with attachments
mail($to, $subject, $body, $from_header);
} else {
// No attachments - simple email
// Ensure headers end with CRLF
if (!empty($headers) && substr($headers, -2) !== "\r\n") {
$headers .= "\r\n";
}
// Add From header
$from_header = "From: " . $smtp_from_name . " <" . $smtp_from . ">\r\n";
$from_header .= "Reply-To: " . $smtp_from . "\r\n";
// Add content type header if not present
if (strpos($headers, 'Content-Type') === false) {
$from_header .= "Content-Type: text/html; charset=UTF-8\r\n";
}
// Combine headers
$full_headers = $from_header . $headers;
// Send simple email
mail($to, $subject, $message, $full_headers);
}
// Return true to prevent wp_mail from running
return true;
}
if you need to just replace wp_mail() with mail() without smtp only to use php to send the mails here is the simpler solution;
<?php
/**
* Replace wp_mail with native PHP mail()
*/
add_filter('pre_wp_mail', 'use_native_mail', 10, 2);
function use_native_mail($null, $atts) {
$to = $atts['to'];
$subject = $atts['subject'];
$message = $atts['message'];
$headers = isset($atts['headers']) ? $atts['headers'] : '';
$attachments = isset($atts['attachments']) ? $atts['attachments'] : array();
// Convert to array to string if needed
if (is_array($to)) {
$to = implode(', ', $to);
}
// Convert headers array to string if needed
if (is_array($headers)) {
$headers = implode("\r\n", $headers);
}
// Use native mail() - ignoring attachments for simplicity
mail($to, $subject, $message, $headers);
// Return true to prevent wp_mail from running
return true;
}