admin管理员组文章数量:1022949
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 ! :)
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 ! :)
Share Improve this question edited Apr 17, 2019 at 12:53 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Aug 19, 2016 at 18:41 PierrePierre 691 silver badge3 bronze badges 3- 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 Commented Aug 20, 2016 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 Rævan Commented Aug 22, 2016 at 17:41
- downvoted as without more debuging effort you are left with only guessing as to what the cause might be – Mark Kaplun Commented Jan 14, 2018 at 13:58
3 Answers
Reset to default 2Add 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';
$phpmailer->Port = 588;
$phpmailer->SMTPAuth = true;
$phpmailer->Username = '[email protected]';
$phpmailer->Password = 'Set Password';
$phpmailer->From = '[email protected]';
$phpmailer->FromName = 'Website Name';
$phpmailer->AuthType = 'LOGIN';
$phpmailer->SMTPAutoTLS = false;
$phpmailer->SMTPSecure ='';
}
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 ofmail()
and creates an options page to manage the settings.
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 );
}
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 ! :)
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 ! :)
Share Improve this question edited Apr 17, 2019 at 12:53 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Aug 19, 2016 at 18:41 PierrePierre 691 silver badge3 bronze badges 3- 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 Commented Aug 20, 2016 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 Rævan Commented Aug 22, 2016 at 17:41
- downvoted as without more debuging effort you are left with only guessing as to what the cause might be – Mark Kaplun Commented Jan 14, 2018 at 13:58
3 Answers
Reset to default 2Add 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';
$phpmailer->Port = 588;
$phpmailer->SMTPAuth = true;
$phpmailer->Username = '[email protected]';
$phpmailer->Password = 'Set Password';
$phpmailer->From = '[email protected]';
$phpmailer->FromName = 'Website Name';
$phpmailer->AuthType = 'LOGIN';
$phpmailer->SMTPAutoTLS = false;
$phpmailer->SMTPSecure ='';
}
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 ofmail()
and creates an options page to manage the settings.
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 );
}
本文标签: emailPHP mail() works but wpmail() does not
版权声明:本文标题:email - PHP mail() works but wp_mail() does not 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745580022a2157252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论