admin管理员组文章数量:1433531
I want to call wp_mail()
from a non-template PHP file, but when I do, it fails and I don't understand why. For example, let's say I have a PHP file that consists of only this:
<?php
echo 'hi';
$mail_sent = wp_mail('[email protected]', 'subject', 'message');
echo $mail_sent;
?>
If I load that PHP file directly in my browser, only "hi" will print. If I remove echo 'hi';
altogether, nothing is printed, which indicates to me that wp_mail()
cannot be called in this particular context. However, if I add /* Template Name: SomeName */
to the file and create a new page using SomeName
as its template, wp_mail()
executes correctly. Clearly, I'm missing some key bit of information regarding how to correctly call wp_mail()
.
The reason I want to do this is because the theme I'm using came with a contact form that submits email via an ajax request roughly like this:
form.submit(function() {
$j.ajax({
type: 'GET',
url: ".php",
data: form.serialize(),
});
});
Currently, send_mail.php
calls the PHP mail()
function which "works" but is extremely flaky. I want it to call wp_mail()
so I can take advantage of this plugin which sends mail using a SMTP server. So my question is: how can I get wp_mail()
to work in this situation?
Thanks!
I want to call wp_mail()
from a non-template PHP file, but when I do, it fails and I don't understand why. For example, let's say I have a PHP file that consists of only this:
<?php
echo 'hi';
$mail_sent = wp_mail('[email protected]', 'subject', 'message');
echo $mail_sent;
?>
If I load that PHP file directly in my browser, only "hi" will print. If I remove echo 'hi';
altogether, nothing is printed, which indicates to me that wp_mail()
cannot be called in this particular context. However, if I add /* Template Name: SomeName */
to the file and create a new page using SomeName
as its template, wp_mail()
executes correctly. Clearly, I'm missing some key bit of information regarding how to correctly call wp_mail()
.
The reason I want to do this is because the theme I'm using came with a contact form that submits email via an ajax request roughly like this:
form.submit(function() {
$j.ajax({
type: 'GET',
url: "http://www.example/wp-content/themes/themeX/send_mail.php",
data: form.serialize(),
});
});
Currently, send_mail.php
calls the PHP mail()
function which "works" but is extremely flaky. I want it to call wp_mail()
so I can take advantage of this plugin which sends mail using a SMTP server. So my question is: how can I get wp_mail()
to work in this situation?
Thanks!
Share Improve this question edited Apr 8, 2019 at 20:41 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Mar 10, 2012 at 22:19 Nick SpreitzerNick Spreitzer 1131 silver badge7 bronze badges3 Answers
Reset to default 2The standalone script will not load WordPress, so it doesn't have a wp_mail()
function. WordPress has its own built-in Ajax handlers which you can leverage and have access to all WordPress functionality within those Ajax calls.
Alternatively, include the Wordpress functions in your standalone script as follows
require_once('wp-blog-header.php');
I find the WP ajax functions are not always the best way to do things, so writing your own AJAX handler is quite valid.
wp_mail is a Pluggable Function so you should be able to call wp-includes/pluggable.php and then have the ablilty to use wp_mail().
本文标签: wp mailNeed clarification on how to correctly call wpmail()
版权声明:本文标题:wp mail - Need clarification on how to correctly call wp_mail() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745613548a2666260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论