Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Can’t get wp_mail to work in ajax script

$
0
0

Replies: 0

Hi,

On my website I’m calling an ajax file after hitting the submit button of a form. I’m including the following to get access to the WordPress functions:

$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

I know it’s working since I’m working a lot with $wpdb. But I just can’t get wp_mail to work in this script. I enabled PHP error reporting and set WordPress in debug mode. But I don’t get any error messages in my ajax response. I also think that wp_mail is crashing the script because all the code that comes after the call of wp_mail is getting ignored. Here is the part in my code where I call the wp_mail function:

$header = "From: XYZ Company <" . CONTACT_MAIL . ">";

$anrede_construction = "Guten Tag,\r\n\r\n";
if(strpos(strtolower($anrede), 'herr') !== false && strlen($nachname) != 0)
{
	$anrede_construction = "Sehr geehrter Herr " . htmlspecialchars($nachname) . ",\r\n\r\n";
}
else if(strpos(strtolower($anrede), 'frau') !== false && strlen($nachname) != 0)
{
	$anrede_construction = "Sehr geehrte Frau " . htmlspecialchars($nachname) . ",\r\n\r\n";
}

$message = $anrede_construction . "Sie haben sich erfolgreich für unseren Newsletter angemeldet.";
$message .= "Bestätigen Sie den Erhalt des Newsletters bitte über folgende URL:\r\n\r\n";
$message .= HOME_URL . "/newsletter-activate/?token=" . $token . "\r\n\r\n\r\n";
$message .= "XYZ Company";

echo $email . ", " . "Newsletter, " . $message . ", " . $header; // only for debug purpose
wp_mail($email, "Newsletter", $message, $header);

I tried it with the WP SMTP Plugin and without. Both didn’t work. If I send the test mail of the SMTP Plugin, the mail works. Contact Form 7 is also working. Only my custom wp_mail call doesn’t work.

Do you know what can cause this behaviour?

Thank you in advance.

Best Regards


Viewing all articles
Browse latest Browse all 59525

Trending Articles