-1

how are things going there?

I developed a php script to send an e-mail to a main user and some other copied users. When I test it putting the e-mail and names directly in the code like this: $mail->addAddress('email@provider.com','Any Name'); I receive the e-mail successfully, but when I get them from the mysql database like this: $mail->addAddress($r['userEmail',$r['userFullName']); $mail->addCC($r['userEmail2',$r['userFullName2']); The script ran successfully, but the e-mail never arrives.

Did someone face something like that before and can help to solve my issue?

See all my code:

    <?php
    include '../dao/conn.php';
    require_once('../inc/phpmailer/class.phpmailer.php');
    $mail = new PHPMailer(true);
    $mail->IsSMTP();
    $mail->Host = 'smtp.domainname.com';
    $mail->Port = 25;
    $mail->SetFrom('no-reply@domainname.com');
    $mail->IsHTML(true);

    //pesquisa os projetos finalizados ha mais de 90  dias expecificado pelo envio manual
    $sql1 = "SELECT p.PDC_NumControle ID
    ,p.PDC_Nome Projeto
    ,DATE_FORMAT(g.GRT_DataInicio,'%d/%m/%Y') DataTermino
    ,datediff(sysdate(), g.GRT_DataInicio) Dias
    ,l.`Position Name` NomePresidente
    ,l.`Position Email` EmailPresidente
    ,u.ManagerName NomeGerente
    ,u.ManagerEmail EmailGerente
    ,u.ArchtectName NomeArquiteto
    ,u.ArchtectEmail EmailArquiteto
    FROM tabgarantiaprojeto g
    INNER JOIN tabprojetos p ON g.GRT_NumContProjeto = p.PDC_NumControle
    INNER JOIN tabpwdusuario u ON u.USU_NumControle = p.PDC_NumContUsuario
    WHERE g.GRT_Status = 1 AND g.GRT_NumControle = $idGarantia AND datediff(sysdate(), g.GRT_DataInicio) >= 90;";
    $res1 = mysqli_query($conn, $sql1);
    $rowCount = mysqli_num_rows($res1);

    if($rowCount > 0) {
        $r = mysqli_fetch_assoc($res1);
    
        //Lê os dados do projetos e envia e-mail para o presidente
        $diasProj = $r["Dias"];
        $projID = $r["ID"];
        $NomeProjeto = $r["Projeto"];
        $dataTermino = $r["DataTermino"];
        
        $mail->AddAddress($r['EmailPresidente'], $r['NomePresidente']);
        $mail->addCC($r['EmailArquiteto'], $r['NomeArquiteto']);
        $mail->addCC($r['EmailGerente'], $r['NomeGerente']);

        $mail->Subject = "Garantia de Obra - $NomeProjeto";
        $mail->AltBody = "";
        $mail->IsHTML(true);
        
        $conteudo = 'Here is my html content';
        $mail->Body = $conteudo;
        $resp = $mail->send();
        if ($resp) {
           $res = 1;
           $msg = "E-mail de aviso enviado com sucesso!"; 
        } else {
           $res = 0;
           $msg = "Não foi possivel processar sua solicitação por causa do seguinte erro: " . $mail->ErrorInfo;
        }
        $mail->ClearAddresses();
        $mail->ClearCCs();
        $mail->ClearBCCs();
           
    }

Any help will be appreciated.
  • 1
    Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Tangentially Perpendicular Aug 31 '21 at 04:29
  • Firstly, you're using an ancient version of PHPMailer that contains known vulnerabilities. You **need** to upgrade. Don't assume that your variables contain what you hope they do: **check**. Also you have no error checking on `addAddress`, so you'll never know if it fails. – Synchro Aug 31 '21 at 06:27

2 Answers2

0

Try setting the values to variables first such as

$addcc1 = $r['EmailArquiteto']; $addnom1 = $r['NomeArquiteto'];

then setting the mailer to that variable:

$mail->addCC($addcc1, $addnom1);

This makes it so you can also do troubleshooting such as:

echo "$addcc1 - $addnom1" to make sure they are what you expect them to be.

0

All is well with the script, as I didn't receive the e-mail copied to me, I thought there was a issue in the script, but all the others recipients received the e-mails successfully.

The problem is in my e-mail account that is not receiving e-mails from the application. I'll investigate with the e-mail server team.

Thanks for all your tips!