Domenico Schitti
Domenico Schitti

Reputation: 15

PHPmailer unespected HTTP ERROR 500, syntax seems ok

I have to send a mail to more users and I'm using a for loop, everything seems to work fine until i put this code under my loop, and I got HTTP ERROR 500 but I dont know why.

if (isset($riga_inscad[$y])) {
//PHPMAILER: inizio variabili da modificare:
$server_smtp = 'xxx';
$username_smtp= 'xxx';
$password_smtp ='xxx';
$indirizzo_mittente = '[email protected]';
$descrizione_mittente = 'xxxx';
$indirizzo_destinatario = 'thevar';
//fine variabili da modificare

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

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

        $mail = new PHPMailer(true); 

        $mail->IsSMTP();
        $mail->SMTPOptions = array(
          'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
         )
        );
        $mail->SMTPAuth   = true;
        $mail->Host       = $server_smtp;
        $mail->Port       = 587;
        $mail->SMTPSecure = "tls"; 
        $mail->Username   = $username_smtp;
        $mail->Password   = $password_smtp;
        
        $mail->setFrom($indirizzo_mittente, $descrizione_mittente);
        $mail->addAddress($indirizzo_destinatario);

        $mail->Subject = "Scadenze SEA – Riepilogo mensile";
        $mail->Body    = '';
        $mail->IsHTML(true);
        if (!$mail->send())
            echo $mail->ErrorInfo;
        else
            echo "ok!";
 $mail->clearAllRecipients();

}

I notice that if I delete those rows:

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

The HTTP ERROR 500 goes away, but I end the for loop. Can you tell me whats wrong pls? (Im not able to see the logs sry)

Upvotes: 1

Views: 204

Answers (1)

Synchro
Synchro

Reputation: 37810

Put use statements at the top of your file. They are not really like normal code, more like compile-time directives. If you're using a really old version of PHP, it may be erroring on the use statements themselves – you should be running PHP 7.4 these days, 8.0 soon.

If you're running this code from inside a loop, then the fatal error is quite straightforward – if you call require to load the same class file more than once, you'll get a fatal error the second time.

You're enabling exceptions by passing true to the constructor, but you have no try/catch block, so you may get a fatal uncaught exception.

Whatever is causing the 500 error will appear in your web server's error logs. If you can't see the logs, enable error message display with ini_set('display_errors', true);.

Disabling TLS verification is a very bad idea. Doing it doesn't fix anything, it just hides the problem and undermines the security of your system. Fix it properly by reading the PHPMailer troubleshooting guide which goes into great detail on that sbject.

Upvotes: 2

Related Questions