Otto Otto - 1 year ago 164
PHP Question

PHPMailer returns SMTP error with Ajax

I have a simple form set up with AJAX and PHP. This all works when I type the unix command

php send_nl.php
, but every time it returns a SMTP error from the ajax request.

Message could not be sent.Mailer Error: SMTP connect() failed.


$(document).ready(function() {
$('#risultato tbody').on( 'click', '.invianl', function (e) {

var jqxhr = $.ajax({
url: includes/api_send_nl.php,
type: "POST",
data: {}
}).done(function(json) {
}).fail(function(data) {
alert( "error" +data );

return false;


require "common.php" ;
require 'PHPMailer/PHPMailerAutoload.php';

$id = 2;

//function sendMail($id){
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->Port = 25;

//$mail->SMTPDebug = 1;

$stmt=$db->prepare("select * from newsletter_email where email_id='$id'");
if (!$stmt) {
if (!test_execute($stmt->execute())) die("ERRORE QUERY 2");

$mail->setFrom($newsletter['from_email'], $newsletter['from_name']);
$mail->addReplyTo($newsletter['replyto_email'], $newsletter['replyto_name']);


$mail->Subject = $newsletter['subject'];
$mail->Body = $newsletter['body'];
$mail->AltBody = 'Se non riesci a visualizzare la mail, clicca qui';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';

Answer Source

Settings $mail->SMTPDebug = 2 I discovered this issue:

PHPMailer SMTP -> ERROR: Failed to connect to server: Permission denied (13)

And I solved with:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download