I’m trying to send emails through wordpress using wp_mail() function and it doesn’t seem to work.
It’s displaying 0
in Chrome ‘Network’ response
with
Status Code:200 OK
this my code part:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "info@company.com";
$host = "ssl://smtp.gmail.com:465";
$username = 'myEmail@company.pro';
$password = 'passpass';
$email_subject = "You have a new email from $email via company.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $email_subject, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
What part might be wrong?
I’ll post this as an official answer:
Two things you should check:
1) The response is 0 because you’re not returning anything. You should be returning a ajax response, usually json_encoding your request like this helps:
2) Not all wordpress/server configurations allow the default WordPress wp_mail function to send out mail. Certainly when testing on a local(host) server, lots of ISP’s block the default outgoing mail traffic (and in another question I’ve noticed you’re using SMTP mail-functions). Sometimes the blocking goes on silently, so it looks like everything works, but mail never gets sent. What helps is to install the WP Mail SMTP plugin (or similar) so you can configure outgoing SMTP servers (with or without authentication).