admin管理员组文章数量:1122832
When I use the wp_mail()
or mail()
function in AJAX, the function doesn't work and doesn't send email.
When I use these functions simply in my functions.php file it works fine.
It's not related to WordPress functions or actions. I have checked everything, including using SMTP mail. SMTP mail works same as mail()
function and it doesn't send in AJAX mode.
This is my functions.php file content:
add_action( 'phpmailer_init', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init( PHPMailer $phpmailer ) {
$phpmailer->Host = 'godadysmtpsampledomain.secureserver';
$phpmailer->Port = 465; // could be different
$phpmailer->Username = '[email protected]'; // if required
$phpmailer->Password = '12345'; // if required
$phpmailer->SMTPAuth = true; // if required
$phpmailer->SMTPSecure = 'ssl'; // enable if required, 'tls' is another possible value
$phpmailer->IsSMTP();
}
// This code works fine;
wp_mail("[email protected]", "test", "test");
But other codes or plugin in AJAX mode like Contact Form 7 don't work, it means the email doesn't delivered. I checked these plugins and wp_mail()
returns true but the email doesn't delivered.
When I use the wp_mail()
or mail()
function in AJAX, the function doesn't work and doesn't send email.
When I use these functions simply in my functions.php file it works fine.
It's not related to WordPress functions or actions. I have checked everything, including using SMTP mail. SMTP mail works same as mail()
function and it doesn't send in AJAX mode.
This is my functions.php file content:
add_action( 'phpmailer_init', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init( PHPMailer $phpmailer ) {
$phpmailer->Host = 'godadysmtpsampledomain.secureserver.net';
$phpmailer->Port = 465; // could be different
$phpmailer->Username = '[email protected]'; // if required
$phpmailer->Password = '12345'; // if required
$phpmailer->SMTPAuth = true; // if required
$phpmailer->SMTPSecure = 'ssl'; // enable if required, 'tls' is another possible value
$phpmailer->IsSMTP();
}
// This code works fine;
wp_mail("[email protected]", "test", "test");
But other codes or plugin in AJAX mode like Contact Form 7 don't work, it means the email doesn't delivered. I checked these plugins and wp_mail()
returns true but the email doesn't delivered.
- 2 Please provide minimal workable code. – Fayaz Commented Jan 28, 2017 at 13:57
4 Answers
Reset to default 0@T.Todua :
Ok and when I use your way it doesn't deliver the email. It doesn't send email in Ajax function and it's my problem exactly
add_action( 'wp_ajax_ABCABCABC', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init() {
//it doesn't send/deliver in a Ajax function
wp_mail("[email protected]", "test", "test");
}
But when I use wp_mail() in post-back page it works fine for example when put it in functions.php file it sends/delivers the email:
//functions.php
//It sends in this mode
wp_mail("[email protected]", "test", "test");
It's my problem.
Too late, but perhaps can help others. I don't recommend to change SMTP data within code, because:
- SMTP connection data is variable. If you work on localhost, probably you want to send test e-mails (eg using mailcacher). With this common scenario, you will have 2 SMTP data and can be confusing when sending files to the production server;
- Is not recommended to put passwords in code, for security reasons. If you work with many people in the project, it will share your password;
There are a good solution to change SMTP data. Is the lightweight plugin Easy WP SMTP. With this plugin, you set your SMTP data in Wordpress database.
As each environment has your own database (production server, dev 1, dev 2 etc), you can set the SMTP data in each WP Admin you use.
In production server, set the real SMTP. In development servers (localhost), you can set your test SMTP data.
Try adding a header:
$body = "<p>Body of email</p>";
$subject = "Email subject";
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( "User Email Here", $subject, $body, $headers);
what is this?
add_action( 'phpmailer_init', ...........
you should use:
add_action( 'wp_ajax_ABCABCABC', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init() { ....... }
and in JAVASCRIPT call, do like this :
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'action': 'ABCABCABC',
'whatever': 1234
};
jQuery.post(ajaxurl, data, function(response) {
//alert('Response got!! : ' + response);
});
});
</script>
(source).
本文标签: wp mailwpmail() function doesn39t send email in Ajax mode
版权声明:本文标题:wp mail - wp_mail() function doesn't send email in Ajax mode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291911a1928822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论