Search code examples
phpphpmailer

SMTP ERROR: Failed to connect to server: Permission denied (13) SMTP connect() failed


I don't undestand why there is the error, maybe i am doing something wrong with the config, maybe with the host. on localhost this config is working, on my online server, with certificate ssl i have this problem

    <?php

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

require 'vendor/autoload.php';

$mail = new PHPMailer();

$mail->SMTPDebug = 1;
$mail->isSMTP();
$mail->Host = 'smtps.aruba.it';
$mail->SMTPAuth = true;
$mail->Username = 'mail@domain.com';
$mail->Password = '********';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$smtp_debug = true;

// genero il codice otp
$randOtp = strval(rand(10000, 99999));

$sender = "mail@domain.com";
$receiver = $_SESSION['receiver'];
$subject = "Codice di sicurezza OTP";
$mailBody = $randOtp . " Questo e' il tuo codice di sicurezza!";


$mail->setFrom($sender);
$mail->addAddress($receiver);
$mail->addReplyTo($sender);

$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $mailBody;


if (!$mail->send()) {
    echo "Errore nell invio dell email";
} else {
    echo "Email inviata con successo";
    // echo '<pre>';
    // var_dump($mail);
    // echo '</pre>';
    // die();
}

Solution

  • Maybe you online server has selinux and it's blocking the communication?

    You can check it with the command:

    getsebool httpd_can_sendmail
    

    And if you need it, enable with:

    setsebool -P httpd_can_sendmail 1
    

    More info:

    https://blog.innovsystems.com/php/smtp-error-failed-to-connect-to-server-permission-denied-13/