How to replace wp_mail Function with PHP mail function in WordPress

Be aware that this modification should probably only be used in extreme cases where you can not get WordPress to send emails any other way because this method requires changes to the WordPress core. Making changes to the core files means that you could loose those changes if you update to a different version of WordPress in the future. If you update, you will have to make the changes again. Luckily it is one simple change to one file, so it’s not really that bad. I often prefer to use such a hack instead of adding another plugin to my WordPress blog.

To replace the default wp_mail function with one that users PHP mail, find the pluggable.php file located in wp-includes  and replace that wp_mail function with this one:

<?php
function wp_mail( $to, $subject, $message, $headers = ”, $attachments = array() ) {
//WP mail function code replaced by Ian L of jafty.com:
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($message,70);
//Always set content-type in headers when sending HTML email:
$headersnew = “MIME-Version: 1.0” . “\r\n”;
$headersnew .= “Content-type:text/html;charset=UTF-8” . “\r\n”;
//Use headers to set from address:
$headersnew .= “From: peter@michaelson.com.au\r\n”;
// Send email:
    if(mail($to,$subject,$txt,$headersnew)) {
    //if mail sent to user send notice to admin:
    $adminsubject = “Admin Notification from WP”;
    mail(‘admin@example.com’,$adminsubject,$txt,$headersnew);
    } else {
    //if mail didn’t go through, send notice to admin:
    $txterr = “<h2>ERROR!</h2> email to user did not go through!”;
    mail(‘admin@example.com’,$subject,$txterr,$headersnew);
    }
}//end php mail function for wp-mail
?>

Leave a Reply

Your email address will not be published. Required fields are marked *