0

So I am trying to send mails through PHP, I wrote the code and it works perfectly on local host, however its giving problems on CPanel on deployment. Is there something I am supposed to do seperately on CPanel. Am I missing something? All the necassary files seem to be there on the Cpanel too

Error:

Message could not be sent.Mailer Error: SMTP connect() failed.
<?php 
    require "PHPMailerAutoload.php";
    require "connect.php";
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->SMTPDebug = false;     
    $mail->Username = 
    $mail->Password = 
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->From = 
    $mail->FromName = 

   if($_POST['type'] == 'Free_Class' ){
            //   echo !extension_loaded('openssl')?"Not Available":"Available";
              $Body ="this is mail from abc for <b>Schedule a Free Class</b><br/>";
              $Body .= "Name : ".$_POST['name']."<br/>";
              $Body .= "Email : ".$_POST['email']."<br/>";
              $Body .= "Mobile : ".$_POST['number']."<br/>";
              if(!empty($_POST['course'])){
                  $Body .= "Course : ".$_POST['course']."<br/>";
              }
              $subject_text = "Schedule a Free Class"; 
            
             
        }
        echo $_POST['email'];
        echo $_POST['name'];
        $mail->AddAddress($_POST['email'],$_POST['name']);  // Add a recipient
        $mail->isHTML(true);    // Set email format to HTML
        $mail->Subject = $subject_text;
        $mail->Body   = $Body;

        if (!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            exit;
        }
        else{
            $name = $_POST['name'];
            $email = $_POST['email'];
            $number = $_POST['number'];
            $course = $_POST['course'];
            $sql = "INSERT INTO demo_class_registrations (name, email, number, course) 
                VALUES ('$name', '$email', '$number', '$course')";
            echo "<script>alert('your response has been recorded. one of our executive contact you soon !!')
            window.location.href = '$base_url';
            </script>";
        }
?>
Milanor
  • 51
  • 5
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – meewog Feb 24 '22 at 16:04
  • Ensure the OpenSSL extension is enabled `extension=openssl` in your php.ini configuration on the production server. Use `phpinfo()` to verify. Otherwise try using `$mail->SMTPSecure = 'tls'; $mail->Port = 587;` Others also indicate commenting out `$mail->IsSMTP();` resolved the issue. – Will B. Feb 24 '22 at 16:12
  • 2
    And ensure your hosting provider doesn't block outgoing SMTP connections. – ADyson Feb 24 '22 at 16:26
  • @WillB. it should be noted that commenting out the `IsSMTP` option will cause PHPMailer to try and send via the local sendmail instance on the server where PHP is running. That may or may not be desirable for the OP - it may have implications that aren't helpful for them, and/or it may not work for other reasons, and/or the hosting environment may not offer that. I can't comment either way, but they should be aware of what the option actually does, rather than just being given a simple advice to disable it without any explanation :-) – ADyson Feb 24 '22 at 16:28
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. https://phpdelusions.net/mysqli also contains good exampl – ADyson Feb 24 '22 at 16:29
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use it again. – ADyson Feb 24 '22 at 16:29
  • 1
    If you have SMTP issues, change `$mail->SMTPDebug = false;` to `$mail->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER` to get more detailed info about the actual issue. You can also pass `true` to the constructor: `new PHPMailer(true)` to make it throw exceptions. – M. Eriksson Feb 24 '22 at 16:42
  • 1
    Please also see the [Troubleshooting Documentation for PHPMailer](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#smtp-error-could-not-connect-to-smtp-host) for additional details and potential resolutions/explanations. – Will B. Feb 24 '22 at 16:45

0 Answers0