Send e-mail with specific content of each line returned from mysql

Asked

Viewed 87 times

0

I need to send an email warning the user that your request is expired, I go to the bank and search all records in this condition, the problem is that the body of the email has to be the content of each returned line... In each email will have to go the user request data, the user can only receive the email referring to your request, are input data, exit, length of stay etc...

I made a part of the code where it goes in the bank and sends the email to everyone, the problem is that the body of the email is always the same.

How do I make PHP go to the database and send these emails to a list and the body of the message is the content of each specific line for each user ?

Code that sends to everyone with fixed body:

  <?php

    $select = "SELECT tb_movto_visitas.*, tb_cad_visitantes.nom_visitante, tb_cad_visitantes.qtd_visitas, tb_cad_motivos.des_motivo, dd_pessoais.nome, dd_pessoais.email  FROM tb_movto_visitas
    LEFT JOIN tb_cad_visitantes ON tb_movto_visitas.tb_cad_visitantes_cod_visitante = tb_cad_visitantes.cpf AND tb_movto_visitas.cod_visitado = tb_cad_visitantes.cpf_visitado
    LEFT JOIN tb_cad_motivos ON tb_movto_visitas.tb_cad_motivos_cod_motivo = tb_cad_motivos.cod_motivo
    LEFT JOIN dd_pessoais ON tb_movto_visitas.cod_visitado = dd_pessoais.cpf2

    WHERE 

    dat_prev_saida < now() and  dat_baixa = 0000-00-00 or
    (dat_prorroga_1 < now() and dat_baixa = 0000-00-00 or dat_prorroga_2 < now() and dat_baixa = 0000-00-00)

    ORDER BY tb_movto_visitas.`sol_visita` DESC";

    $query = mysqli_query($conn , $select);
    $row = mysqli_num_rows($query);

    if ($row <> 0) {

    while ($row_solicitacao = mysqli_fetch_assoc($query)) {


    require_once '/../../lib/PHPMailer/PHPMailerAutoload.php';
    $smtp_host = '130.100.10.24';
    $mail = new PHPMailer();
    $mail->IsSMTP(); 
    $mail->Host = $smtp_host;
    $mail->Port = 25;
    $mail->From = '[email protected]'; 
    $mail->FromName = 'Sistema de Visitas';

    $array = explode(",",$row_solicitacao['email']);
    $nb = count($array);
    for ($i=0;$i<$nb;$i++) {
        $mail->AddBCC($array[$i]);
    }


    $email_copia = "";
    $assunto = 'AVISO DE VISITAS VENCIDAS';
    $mensagem  = "Prezado(a) Senhor(a) Diego Santos <br><br>";
    $mensagem .= "De acordo com nosso cadastro de visitantes,<br>";
    $mensagem .= "Para cumprimento do PAD2006, favor  dirigir-se à t <br><br>";
    $mensagem .= "Almeida  <br><br>";
    $mensagem .= "Visitantes: <br>";
    $mensagem .= "Emissão: <br>";
    $mensagem .= "Validade: <br>";

    $mail->IsHTML(true);
    $mail->CharSet = 'UTF-8';  
    $mail->Subject = $assunto;
    $mail->Body = $mensagem;
    $mail->AltBody = $mensagem;

    $mail->Send(); 
    }
    } 
    ?>
  • Hello, If I understand correctly SQL is already working as intended and the email addresses are also ok. The embarrassment is in the body of the email that is not dynamic. That’s it?

  • Good. Then enter the answer and mark it as correct :D

  • Hello James, that’s right, I just solved!! I simply put the sending of the e-mail inside the "for" and into the body put the variable coming from the bank Ex: $message .= " Visitor: ". $row_request['guest name']." " ; $message .= " Issue: ". $row_request['dat_arrival']." ";

  • Please don’t add SOLVED or FIXED in the title, the site works different from forums. Visit [tour] to learn more about this operation.

  • Okay, Diegofm, got it. Thank you.

1 answer

3

The solution was to put the sending of the email inside FOR loop and insert the variables.

<?php

$select = "SELECT tb_movto_visitas.*, tb_cad_visitantes.nom_visitante, tb_cad_visitantes.qtd_visitas, tb_cad_motivos.des_motivo, dd_pessoais.nome, dd_pessoais.email  FROM tb_movto_visitas
LEFT JOIN tb_cad_visitantes ON tb_movto_visitas.tb_cad_visitantes_cod_visitante = tb_cad_visitantes.cpf AND tb_movto_visitas.cod_visitado = tb_cad_visitantes.cpf_visitado
LEFT JOIN tb_cad_motivos ON tb_movto_visitas.tb_cad_motivos_cod_motivo = tb_cad_motivos.cod_motivo
LEFT JOIN dd_pessoais ON tb_movto_visitas.cod_visitado = dd_pessoais.cpf2

WHERE 

dat_prev_saida < now() and  dat_baixa = 0000-00-00 or
(dat_prorroga_1 < now() and dat_baixa = 0000-00-00 or dat_prorroga_2 < now() and dat_baixa = 0000-00-00)

ORDER BY tb_movto_visitas.`sol_visita` DESC";

$query = mysqli_query($conn , $select);
$row = mysqli_num_rows($query);

if ($row <> 0) {

while ($row_solicitacao = mysqli_fetch_assoc($query)) {


require_once '/../../lib/PHPMailer/PHPMailerAutoload.php';
$smtp_host = '130.100.10.24';
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->Host = $smtp_host;
$mail->Port = 25;
$mail->From = '[email protected]'; 
$mail->FromName = 'Sistema de Visitas';

$array = explode(",",$row_solicitacao['email']);
$nb = count($array);
for ($i=0;$i<$nb;$i++) {

$mail->AddBCC($array[$i]);

$email_copia = "";
$assunto = 'AVISO DE VISITAS VENCIDAS';
$mensagem  = "Prezado(a) Senhor(a) ".$row_solicitacao['nom_solicitante']."<br><br>";
$mensagem .= "De acordo com nosso cadastro de visitantes, a(s) pessoa(s)...expirado desde o dia ".$row_solicitacao['dat_prev_saida'].". <br>";
$mensagem .= "Para cumprimento do PAD2006, favor  dirigir-se . <br><br>";
$mensagem .= "Figueiredo. <br><br>";

$mensagem .= "Visitante: ".$row_solicitacao['nom_visitante']." <br>";
$mensagem .= "Emissão: ".$row_solicitacao['dat_chegada']." <br>";
$mensagem .= "Validade: ".$row_solicitacao['dat_prev_saida']." <br>";

$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';  
$mail->Subject = $assunto;
$mail->Body = $mensagem;
$mail->AltBody = $mensagem;

$mail->Send(); 
}
}

} 
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.