0
votes

I Have Problem With PHPMAILER it's Work Good in localhost but in server give me error

PHPMAILER Code

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

function sendMail($driver, $driverUser){

    // Load Composer's autoloader
    require 'vendor/autoload.php';

    // Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->SMTPDebug = 2;                                       // Enable verbose debug output
        $mail->isSMTP();                                            // Set mailer to use SMTP
        $mail->Host       = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = '[email protected]';                // SMTP username
        $mail->Password   = '*******';                      // SMTP password
        $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
        $mail->Port       = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('[email protected]', 'Wasta Driver');
        $mail->addAddress($driver, $driverUser);     // Add a recipient

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'New Order';
        $mail->Body    = ' مرحبا ' . '<strong>' . $driverUser . '</strong>' . '  لديك طلبيه جديده برجاء مراجعه برنامج الطيارين ';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }

}

Error

2019-04-30 05:14:51 SERVER -> CLIENT: 220-server.issgroups.org ESMTP Exim 4.91 #1 Tue, 30 Apr 2019 07:14:51 +0200 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2019-04-30 05:14:51 CLIENT -> SERVER: EHLO wastetkheer.com
2019-04-30 05:14:51 SERVER -> CLIENT: 250-server.issgroups.org Hello wastetkheer.com [138.201.107.252]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
2019-04-30 05:14:51 CLIENT -> SERVER: STARTTLS
2019-04-30 05:14:51 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host.
2019-04-30 05:14:51 CLIENT -> SERVER: QUIT
2019-04-30 05:14:51 
2019-04-30 05:14:51 
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

i'm trying change SMTPDebug To 1 & //$mail->isSMTP();

it's work good in local host not in server I guess the problem is with the SMTP authentication, but I couldn´t find the problem.

2
What server are you using? Some hosting companies blocks SMTP-ports and forces you to use their SMTP-relay (like GoDaddy). Also, if you get "Could not connect to SMTP host", it's most likely not the authentication (since that requires an established connection to the host).Magnus Eriksson

2 Answers

0
votes

Please search before posting as this has been answered many times before.

It's failing immediately after STARTTLS, indicating a TLS error. This is very common with gmail because their CA root certificates changed about a year ago to ones that are not present in many older OSs. It's working for you on localhost because your local OS does not have outdated CA certificates.

Read the troubleshooting guide which tells you exactly how to deal with this.

It could also be down to your ISP redirecting SMTP traffic to their own mail server, causing a certificate name mismatch - the guide provides ways of diagnosing the exact problem.

-1
votes

Try to replace $mail->SMTPSecure = 'tls'; to $mail->SMTPSecure = 'ssl';

//Because your server quit after this error: 2019-04-30 05:14:51 SERVER -> CLIENT: 220 TLS go ahead SMTP Error: Could not connect to SMTP host.

So try it