使用PHP Mailer发送电子邮件

I'm trying to send emails using PHP Mailer. The site is uploaded to a free hosting. The error is displayed about failed SMTP connection. I'm hoping to someone that has expertise in this matter.

Here goes my code.

    require '../extensions/phpmailer/PHPMailerAutoload.php';

        $emailAddress = 'SampleEmail';
        $password = 'Emailpassword';            
        $subject = 'Invitation to employee portal'; 
        $message = '                
            <!DOCTYPE html>
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <title>Invitation</title>
            <body style ="background:#eee;">
            <table cellpadding="0" cellspacing="0" border="0" id="backgroundTable" style ="padding: 40px; max-width:640px;background-color:#f3f3f3;display:block;margin:0 auto; font-family:Lato, sans-serif; color:#626262;font-size:13px;line-height:22px;" width ="600"> 
            <tbody>                                 
            <tr>                                        
            <td style ="background-color: #3552f2; padding: 34px 122px;">   
                <h1 width ="600" style ="max-width:600px;width:100%; text-align:center; color:#fff;">HRMS DHVTSU</h1>                                       
            </td>                                   
            </tr>                                   
            <tr>                                        
            <td style ="background-color: #ffffff; text-align: center; padding: 40px;">                                         
            <h1 style="font-size: 40px; color:#000; font-style: italic;">Sorry your account has been deactivated.</h1>                                          
            <br>                                            
            <p style ="color:#000; font-size: 16px;">For any questions please contact  HR Department.</p>                                           
            <br>                                            
            <br>                                        
            </td>                                   
            </tr>                                   
            <tr>                                        
            <td  style ="background-color: #3552f2; text-align: center; font-size: 16px; color: #fff;">                                         
            <p>© 2016. All rights reserved.</p>                                     
            </td>                                   
            </tr>                               
            </tbody>                            
            </table>                        
            </body>';               



        $mail = new PHPMailer;                      
        $mail->isSMTP();    
        // $mail->SMTPDebug = 2;
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );          
        $mail->Host = 'smtp.gmail.com';         
        $mail->Port = 587;          
        $mail->SMTPSecure = 'tls';          
        $mail->SMTPAuth = true;         
        $mail->Username = $emailAddress;            
        $mail->Password = $password;            
        $mail->setFrom('noreply@hrms.com', 'HRMS');         
        $mail->addReplyTo('johndoe@gmail.com', 'HRMS');         
        $mail->addAddress($email);          
        $mail->Subject = $subject;          
        $mail->msgHTML($message);                       


        if (!$mail->send()) {

            // $error = "Mailer Error: " . $mail->ErrorInfo;
            // echo '<script>alert("'.$error.'");</script>';
            return false;
        }else {
            $query = 'Update employee_has_credentials set status = '.self::STATUS_INACTIVE.' where id= '.$id.'';
            $result = $this->con->prepare($query);
            $result->execute(array($id));

            if($result->rowCount()){
                return true;
            }else{
                return false;
            }
        }

Possible duplicate of PHP Mailer issue use that code and it will fix your problem, because you have not added following code:

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php'

or go to above link more description you can see from that answer.