0

I'm not receiving emails anymore using PHPMailer.

I have the following code relative to a php page which uses PHPMailer to send an e-mail to notify a client request for a specific service. I tested the code in march for the services "Analisi posizioni", "Evento" and "Pianificazione assicurativa" and it worked properly. Yesterday I added two more services "Contattami" and "Lavora con me", I tested the code again and I realized I'm not receiving the emails anymore, even though 'mail.send()' returns true. I have the problem for all the services, including the first three I did in march. I also tried to create a new php file just to send a simple mail and it doesn't work either. Same problem testing the code found on Youtube and on the official page of PHPMailer.

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="style/desktop/structure.css">
    <link rel="stylesheet" href="style/desktop/common.css">
    <link rel="stylesheet" href="style/desktop/elab-form.css">
</head>
<body>

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

require 'config.php';


function fetchAttachments($mailObject, $fieldName) {
    if(isset($_FILES[$fieldName]["name"])) {
        $numFile = count($_FILES[$fieldName]["name"]);

        for($i=0; $i<$numFile; $i++) {
            $tempFile = $_FILES[$fieldName]["tmp_name"][$i];
            $fileName = $_FILES[$fieldName]["name"][$i];

            if($tempFile != "") 
                $mailObject -> addAttachment($tempFile, $fileName);
        }
        return $numFile;
    }
    return 0;
}

session_start();
$_SESSION['fi'] = 'form';
$source = "index.html";


if(!isset($_POST["ap-confirm"]) and !isset($_POST["e-confirm"]) and !isset($_POST["pm-confirm"]) and !isset($_POST["cnt-confirm"]) and !isset($_POST["wwm-confirm"])) {
    session_destroy();
    header("Location: index.html");
}

$mail = new PHPMailer(true);
$mail -> SMTPDebug = 2;
$mail -> SMTPAuth = true;    
$mail -> Host = "smtp.gmail.com";
$mail -> SMTPSecure = "ssl";
$mail -> Port = 465;

$mail -> Username = $USERNAME;
$mail -> Password = $PASSWORD;

$mail -> setFrom($USERNAME);
$mail -> addAddress($USERNAME);
$mail -> addReplyTo($USERNAME);

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

// SERVIZI =================================================================
// Analisi posizioni
if(isset($_POST["ap-confirm"])) {
    $mail -> Subject = "Richiesta Analisi delle posizioni assicurative e finanziarie in essere";
    $mail -> Body = $_POST["ap-gender"] . " " .
               "<b>" . addslashes($_POST["ap-name"]) . "</b>" .
               "<br>Professione<b>: " . addslashes($_POST["ap-job"]) . 
               "</b><br><br>Telefono<b>: " . $_POST["ap-tel"] .
               "</b><br>Email<b>: " . $_POST["ap-email"] .
               "</b><br>Data di nascita<b>: " . $_POST["ap-birth"] .
               "</b><br>Residenza<b>: " . addslashes($_POST["ap-address"]) .
               "</b><br>Messaggio<b>: " . addslashes($_POST["ap-message"]) . "</b><br><br>";     

    $fileFetched = fetchAttachments($mail, "ap-file");
    $source = "servizi.html";
}

// Evento
else if(isset($_POST["e-confirm"])) {
    $mail -> Subject = "Evento";
    $mail -> Body = "<b>" . addslashes($_POST["e-name"]) . "</b>" .
               "<br><br>Telefono<b>: " . $_POST["e-tel"] .
               "</b><br>Email<b>: " . addslashes($_POST["e-email"]) . 
               "</b><br>Contesto<b>: " . $_POST["e-context"] .
               "</b><br>Messaggio<b>: " . addslashes($_POST["e-message"]) . "</b><br><br>";
               
    $fileFetched = fetchAttachments($mail, "e-file");
    $source = "servizi.html";
}

// Pianificazione assicurativa
else if(isset($_POST["pm-confirm"])) {
    $mail -> Subject = "Richiesta Pianificazione assicurativa e patrimoniale completa";
    $mail -> Body = $_POST["pm-gender"] . " " .
               "<b>" . addslashes($_POST["pm-name"]) . "</b>" .
               "<br>Professione<b>: " . addslashes($_POST["pm-job"]) . 
               "</b><br><br>Telefono<b>: " . $_POST["pm-tel"] .
               "</b><br>Email<b>: " . $_POST["pm-email"] .
               "</b><br>Data di nascita<b>: " . $_POST["pm-birth"] .
               "</b><br>Residenza<b>: " . addslashes($_POST["pm-address"]) .
               "</b><br><br>Messaggio<b>: " . addslashes($_POST["pm-message"]) . "</b><br><br>";
               
    $fileFetched = fetchAttachments($mail, "pm-file");
    $source = "servizi.html";
}

// CONTATTI ===============================================================
// Contattami
else if(isset($_POST["cnt-confirm"])) {
    $mail -> Subject = "Richiesta di contatto";
    $mail -> Body = "<b>" . addslashes($_POST["cnt-name"]) . "</b> " .
               "<br>Email:<b> " . $_POST["cnt-mail"] . "</b>" .
               "<br>Telefono<b>: " . $_POST["cnt-tel"] . "</b>" .
               "<br><br>Messaggio<b>: " . addslashes($_POST["cnt-message"]) . "</b><br><br>";

    $source = "contattami.html";
}

// Lavora con me
else if(isset($_POST["wwm-confirm"])) {
    $mail -> Subject = "Richiesta di contatto";
    $mail -> Body = "<b>" . addslashes($_POST["wwm-name"]) . "</b> " .
               "<br>Email:<b> " . $_POST["wwm-mail"] . "</b>" .
               "<br>Telefono<b>: " . $_POST["wwm-tel"] . "</b>" .
               "<br><br>Messaggio<b>: " . addslashes($_POST["wwm-message"]) . "</b><br><br>";

    $fileFetched = fetchAttachments($mail, "wwm-file");
    $source = "contattami.html";
}

// LAYOUT =================================================================

echo '<img class="elab-img" src="resources/images/logo/horizontal/FI_logo_affiancato.png">';

if(!$mail -> send()) 
    echo '<h3 class="elab-desc">Errore nell\'invio della richiesta: ' . $mail -> ErrorInfo . '. Riprova più tardi.</h3>';
else 
    echo '<h3 class="elab-desc">La richiesta è stata inviata con successo.</h3>';
    
session_destroy();
header("Refresh: 4; url=" . $source);
?>

</body>
</html>
Nico99
  • 1
  • 1
  • Check your server logs for errors? – j08691 Jul 14 '23 at 14:38
  • Read https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting and come back when you have some meaningful info about the problem. Right now, it could be almost anything. Code which used to work but now doesn't, without any relevant changes having been made to it, is rarely going to give us much clue about the problem, it's more likely something changed in your environment, which we can't access, and you don't seem to have investigated in any detail. Get some debug info so we can help you out better. Thanks. – ADyson Jul 14 '23 at 14:50
  • P.S. Is this really your real code though? Has it been carefully pared back to form a [mre]? If so, there are some issues: e.g. `$mail -> Username = $USERNAME;` (and elsewhere), `$USERNAME` and `$PASSWORD` don't seem to be defined anywhere, yet are clearly crucial to the success of the script. – ADyson Jul 14 '23 at 14:51
  • Also you have `SMTPDebug = 2` set, so you should be seeing debug output, but you've not shown us what it says. – Synchro Jul 14 '23 at 17:05

0 Answers0