0
I make an appointment at the bank and am printing on screen normally through a foreach
. I’m having a hard time getting the data from foreach
and send it to Phpmailer.
My code:
include "banco.php";
function BuscaAlgo($conexao){
$query = "SELECT USU.experiencia,
USU.altura,
USU.peso,
PRAN.exp_ref,
PRAN.altura_ref,
PRAN.peso_ref,
PRAN.tipo_prancha,
PRAN.tamanho_prancha,
PRAN.meio_prancha,
PRAN.litragem_prancha
FROM DADOS_USUARIO AS USU
INNER JOIN PRANCHA AS PRAN
on USU.experiencia = PRAN.exp_ref
WHERE USU.altura = PRAN.altura_ref
AND USU.peso = PRAN.peso_ref
ORDER BY USU.usuario DESC LIMIT 1";
//RETORNA A MENSAGEM COM O UTF8
mysqli_set_charset($conexao, "utf8");
//VARIÁVEL QUE GUARDA A CONSULTA NO BANCO
$resultado = mysqli_query($conexao,$query);
if( !$resultado ){
die("Consulta falhou.");
}
$retorno = array();
//PEGA TODOS OS VALORES DA CONSULTA E INSERE NO ARRAY $retorno[];
while($experiencia = mysqli_fetch_assoc($resultado)){
$retorno[] = $experiencia;
}
return $resultado;
}
$resultado = array();
$resultado = BuscaAlgo($conexao);
//MEU FOREACH -> IMPRIME NA TELA
foreach($resultado as $valor){
header('Content-Type: text/html; charset=utf-8');
echo "<span class='desc'>TAMANHO 1:</span>"; echo $valor["tamanho_primeiro"]; print("<br>");
echo "<span class='desc'>TAMANHO 2:</span>"; echo $valor["tamanho_segundo"]; print("<br>");
echo "<span class='desc'>TAMANHO 3:</span>"; echo $valor["tamanho_terceiro"];
include 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'minhaSENHA';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->IsHTML(true);
$mail->From = '[email protected]';
$mail->FromName = 'REMETENTE';
$mail->addAddress('[email protected]');
$mail->Subject = 'E-mail PHPMailer';
$mail->Body;
foreach($resultado as $valor){
$mail->Body .=$valor["tamanho_primeiro"];
$mail->Body .=$valor["tamanho_segundo"];
$mail->Body .=$valor["tamanho_terceiro"];
}
// Envia o e-mail e captura o sucesso ou erro
if($mail->Send()):
echo 'Êxito no envio!';
else:
echo 'Erro ao enviar Email:' . $mail->ErrorInfo;
endif;
return $valor;
}
What am I doing wrong?
Obs: The code of the PHPMailer
is working properly. I tested it in another file by sending any text and the sending was done successfully.
#UPDATING
//FOREACH
foreach($resultado as $valor){
header('Content-Type: text/html; charset=utf-8');
echo "<span class='desc'>TAMANHO 1:</span>"; echo $valor["tamanho_primeiro"]; print("<br>");
echo "<span class='desc'>TAMANHO 2:</span>"; echo $valor["tamanho_segundo"]; print("<br>");
echo "<span class='desc'>TAMANHO 3:</span>"; echo $valor["tamanho_terceiro"];
$tamanho_pri = $valor["tamanho_primeiro"];
$tamanho_seg = $valor["tamanho_segundo"];
$tamanho_ter = $valor["tamanho_terceiro"];
}
//PHPMAILER
$mail->Body = $tamanho_pri;
$mail->Body .= $tamanho_seg;
$mail->Body .= $tamanho_ter;
I did it this way and it’s working. However, this is the best way to do it by following good practices?
What is this difficulty? could detail better
– rray
It is not sending, this is the problem /:
– Zkk
I took a look at the console of browse=dor and I do not see any error
– Zkk
you have two foreach doing the same thing ... and have one
return $valor;
lost– rray
The first
foreach
returns to the user. The secondforeach
(in my logic), it is for Phpmailer– Zkk
you have
foreach($resultado as $valor){
and then$mail->Body;
 foreach($resultado as $valor){
– rray
I removed the
return $valor
and now it’s sending. How can I have a singleforeach
sent to thePHPMailer
and printed on screen for the user?– Zkk
Just remove the second, do the assignment of
$mail->Body
and concatenate the strings.– rray
I modified the code and now it’s working. The way I did, is it correct according to good practice? (I updated the post)
– Zkk
It is possible to improve some things, for example change those 3 lines of the
$mail->body
for$mail->Body = $valor["tamanho_primeiro"] . ' - '. $valor["tamanho_segundo"];
and take out the phpmailer configuration from foreach since most of it will not change, should stay inside the foreachsend()
andaddAddress()
do not forget to reset the email addresses otherwise the first destination will receive the same email every time.– rray
I made the modifications, it worked. Thank you very much for your help!
– Zkk
I edited the question because "phpmyadmin" is not a "database", read this to understand the differences: What is the difference between mysql and phpmyadmin?
– Guilherme Nascimento