Form does not send the information to the email

Asked

Viewed 436 times

0

I need the information from my form to be sent to my email I’m doing at php but it doesn’t work follow my code:

        <form action="email.php" method="post">
    <label for="Nome">nome:</label>
    <input type="text" name="Nome" size="35" /><br>

    <label for="Email">E-mail:</label>
    <input type="text" name="Email" size="35" /><br>

    <label for="Fone">Telefone:</label>
    <input type="text" name="Fone" size="35" /><br>

    <label for="Mensagem">Mensagem:</label>
    <textarea name="Mensagem" rows="8" cols="40"></textarea><br> 

    <input type="submit" name="Enviar" value="Enviar" />
</form>

my code in php is in a separate file from my code:

    <?php
$Nome       = $_POST["Nome"];   // Pega o valor do campo Nome
$Fone       = $_POST["Fone"];   // Pega o valor do campo Telefone
$Email      = $_POST["Email"];  // Pega o valor do campo Email
$Mensagem   = $_POST["Mensagem"];   // Pega os valores do campo Mensagem

// Variável que junta os valores acima e monta o corpo do email

$Vai        = "Nome: $Nome\n\nE-mail: $Email\n\nTelefone: $Fone\n\nMensagem: $Mensagem\n";

require_once("phpmailer/class.phpmailer.php");

define('GUSER', '[email protected]');   // <-- Insira aqui o seu GMail
define('GPWD', 'lalala123');        // <-- Insira aqui a senha do seu GMail

function smtpmailer($para, $de, $de_nome, $assunto, $corpo) { 
    global $error;
    $mail = new PHPMailer();
    $mail->IsSMTP();        // Ativar SMTP
    $mail->SMTPDebug = 2;       // Debugar: 1 = erros e mensagens, 2 = mensagens apenas
    $mail->SMTPAuth = true;     // Autenticação ativada
    $mail->SMTPSecure = 'ssl';  // SSL REQUERIDO pelo GMail
    $mail->Host = 'smtp.gmail.com'; // SMTP utilizado
    $mail->Port = 587;          // A porta 587 deverá estar aberta em seu servidor
    $mail->Username = GUSER;
    $mail->Password = GPWD;
    $mail->SetFrom($de, $de_nome);
    $mail->Subject = $assunto;
    $mail->Body = $corpo;
    $mail->AddAddress($para);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Mensagem enviada!';
        return true;
    }
}

// Insira abaixo o email que irá receber a mensagem, o email que irá enviar (o mesmo da variável GUSER), 

 if (smtpmailer('[email protected]', '[email protected]', 'felipe', 'Assunto do Email', $Vai)) {

    Header("location:http://www.dominio.com.br/obrigado.html"); // Redireciona para uma página de obrigado.

}
if (!empty($error)) echo $error;
?>

run the project and gave this error:

inserir a descrição da imagem aqui

  • 1

    Instead of just using \n, use \r\n as line break.

  • @qmechanik friend so nothing happened i lico in the starting button and he redirects me to a page where has hi my php code on the screen and does not send

  • 1

    You have the application in local environment?

  • ss site I haven’t even hosted my site yet and I’m very novice in php started today say asim rsrsrsrs

  • When you submit the form, does it go to the . php page that contains the code and displays it? So the problem is on your web server. Make sure you have installed PHP and Apache correctly.

  • 1

    Take a look at this topic: http://stackoverflow.com/questions/4532486/failed-to-connect-to-mailserver-at-localhost-port-25 OBS: It is not easier for you to use Phpmailer or Swiftmailer?

  • 1

    Put your $mail->SMTPDebug = 0; with the value 2. $mail->SMTPDebug = 2; and look what happened.

  • friend gave a different error I will edit the post and put the error that gave

  • Take a look at the gmail configuration table: https://support.google.com/a/answer/176600?hl=pt-BR according to this documentation port 587 uses TLS, try switching to port 465. (I hope that’s right...) or changes your SMTPSecure for tls

  • 1

    Hi, go to: https://www.google.com/settings/security/lesssecureapps and cancel the option: Ativar. From a time until you receive an email from Google saying that security has been disabled and try running the script. : $mail->SMTPSecure = 'ssl'; $mail->Port = 465;

  • ready I’ll wait now and see what’s taking so long ???

  • obrigadooooo rapaziadaaaa amo vcsssssssssssss uhuuuuuu

Show 7 more comments

2 answers

2


  $headers .= "From:  dominio.com.br<[email protected]>\n";
  $headers .= "X-Sender:  <[email protected]>\n";
  $headers .= "X-Mailer: PHP  v".phpversion()."\n";
  $headers .= "X-IP:  ".$_SERVER['REMOTE_ADDR']."\n";
  $headers .= "Return-Path:  <[email protected]>\n";
  $headers .= "MIME-Version: 1.0\n";

You need to configure this chunk with information from your server or a third party server, so it’s not working.

Since you’re learning, this article can help you in setting up sending emails via SMTP by Gmail which is free.

  • hello friend put on wamp server and gave this error up I edited and put a print on the post

  • guy and I started kind of that today with php I don’t know anything I need to change type what for example

1

The PHP code needs to be compiled, and for that you need a web server (HTTP) and PHP to execute the code!

Install WAMP or XAMPP or Easyphp

WAMP

XAMPP

Easyphp

These WAMP (Windows, Apache, Mysql, PHP) packages (or LAMP - where L is from Linux) are programs that install the Web server (Apache), PHP and a database (Mysql) and then you can test your PHP code on your machine.

Once you install and start Apache (web server), you will put the files in a predetermined folder (Depends on where you installed the server) and then you can access by http://localhost/fileName.php and run the tests!

Sending e-mail via SMTP

To send emails, you will need a server SMTP, but I do not recommend you try to install one on your machine, because in addition to headache, if it is not very well configured, someone over the internet may end up sending SPAM from your computer to anywhere in the world.

If you want to test the sending of emails, I recommend that you install the Papercut. It is a small SMTP server that sends any email to itself. So you can test and view the email directly by it. Just download and install it, then just try sending the email again by PHP and you will see that Papercut received a new message!

  • hello friend put on wamp server and gave this error up I edited and put a print on the post

  • Good, that means it’s working! What happens is that to send emails you need a server SMTP and since you don’t have one on your machine (or waste time trying to install one if the machine is just for testing), I recommend that you either ignore the error, or if you really want to see how the email will arrive, use an SMTP test server, such as the Papercut

Browser other questions tagged

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