0
votes

Following error that I am getting while sending an email

2020-10-29 15:34:02 Connection: opening to localhost:587, timeout=300, options=array () 2020-10-29 15:34:04 Connection failed. Error #2: stream_socket_client(): unable to connect to localhost:587 (No connection could be made because the target machine actively refused it.) [C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.smtp.php line 298] 2020-10-29 15:34:04 SMTP ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it. (10061) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Fatal error: Uncaught phpmailerException: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php:1558 Stack trace: #0 C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php(1340): PHPMailer->smtpSend('Date: Thu, 29 O...', 'This is a multi...') #1 C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php(1215): PHPMailer->postSend() #2 C:\xampp\htdocs\Hospital\adminpanel\table\apt status.php(56): PHPMailer->send() #3 {main} thrown in C:\xampp\htdocs\Hospital\adminpanel\phpmailer\class.phpmailer.php on line 1558

Following is the php codes that I tried

<?php

include '../auth/dbconnection.php';
require ('../phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer(TRUE);

 //Server settings
 $mail->isSMTP();                             // Set mailer to use SMTP
 $mail->Host       = 'localhost';             // Set the SMTP server to send through
 $mail->SMTPAuth   = true;                    // Enable SMTP authentication
 $mail->SMTPDebug = 4; 
 $mail->Username   = '[email protected]';     // SMTP false username
 $mail->Password   = '';                      // SMTP false password
 $mail->SMTPSecure = 'tsl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
 $mail->Port       = 587;                // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

 
 $stmt1 = $conn->prepare("SELECT * FROM patients WHERE p_id=?");
 $stmt1->bind_param("s", $_POST['p_id']);
 $stmt1->execute();
 $result = $stmt1->get_result();
  if(mysqli_num_rows($result)>0){
    while($row = $result->fetch_assoc()) {
     $username=$row['username'];
     $email=$row['email'];
     
       //Recipients
   $mail->setFrom('[email protected]', 'Mailer');
   $mail->addAddress($email);     // Add a recipient
   $mail->addReplyTo('[email protected]');

    }
}

if (isset($_POST['data_id'])) {

  $id=$_POST['data_id'];
  $date=$_POST['date'];

   $stmt = $conn->prepare("UPDATE appointment SET admin_status = ?,patient_status = ? WHERE apt_id= ?");
   $admin_status='1';
   $patient_status='1';

   $stmt->bind_param("ssi", $admin_status,$patient_status, $id);
   $status = $stmt->execute();

   if ($status === true) {

    // Content
    $mail->isHTML(true);     // Set email format to HTML
    $mail->Subject = "Regarding the Appointment Request";
    $mail->Body    = "<h3 align=right> Your Appointment requested on'.$date. 'has <b> approved.</b> <br> Please refer the patient schedule for time arrivals. <br> Best Regards from </b> <b><i>  SEUS Hospitals </i> </b>";

    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    $mail->send();

    echo "Records was updated successfully."; 

} else {
    echo 'cant'; 
}

$conn->close(); 
}
?>

I don't know where I went wrong

1

1 Answers

1
votes

Several straightforward errors.

Firstly you're using a very old version of PHPMailer. Upgrade, and ideally switch to using composer.

One simple typo: $mail->SMTPSecure = 'tsl'; should be $mail->SMTPSecure = 'tls';.

Next is a bit more conceptual - you can't get a valid certificate (signed by a public CA) for localhost, so you'll always have trouble trying to do that. There's no benefit in using an encrypted connection to localhost anyway because nothing travels over a network. It's also likely that you don't need authentication when submitting to localhost.

I'd recommend changing all this:

 $mail->isSMTP();                             // Set mailer to use SMTP
 $mail->Host       = 'localhost';             // Set the SMTP server to send through
 $mail->SMTPAuth   = true;                    // Enable SMTP authentication
 $mail->SMTPDebug = 4; 
 $mail->Username   = '[email protected]';     // SMTP false username
 $mail->Password   = '';                      // SMTP false password
 $mail->SMTPSecure = 'tsl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
 $mail->Port       = 587;                // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

to just this, because everything else will work with defaults:

$mail->isSMTP();