Title: What is wrong with this PHP function code? (Contact Form)
Last modified: December 13, 2022

---

# What is wrong with this PHP function code? (Contact Form)

 *  Resolved [kathys88](https://wordpress.org/support/users/kathys88/)
 * (@kathys88)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/what-is-wrong-with-this-php-function-code-contact-form/)
 * I’m still in the testing mode version of my website (it’s LIVE on GoDaddy’s servers-
   Using Linux cPanel Plan), and apparently I have a problem with the contact page,
   I try to send an email and I get this message:
 * There was an error while submitting the form. Please try again later. Mailer 
   Error: SMTP Error: Could not connect to SMTP host.
 * I talked to GoDaddy and they checked the server and everything is 100% fine, 
   The SMTP settings are correct too. The e-mail is fine too, it’s sending and recieving
   any other mails except via contact form.
 * I’m so sure from the code lines, I tried to change it code several times and 
   I didn’t find any error on the written lines, please I would appreciate it if
   you could tell me what I missed?
 * Attached PHP code:
 *     ```
       <?php
       // PHP Mailer
       use PHPMailer\PHPMailer\PHPMailer;
       use PHPMailer\PHPMailer\Exception;
   
               try
               {
                   require 'PHPMailer/Exception.php';
                   require 'PHPMailer/PHPMailer.php';
                   require 'PHPMailer/SMTP.php';
   
                   $mail = new PHPMailer(true);
   
                   //Server settings
                   //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                   // Enable verbose debug output
                   $mail->isSMTP();                                           // Send using SMTP
                   $mail->Host       = 'localhost';                    // Set the SMTP server to send through
                   $mail->SMTPAuth   = false;                                  // Enable SMTP authentication
                   $mail->Username   = 'myemail@here.com';          // SMTP username
                   $mail->Password   = 'myPasswordHere';                       // SMTP password
                   $mail->SMTPSecure = 'None';                                 // 'ssl' or 'tls' or Enable TLS encryption; <code>PHPMailer::ENCRYPTION_SMTPS</code> also accepted
                   $mail->Port       = 25;                                   // TCP port to connect to. For Example: 25 - Simple SMTP. 465 - SSL SMTP. 587 - TLS SMTP.
   
                   //From
                   $mail->setFrom('myemail@here.com', 'Mailer');    // Add your hosting account email or server admin email
                   //Recipient
                   $mail->addAddress('myemail@here.com', 'My Name');     // Add a recipient (your email). Add your name
                   //ReplyTo
                   $mail->addReplyTo($_POST['email'], $_POST['name']);        // Do not change this line
   
                   // Content
                   $mail->isHTML(true);                                       // Set email format to HTML
                   $mail->Subject = 'New message from contact form';          // Email subject. You can change this text
   
                   $fields = array('name' => 'Name', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
   
                   $emailText = nl2br("You have new message from Contact Form\n");
   
                   foreach ($_POST as $key => $value) {
                       if (isset($fields[$key])) {
                           $emailText .= nl2br("$fields[$key]: $value\n");
                       }
                   }
   
                   $mail->Body    = $emailText;
   
                   $mail->send();
                   $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!'; // You can change this text (message)
                   $responseArray = array('type' => 'success', 'message' => $okMessage);
               } catch (Exception $e) {
                   $errorMessage = "There was an error while submitting the form. Please try again later. Mailer Error: {$mail->ErrorInfo}"; // You can change this text (message)
                   $responseArray = array('type' => 'danger', 'message' => $errorMessage);
               }
   
               if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
                   $encoded = json_encode($responseArray);
   
                   header('Content-Type: application/json');
   
                   echo $encoded;
               }
   
   
               else {
                   echo $responseArray['message'];
               }
       ```
   

Viewing 1 replies (of 1 total)

 *  Plugin Support [Darshana](https://wordpress.org/support/users/darshanaw/)
 * (@darshanaw)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/what-is-wrong-with-this-php-function-code-contact-form/#post-16297728)
 * Hi [@kathys88](https://wordpress.org/support/users/kathys88/),
   Thanks for reaching
   out! I apologize, but your request is outside the scope of our [support](https://wordpress.org/support/topic/wp-mail-smtp-support-policy-3/)
   as your issue is not related to the WP Mail SMTP plugin.
 * If you’re using WordPress and the WP Mail SMTP plugin, you just need to call 
   the [WP_Mail() function](https://developer.wordpress.org/reference/functions/wp_mail/)
   to send an email using your custom code.
 * Thanks!

Viewing 1 replies (of 1 total)

The topic ‘What is wrong with this PHP function code? (Contact Form)’ is closed 
to new replies.

 * ![](https://ps.w.org/wp-mail-smtp/assets/icon-256x256.png?rev=1755440)
 * [WP Mail SMTP by WPForms - The Most Popular SMTP and Email Log Plugin](https://wordpress.org/plugins/wp-mail-smtp/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-mail-smtp/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-mail-smtp/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-mail-smtp/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-mail-smtp/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-mail-smtp/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Darshana](https://wordpress.org/support/users/darshanaw/)
 * Last activity: [3 years, 5 months ago](https://wordpress.org/support/topic/what-is-wrong-with-this-php-function-code-contact-form/#post-16297728)
 * Status: resolved