Using wp_mail() instead of mail() in Wordpress does not work

17,161

Solution 1

Insert at line 2:

    define('WP_USE_THEMES', false);
    require('../../../wp-load.php');

The trick is that sendmail.php as originally written doesn't actually load in the wordpress gear, so wp_mail isn't defined.

The first line should be optional. I took it from the sample code at http://butlerblog.com/2012/09/23/testing-the-wp_mail-function/.

Solution 2

By default, the Wordpress function wp_mail() uses PHPs internal mail() function so the change you've made won't fix the issue.

If your web server has disabled the standard PHP mail() function then you will need to switch over to using SMTP for sending your emails.

There's a few different ways to achieve this, but the easiest method I've found has been to use the Easy WP SMTP plugin. Once installed, you'll need to configure the plugin to use the login details for your SMTP server. If you hvae a Gmail address, you can use those details for setting it up.

EDIT: You still need to switch your code over to using the wp_mail() function which will automatically use SMTP (if you have the WP SMTP plugin installed).

Solution 3

I've lost about 30 minutes figuring this out.

If you're using anything that allows you to configure SMTP within WordPress, take it out.

Then put everything into a function:

add_action('init','delay_until_init');
function delay_until_init(){
   // call wp_mail() here
}
Share:
17,161
Sam Hamilton
Author by

Sam Hamilton

Updated on August 19, 2022

Comments

  • Sam Hamilton
    Sam Hamilton over 1 year

    Since PHP mail has been disabled on my server it has stopped a theme integrated contact form from working.

    The theme is called Boldy and it has its own sendmail.php file which uses mail() instead of wp_mail().

    Changing mail() to wp_mail() does not work, but I'm not sure why?

    <?php
    if (isset($_POST['submit']))
    {
        error_reporting(E_NOTICE);
    
        function valid_email($str)
        {
            return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
        }
    
        if ($_POST['name'] != '' && $_POST['email'] != '' && valid_email($_POST['email']) == TRUE && strlen($_POST['comment']) > 1)
        {
            $to = preg_replace("([\r\n])", "", $_POST['receiver']);
            $from = preg_replace("([\r\n])", "", $_POST['email']);
            $subject = "Website contact message from ".$_POST['name'];
            $message = $_POST['comment'];
            $match = "/(bcc:|cc:|content\-type:)/i";
    
            if (preg_match($match, $to) || preg_match($match, $from) || preg_match($match, $message))
            {
                die("Header injection detected.");
            }
    
            $headers = "From: ".$from."\r\n";
            $headers .= "Reply-to: ".$from."\r\n";
    
            if (mail($to, $subject, $message, $headers))
            {
                echo 1; //SUCCESS
            }
            else
            {
                echo 2; //FAILURE - server failure
            }
        }
        else
        {
            echo 3; //FAILURE - not valid email
        }
    
    }
    else
    {
        die("Direct access not allowed!");
    }
    ?>
    
    • jaressloo
      jaressloo almost 12 years
      Was this ever resolved? I'm running into the exact same issue right now for my site...Any help is appreciated.
  • Traderhut Games
    Traderhut Games almost 6 years
    I get a error 500 on the whole website when I put that code in my functions.php file (that is used to send email), not sure if the ../.. is the right number of levels or what