Quantcast
Channel: WordPress.org Forums » [WP Mail SMTP by WPForms - The Most Popular SMTP and Email Log Plugin] Support
Viewing all articles
Browse latest Browse all 5028

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

$
0
0

Replies: 0

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 all articles
Browse latest Browse all 5028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>