1

 <?php
        include('libs/PHPMailer-master/PHPMailerAutoload.php');
        //require 'PHPMailerAutoload.php';
        //require 'system/includes/PHPMailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;
        
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output
        
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = gethostbyname("hostName");  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        //$mail->Username = 'user@example.com';                 // SMTP username
        //$mail->Password = 'secret';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to
        
        $mail->setFrom(ex@com.lk, 'Mailer');
        $mail->addAddress('ex@com.lk', 'Joe User');     // Add a recipient
        //$mail->addAddress('ellen@example.com');               // Name is optional
        //$mail->addReplyTo('info@example.com', 'Information');
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');
        
        //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        $mail->isHTML(true);                                  // Set email format to HTML
        
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        
        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
        ?>

This is my code to send an email. But it gives me an error "Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting". Does anybody knows the reason for it?

sj111
  • 27
  • 8
  • You could find this answer helpful: https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server – Denis Solakovic Aug 10 '17 at 09:00
  • I see 2 possible reason : your hosting lock outgoing connexion on port 587 or your credential to for your SMTP is incorrect. Why `$mail->Username` and `$mail->password` are commented ? Have you tried to uncomment this 2 lines and enter your correct credentials ? If your SMTP provider doesn't need credential, you probably need to set `$mail->SMTPAuth` to `false` – Fabien Aug 10 '17 at 09:16
  • I have uncommented the two lines and set up the values. Still I am getting the same error. – sj111 Aug 10 '17 at 09:49

1 Answers1

4

Remove or comment out the line-

$mail->IsSMTP();

And it will work for you.

I have checked and experimented many answers from different sites but haven't got any solution except the above solution.