1

I'm totally lost by the mail function in WordPress (WP 4.6 multisites).

I tested locally (on my localhost) WordPress send mail. This works fine at first sight (I don't really know why because I've never configured any SMTP server address in WordPress). When a user is created, a notification mail is correctly sent to the right address.

BUT, if I try to use the wp_mail() function in one of my theme pages, it just doesn't work! If I use instead the mail() php function, it works perfectly. (php mail() is set on my smtp server 'smtp' which is set on my provider's SMTP server, with a valid 'sender address')

There is about the same question here (Dead link, question was removed). The difference is that in my case, wp_mail() does work in the dashboard part but not in my theme... MANY thanks to you for a track, Pierre. (hope this post will be posted ! :)

  • There is a bug relating to 4.6 which minght cause it depending on server configuration and maybe the email addresses being actually used (sorry, don't remember its number) which might or might not be the cause for this (as far as I understand it relates to the legality of the sender address that triggers the issue) – Mark Kaplun Aug 20 '16 at 15:46
  • Did my answer resolve your question? If so, please upvote it and click the green checkmark to let the community know. Thanks. – Ethan Jinks O'Sullivan Aug 22 '16 at 17:41
  • downvoted as without more debuging effort you are left with only guessing as to what the cause might be – Mark Kaplun Jan 14 '18 at 13:58
1

The plugin, WP Mail SMTP, looks like a possible solution to use the wp_mail() properly on your localhost. According to the description:

Reconfigures the wp_mail() function to use SMTP instead of mail() and creates an options page to manage the settings.

  • Thanks a lot Ethan for answering....I just precise that I AM my own hosting so far, and the problem is on my localhost, this is the point ! haven't given the smallest parameter concerning a SMTP server to WP, and despite this, it sends a mail with wp_mail() and it WORKS fine in the dashboard part...My second problem is that wp_mail() doesn't work in the theme page... – Pierre Aug 20 '16 at 13:47
  • Okay, so everything you are doing is on your localhost. From reading your question, I was under the impression that your tests works on the localhost and then you tried it on your live online server. What are you using to run your localhost? WAMP, XAMPP, etc. Using the mailing functionality usually doesn;t work because extra configuration is needed. – Ethan Jinks O'Sullivan Aug 20 '16 at 13:55
  • I am on Linux apche2 php7. – Pierre Aug 20 '16 at 13:57
  • By the way, my smtp server works fine and can send a mail (using the SMTP address of my provider "Free" and my "Free" email adress as sender) and so does my php function mail() – Pierre Aug 20 '16 at 13:59
  • @Pierre, those are the specs that you gave me for your localhost server. But what did you use to set them up? For example, I'm using WAMP as my tool to set up the environment (screenshot). Like I said, sending mail on a localhost isn't very practical and I'd recommend testing out the wp_mail() function on a live server. – Ethan Jinks O'Sullivan Aug 20 '16 at 14:06
1

Add action method phpmailer_init in the theme functions.php. And configure settings with your SMTP server details.

add_action( 'phpmailer_init', 'phpmailer_ss_SMTP' );
function phpmailer_ss_SMTP( $phpmailer )
{
    $phpmailer->IsSMTP();
    $phpmailer->Host        = 'mail.domainname.com';
    $phpmailer->Port        = 588; 
    $phpmailer->SMTPAuth    = true; 
    $phpmailer->Username    = 'info@domainname.com';
    $phpmailer->Password    = 'Set Password';
    $phpmailer->From        = 'info@domainname.com';
    $phpmailer->FromName    = 'Website Name';
    $phpmailer->AuthType    = 'LOGIN';
    $phpmailer->SMTPAutoTLS = false;
    $phpmailer->SMTPSecure  =''; 
}
0

The other answers give good solutions to checking into setting up SMTP, which you should do regardless of whether it's your actual problem or not.

However, based on your description, I don't think that's the actual problem here. It is more likely that where you are trying to use wp_mail() the function has not been loaded yet.

Are you running WP in debug mode? If not, enable it and test again. If you get an error that the wp_mail() function is undefined, then your issue is in fact that wp_mail() (and pluggable.php) is not loaded and is not available for use.

This happens when people don't hook a process to a specific action, or if they don't know the typical order of actions and use a hook that fires before the function is loaded.

wp_mail() is a pluggable function, which means that plugins can alter it. Pluggable functions are loaded late in the process so that plugins can define them first.

Hooking a process that uses a pluggable function to something like the init action will help since all pluggable functions are loaded at that point. So your process should be something like the following:

add_action( 'init', 'my_hooked_function' );
function my_hooked_function() {
    // doing something here...
    wp_mail( $to, $subject, $message );
}

If the process you are doing is in your theme's functions.php but not hooked to an action, hooking to the appropriate action will solve your problem.

An alternative that you'll see mentioned but is less desirable is to simply make sure pluggable.php is included:

function my_not_hooked_function() {
    include_once( ABSPATH . '/wp-includes/pluggable.php' );
    // doing something here...
    wp_mail( $to, $subject, $message );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.