I have a file, mail.php – it’s in the public_html/scripts/
directory.
I have an ajax javascript function that calls it:
<script>
function sendEmail() {
var variable1 = 'hello'; // Get the value from the input box
var variable2 = 'world'; // Get the value from the input box
var xhr = new XMLHttpRequest();
xhr.open('POST', '/scripts/mail.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// Request completed successfully
console.log('Email sent successfully!');
}
};
xhr.send('variable1=' + encodeURIComponent(variable1) + '&variable2=' + encodeURIComponent(variable2));
}
sendEmail();
</script>
The file mail.php
has the following:
<?php
require_once('/wp-load.php'); // Load the WordPress core file to access WordPress functions
When looking in Developer Tools (Chrome), the Response to mail.php in the Network tab is:
Fatal error: require_once(): Failed opening required
‘/wp-load.php’ (include_path=’.:/usr/local/php74/pear’) in
/home/customer/www/website.com/public_html/scripts/mail.php on
line 2
I’ve tried with just: require_once(‘../wp-load.php’) and require_once(‘../../wp-load.php’)
But the error remains.
Is this a permissions issue (should my scripts directory be in wp-content for example), or is there anything else obvious wrong with my code?
Thanks, Mark
2
Answers
I was using the wp_smtp_mail plugin, and referencing that in my PHP - but the error was showing as not being able to load wp-load.php
When I send using wp_mail (default WP mail function), it works without the error.
It seems the error was not with wp-load.php, although that's where developer tools network tab, suggested it was.
So, not using the plugin, and using the default mail, has solved this problem, if not the reason for the error.
Try to add a dot at the beginning of the file name:
And check if the wp-load.php is in the same folder of your script.