Jump to content

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


KathyS88

Recommended Posts

I'm still in the testing mode version of my website (it's LIVE on GoDaddy's servers), 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.

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; `PHPMailer::ENCRYPTION_SMTPS` 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'];
        }
Edited by KathyS88
Link to comment
Share on other sites

The error you are getting is saying no smtp connection could be made to localhost.  Does your server have an mta running (sendmail or postfix)?  Does your server have any sort of firewall running that might be blocking port 25?  Email configuration for a domain and server is a highly complicated endeavor that involves many different moving parts.  It also depends a lot on the type of hosting you have.    When you state that "it's sending and receiving other emails" I don't know what that means exactly in your case.  

There are many things this could be, all of which require a fair degree of system administration skills, and an understanding of your hosting.  One possible issue is that "localhost" is not being resolved by your server to the loopback address.  About the only suggestion I could make, pretty much as a hail mary attempt, would be to change your script to use '127.0.0.1' instead of 'localhost'.  Assuming this is a linux based server, you can check the contents of the /etc/hosts file to see if there is a proper entry mapping localhost to that ip.

For mail to have any degree of deliverability direct from a domain, requires  lot of things to be setup correctly.  This site is an excellent aid in testing and diagnosing deliverability:  https://www.mail-tester.com/ 

If GoDaddy is acting as your MTA, then I would expect that your scripts would be delivering mail to a godaddy mail server, and not to localhost at all, so again, email is complicated, and we really don't have enough information to provide more than a couple of educated guesses as to what your issue is.

Link to comment
Share on other sites

12 hours ago, gizmola said:

The error you are getting is saying no smtp connection could be made to localhost.  Does your server have an mta running (sendmail or postfix)?  Does your server have any sort of firewall running that might be blocking port 25?  Email configuration for a domain and server is a highly complicated endeavor that involves many different moving parts.  It also depends a lot on the type of hosting you have.    When you state that "it's sending and receiving other emails" I don't know what that means exactly in your case.  

There are many things this could be, all of which require a fair degree of system administration skills, and an understanding of your hosting.  One possible issue is that "localhost" is not being resolved by your server to the loopback address.  About the only suggestion I could make, pretty much as a hail mary attempt, would be to change your script to use '127.0.0.1' instead of 'localhost'.  Assuming this is a linux based server, you can check the contents of the /etc/hosts file to see if there is a proper entry mapping localhost to that ip.

For mail to have any degree of deliverability direct from a domain, requires  lot of things to be setup correctly.  This site is an excellent aid in testing and diagnosing deliverability:  https://www.mail-tester.com/ 

If GoDaddy is acting as your MTA, then I would expect that your scripts would be delivering mail to a godaddy mail server, and not to localhost at all, so again, email is complicated, and we really don't have enough information to provide more than a couple of educated guesses as to what your issue is.

 

Thanks for your explaination and support :)

I'm so sure about all the SMTP configuration settings, Because this is what Godaddy's agents sent me. 

I did a mail-tester and got total: 6.9/10 and this is what I've got. is this helpful to get resolve the problem? 

 

 

 

Tester.png

Link to comment
Share on other sites

the following is the only documentation that i found - https://www.godaddy.com/help/send-form-mail-using-an-smtp-relay-server-953

so, first question, which type of hosting do you have, since it determines what hostname you use?

next, you can get the phpmailer to use php's mail() by calling the isMail() method, instead of the isSMTP() method. if using the isMail() method, you would not use any of the settings. if using the isSMTP() method, i would not use the username and password settings.

Link to comment
Share on other sites

So according to Mac's link, as I surmised, you can't relay mail directly from your server via it's mta.

Port: 25
SMTP Authentication: False or none
SSL or Secure Connection: None
Server or Host: The relay server you need to use depends on the type of hosting and script you use.

This is apparently why your script doesn't work.  It is trying to send mail to your local mta via the smtp protocol.  There should be instructions on what the relay server is for your email domain.  It is not going to be localhost, but rather some domain name they specify.  Changing the script to use the right server should allow it to work.

 

While this has nothing to do with your contact script, as far as the other issues with deliverability, one main issue from the report you provided is that DKIM is not being used.  There is nothing you can do about that, as DKIM has to be implemented by the email administrator.  I don't know if there is any possibility of having it supported by GoDaddy hosting or not, as it is non-trivial.  

If you want to know more about it, this article does a good job of explaining the basics so you have a frame of reference:  https://www.uriports.com/blog/introduction-to-spf-dkim-and-dmarc/

I don't know the purpose of your server or what types of emails you intend to send, but they will not have a high deliverability score, and thus will likely be spam filtered by some email systems.   For the purposes of a contact page, that is not a concern, as you can whitelist your address, but if you will be sending regular/automated emails from your domain, you may encounter problems with other people receiving them.

Candidly, Godaddy is not a well regarded hosting company, although many people do use it.   Hopefully at very least you are not using shared hosting.

Link to comment
Share on other sites

16 hours ago, mac_gyver said:

the following is the only documentation that i found - https://www.godaddy.com/help/send-form-mail-using-an-smtp-relay-server-953

so, first question, which type of hosting do you have, since it determines what hostname you use?

next, you can get the phpmailer to use php's mail() by calling the isMail() method, instead of the isSMTP() method. if using the isMail() method, you would not use any of the settings. if using the isSMTP() method, i would not use the username and password settings.

I'm using Linux (cPanel) plan.

can you help me with the code? I'm newbie on PHP

Edited by KathyS88
Link to comment
Share on other sites

12 hours ago, gizmola said:

So according to Mac's link, as I surmised, you can't relay mail directly from your server via it's mta.

Port: 25
SMTP Authentication: False or none
SSL or Secure Connection: None
Server or Host: The relay server you need to use depends on the type of hosting and script you use.

This is apparently why your script doesn't work.  It is trying to send mail to your local mta via the smtp protocol.  There should be instructions on what the relay server is for your email domain.  It is not going to be localhost, but rather some domain name they specify.  Changing the script to use the right server should allow it to work.

 

While this has nothing to do with your contact script, as far as the other issues with deliverability, one main issue from the report you provided is that DKIM is not being used.  There is nothing you can do about that, as DKIM has to be implemented by the email administrator.  I don't know if there is any possibility of having it supported by GoDaddy hosting or not, as it is non-trivial.  

If you want to know more about it, this article does a good job of explaining the basics so you have a frame of reference:  https://www.uriports.com/blog/introduction-to-spf-dkim-and-dmarc/

I don't know the purpose of your server or what types of emails you intend to send, but they will not have a high deliverability score, and thus will likely be spam filtered by some email systems.   For the purposes of a contact page, that is not a concern, as you can whitelist your address, but if you will be sending regular/automated emails from your domain, you may encounter problems with other people receiving them.

Candidly, Godaddy is not a well regarded hosting company, although many people do use it.   Hopefully at very least you are not using shared hosting.

How do I whitelist my email?

Link to comment
Share on other sites

These are the parameters you provided in the script:

 

            $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; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            $mail->Port       = 25;                                   // TCP port to connect to. For Example: 25 - Simple SMTP. 465 - SSL SMTP. 587 - TLS SMTP.

 

Based on information you get from GoDaddy,  $mail->Host should be one of their hostnames.  You either need a user/password or you don't.  This is information you need to get from GoDaddy.

Let's assume that the mail relay server for your hosting plan is 'mail3.godaddy.com'.  Then you would need to change this line of code in the script:  

$mail->Host       = 'localhost';

To whatever the actual host is.  In the example I provided, then that line needs to be changed to be:

$mail->Host       = 'mail3.godaddy.com'; 

 This is not an actual godaddy host, so don't try and use that.  You need to get the correct mail server host name from your hosting information provided to you by godaddy.

 

As for whitelisting, that depends on the email client you use.  For example, I used gmail for most things, so a search on "whitelist {your email client}" should provide you some documentation or articles showing you how to do it.   For gmail that would be an article like this one:  https://www.whitelist.guide/gmail/

 

Link to comment
Share on other sites

4 hours ago, gizmola said:

These are the parameters you provided in the script:

 

            $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; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            $mail->Port       = 25;                                   // TCP port to connect to. For Example: 25 - Simple SMTP. 465 - SSL SMTP. 587 - TLS SMTP.

 

Based on information you get from GoDaddy,  $mail->Host should be one of their hostnames.  You either need a user/password or you don't.  This is information you need to get from GoDaddy.

Let's assume that the mail relay server for your hosting plan is 'mail3.godaddy.com'.  Then you would need to change this line of code in the script:  

$mail->Host       = 'localhost';

To whatever the actual host is.  In the example I provided, then that line needs to be changed to be:

$mail->Host       = 'mail3.godaddy.com'; 

 This is not an actual godaddy host, so don't try and use that.  You need to get the correct mail server host name from your hosting information provided to you by godaddy.

 

As for whitelisting, that depends on the email client you use.  For example, I used gmail for most things, so a search on "whitelist {your email client}" should provide you some documentation or articles showing you how to do it.   For gmail that would be an article like this one:  https://www.whitelist.guide/gmail/

 

According to the host you can see here in the attachment and link that it should be localhost, I’ve contacted them about this and they said the same (localhost), I was confused too. 
 

here: Link .

 

I also have tried 

Outgoing server and ports

Outgoing server (SMTP):smtpout.secureserver.net

SSL Port: 465 or 587

 

 

and Unfortunately it didn’t work too.

 

 

97A8FCA7-0DB2-4D97-849A-6136EB208753.jpeg

Edited by KathyS88
Link to comment
Share on other sites

  • 3 weeks later...
On 12/12/2022 at 10:25 PM, gizmola said:

So according to Mac's link, as I surmised, you can't relay mail directly from your server via it's mta.

Port: 25
SMTP Authentication: False or none
SSL or Secure Connection: None
Server or Host: The relay server you need to use depends on the type of hosting and script you use.

This is apparently why your script doesn't work.  It is trying to send mail to your local mta via the smtp protocol.  There should be instructions on what the relay server is for your email domain.  It is not going to be localhost, but rather some domain name they specify.  Changing the script to use the right server should allow it to work.

 

While this has nothing to do with your contact script, as far as the other issues with deliverability, one main issue from the report you provided is that DKIM is not being used.  There is nothing you can do about that, as DKIM has to be implemented by the email administrator.  I don't know if there is any possibility of having it supported by GoDaddy hosting or not, as it is non-trivial.  

If you want to know more about it, this article does a good job of explaining the basics so you have a frame of reference:  https://www.uriports.com/blog/introduction-to-spf-dkim-and-dmarc/

I don't know the purpose of your server or what types of emails you intend to send, but they will not have a high deliverability score, and thus will likely be spam filtered by some email systems.   For the purposes of a contact page, that is not a concern, as you can whitelist your address, but if you will be sending regular/automated emails from your domain, you may encounter problems with other people receiving them.

Candidly, Godaddy is not a well regarded hosting company, although many people do use it.   Hopefully at very least you are not using shared hosting.

Would you please tell me what is the best/recommended hosting  ? :)

I live in Israel if this change anything :)

 

Link to comment
Share on other sites

  • 3 weeks later...
On 12/29/2022 at 6:47 AM, KathyS88 said:

Would you please tell me what is the best/recommended hosting  ? :)

I live in Israel if this change anything :)

 

My experience is with larger Hosting companies, using VPS servers.   What is right for you depends a lot on what you are doing with your server.   What does the server do for you?  Is it a business, and if so what market(s) does it serve?  The location of the server is important to any decision you make.

I have experience with all 4 of these services and can recommend them for someone who is not based in the US.

  1. Amazon Web Services (AWS)
  2. Microsoft Azure
  3. Linode
  4. Vultr

All of these services have a presence that should be suitable for you in Israel, although you might need to experiment.  With a VPS provider, you can easily create VPS server, and experiment with it, and shut it down if you don't feel it's providing the performance you want, given your location.

AWS in particular is soon to open a Tel Aviv "Region" which means one or more data centers located in that city.  

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.