Why error although it was done?

Why error although it was done?

Hi,

I am using below to post a form to php in order to send email to the user.

  1. $.ajax(
  2. {
  3.     type: "POST",
  4.     url: 'https://www.softnames.com/Nabeel_Al_Hamer/send_message.php',
  5.     data: formData,
  6.     processData: false,
  7.     contentType: false,
  8. }).done(function ()
  9. {
  10.     // done message
  11. }).fail(function (xhr, status, error)
  12. {
  13.     console.log(xhr.statusText);
  14.     console.log(status);
  15.     console.log(error);
  16. })

why I keep getting error although was successfully sent? and all I get in the message is the word error. How can I get what's the exact error and how can I fix?

This is my PHP:

  1. <?php
  2.     if (isset($_SERVER['HTTP_ORIGIN']))
  3.     {
  4.         header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
  5.         header('Access-Control-Allow-Credentials: true');
  6.         header('Access-Control-Max-Age: 86400');    // cache for 1 day
  7.     }

  8.     include('../PHPMailer/class.phpmailer.php');
  9.     
  10.     $sender_name = $_POST["sender_name"];
  11.     $sender_email = $_POST["sender_email"];
  12.     $sender_telephone = $_POST["sender_telephone"];
  13.     $message_title = $_POST["message_title"];
  14.     $message_text = $_POST["message_text"];

  15.     $unique_id = uniqid();
  16.     
  17. // send email to sender
  18. $body = "<html dir='rtl' lang='ar'>";
  19.     $body .= "<body>";
  20.     $body .= "<head>";
  21.     $body .= "<meta charset='utf-8'>";
  22.     $body .= "</head>";
  23. $body .= "<b>هذه الرسالة الالكترونية تم إرسالها بواسطة الرد التلقائي لذا يرجى عدم الرد.</b><hr><br><br>";
  24. // $body .= "Dear " . $customer_title. " " . $customer_first_name . ",<br><br>";
  25.     $body .= "لكم جزيل الشكر.<br><br>";
  26.     $body .= "تم استلام رسالتكم وسنقوم بالرد عليها في أقرب وقت.<br><br>";
  27.     $body .= "مع تحياتي,<br>";
  28.     $body .= "نبيل يعقوب الحمر";
  29. $body .= "<br><br>";
  30. // $body .= "<a href='https://www.aromabh.com/' target='_blank'>www.aromabh.com</a>";
  31. $body .= "</html>";
  32.     $body .= "</body>";
  33. $body = nl2br($body);

  34. //Create a new PHPMailer instance
  35. $mail = new PHPMailer();
  36. //Tell PHPMailer to use SMTP
  37. $mail->IsSMTP();
  38. //Enable SMTP debugging
  39. // 0 = off (for production use)
  40. // 1 = client messages
  41. // 2 = client and server messages
  42. $mail->SMTPDebug  = 2;
  43. //Ask for HTML-friendly debug output
  44. $mail->Debugoutput = 'html';
  45. //Set the hostname of the mail server
  46. $mail->Host       = "mail.myhost.com";
  47. //Set the SMTP port number - likely to be 25, 465 or 587
  48. $mail->Port       = 587;
  49. //Whether to use SMTP authentication
  50. $mail->SMTPAuth   = true;
  51. //Username to use for SMTP authentication
  52. $mail->Username   = "noreply@mydomain.com";
  53. //Password to use for SMTP authentication
  54. $mail->Password   = "xxxxxxxx";
  55. //Set who the message is to be sent from
  56. $mail->SetFrom("noreply@mydomain.com", "نبيل يعقوب الحمر");
  57. //Set who the message is to be sent to
  58. $mail->AddAddress($sender_email, $sender_name);

  59. //Replace the plain text body with one created manually
  60. $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  61. //Attach an image file
  62. // $mail->AddAttachment('images/phpmailer_mini.gif');
  63. // $mail->AddCustomHeader($mail_header); 

  64. //Set the subject line
  65. $mail->Subject = "شكرا " . " " . $sender_name . " :)";

  66. //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
  67. // $mail->MsgHTML($body);
  68. $mail->Body = $body;
  69. $mail->CharSet = 'UTF-8';
  70. $mail->IsHTML(true);

  71. //Send the message, check for errors
  72. if(!$mail->Send()) {
  73. echo "Mailer Error: " . $mail->ErrorInfo;
  74. } else {
  75. echo "Message sent!";
  76. }

  77.     // send email to management
  78. $body = "<html dir='rtl' lang='ar'>";
  79.     $body .= "<body>";
  80.     $body .= "<head>";
  81.     $body .= "<meta charset='utf-8'>";
  82.     $body .= "</head>";
  83.     $body .= "<b>هذه الرسالة الالكترونية تم إرسالها بواسطة الرد التلقائي لذا يرجى عدم الرد.</b><hr><br><br>";
  84. $body .= "Dear Mesk Staff,<br><br>";
  85.     $body .= "<br><br><br>";
  86.     $body .= "Best Regards,";
  87. $body .= "<br>";
  88. $body .= "Mesk Holdings";
  89. $body .= "<br><br>";
  90. $body .= "<a href='https://www.meskholdings.com/' target='_blank'>www.meskholdings.com</a>";
  91. $body .= "</html>";
  92.     $body .= "</body>";
  93. $body = nl2br($body);

  94. //Create a new PHPMailer instance
  95. $mail = new PHPMailer();
  96. //Tell PHPMailer to use SMTP
  97. $mail->IsSMTP();
  98. //Enable SMTP debugging
  99. // 0 = off (for production use)
  100. // 1 = client messages
  101. // 2 = client and server messages
  102. $mail->SMTPDebug  = 2;
  103. //Ask for HTML-friendly debug output
  104. $mail->Debugoutput = 'html';
  105. //Set the hostname of the mail server
  106. $mail->Host       = "mail.myhost.com";
  107. //Set the SMTP port number - likely to be 25, 465 or 587
  108. $mail->Port       = 587;
  109. //Whether to use SMTP authentication
  110. $mail->SMTPAuth   = true;
  111. //Username to use for SMTP authentication
  112. $mail->Username   = "noreply-mailer@mydomain.com";
  113. //Password to use for SMTP authentication
  114. $mail->Password   = "xxxxxxxx";
  115. //Set who the message is to be sent from
  116. $mail->SetFrom("noreply@mydomain.com", $sender_name);
  117. // display the from name and email;
  118. // $mail->FromName = $_POST['txtName'] ." <" . $_POST['txtEmail'] . ">";
  119. //Set an alternative reply-to address
  120. $mail->AddReplyTo($sender_email, $sender_name);
  121. //Set who the message is to be sent to
  122. $mail->AddAddress("email@domain.com", "Jassim Rahma");

  123. //Replace the plain text body with one created manually
  124. $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  125. //Attach an image file
  126. // $mail->AddAttachment('images/phpmailer_mini.gif');
  127. // $mail->AddCustomHeader($mail_header); 

  128. //Set the subject line
  129. $mail->Subject = $message_title;

  130. //Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
  131. // $mail->MsgHTML($body);
  132. $mail->Body = $body;
  133. $mail->CharSet = 'UTF-8';
  134. $mail->IsHTML(true);

  135. //Send the message, check for errors
  136. if(!$mail->Send()) {
  137. echo "Mailer Error: " . $mail->ErrorInfo;
  138. } else {
  139. echo "Message sent!";
  140. }
  141. ?>