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:

function wp_mail( $to, $subject, $message, $headers = ”, $attachments = array() ) {
//WP mail function code replaced by Ian L of
// 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:\r\n”;
// Send email:
    if(mail($to,$subject,$txt,$headersnew)) {
    //if mail sent to user send notice to admin:
    $adminsubject = “Admin Notification from WP”;
    } else {
    //if mail didn’t go through, send notice to admin:
    $txterr = “<h2>ERROR!</h2> email to user did not go through!”;
}//end php mail function for wp-mail

Leave a Reply

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