I made this code to send an automatic email to the subscriber in db . The registration is ok, but the email does not go to the client. What is wrong?

Asked

Viewed 404 times

0

php code

<?php

include "conectar.php";


//comando para iserir dados direto do formulário para o banco de dados

$vnome=$_POST['nome'];
$vcpf=$_POST['cpf'];
$videntidade=$_POST["identidade"];
$vtelefone=$_POST["telefone"];
$vcelular=$_POST["celular"];
$vemail=$_POST["email"];
$vcep=$_POST["cep"];
$vendereco=$_POST["endereco"];
$vcomplemento=$_POST["complemento"];
$vbairro=$_POST["bairro"];
$vcidade=$_POST["cidade"];
$vuf=$_POST["uf"];
$vsexo=$_POST["sexo"];
$vidade=$_POST["idade"];
$vpeso=$_POST["peso"];


$sql="INSERT INTO solidario VALUES (NULL, '$vnome', '$vcpf', '$videntidade', '$vtelefone', '$vcelular', '$vemail', '$vcep', '$vendereco', '$vcomplemento', '$vbairro', '$vcidade', '$vuf', '$vsexo', '$vidade', '$vpeso')";

$res=mysqli_query($con,$sql);
$num=mysqli_affected_rows($con);

if($num == 1){

mail('[email protected]',$vnome,$vcpf, 'FROM:$vemail');
echo"<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=index.php'>
                <script type=\"text/javascript\">
                    alert(\"Seu cadastrado foi realizado com sucesso!.\");
                </script>

            ";  

}else{
    echo"   <META HTTP-EQUIV=REFRESH CONTENT = '0;URL=index.php'>
                <script type=\"text/javascript\">
                    alert(\"seu CPF já foi cadastrado!.\");
                </script>                   
            ";
}

mysqli_close($con);
?>
  • I think the error is in the parameters of the mail function it has 3 mail(string $to, string $subject, string $message) you eatá passing 4 and a echo down, but I don’t know if you’re doing any other way that I don’t know

1 answer

1

I believe it’s because of 'FROM:$vemail', when you use ' PHP does not interpret anything inside it.

Change to:

'FROM: '.$vemail

or

"FROM: $vemail"

Should to work. However, there is another detail, you are setting the FROM and not the TO. That is, the $vemail is that you are "sending" the message and not receiving, I believe you should do something like:

mail($vemail, $vnome, $vcpf, 'FROM: [email protected]');

So the $vemail will receive the message, sent by [email protected]. I believe this is enough. But depending on where you are running PHP there may be other problems, some shared hosts, for example, may not allow sending or require additional settings. If you are on localhost may also need to configure. If you have a lot of bad luck, the domain itself, DNS, can define who is authorized to send the email, which makes some email providers ignore the sent message.


There are other problems in the code, the way it is allows both a Mysql Injection, and a spam abuse. But, this is out of the question.

  • Thanks Inkeliz I keep studying if it works out I put here!

Browser other questions tagged

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