HTML does not have native function for automatic sending of email, because it is client side
. What mailto
does, at most, just open the email client with the information from the form
.
As I said in the comment, to do this you can use the PHP mail function (which is server side
), as follows:
First you creates the variables to store the values coming from form
, using the superglobal $_POST
.
For example, if the field input
be like this:
<input type="text" name="nome">
<input type="text" name="endereco">
<input type="email" name="email">
The variables in PHP will look like this:
<?php
$nome = $_POST['nome'];
$email = $_POST['email'];
$endereco = $_POST['endereco'];
?>
Then you create the variables that function mail
PHP will use to send the email: $to
, $subject
, $msg
:
$to = "[email protected]";
$subject = "O assunto";
$msg = "Nome: $nome \n Endereço: $endereco."; // aqui você pode incluir os campos que quiser
Hence calls the function mail
:
mail($to, $subject, $msg);
So putting it all together let’s create the archive enviaremail.php
(Edit: I edited based on your HTML, test and if you have a problem let me know):
<html>
<head>
<title>Página de Enviar e-mail </title>
</head>
<body>
<?php
$nome = $_POST['nome']; // o $_POST usa o name pra pegar os dados
$email = $_POST['email'];
$mensagem = $_POST['mensagem'];
$to = "[email protected]";
$subject = "O assunto";
$msg = "Nome: $nome\n" . // O \n pula uma linha e o . conecta os pedaços
"Mensagem: $mensagem\n" .
"Enviado por: $email";
// aqui você pode incluir os campos que quiser
mail($to, $subject, $msg);
echo "Obrigado por enviar seu email"; // esta será a mensagem na página de saída, depois do e-mail ser enviado, mas vc pode personalizar...
?>
</body>
</html>
From there on the HTML page you call the created file:
<form method="post" action="enviaremail.php">
This should generate an output like this in [email protected]:
Name: [The name entered in the form];
Address: [The address entered in the form];
Sent by: [e-mail typed in the form];
Note: If by chance you want to send from localhost is already a little more complicated, but there is the answer here on Sopt: How to send email from localhost using the PHP mail function?
Can be with php?
– gustavox
@gustavox can be whatever it is, I need it to work just that. And on top of that I’m relatively new at this.
– Bruno Gibellino
Put the HTML code of the form... and you know that with PHP, you will need to run on a server that supports PHP. HTML does not send email the way you want, it opens the browser by filling in the form information that corresponds to the fields, but the submission has to be by the user himself. PHP will package everything and send it directly. Put the HTML code there for me to see if I can help you...
– gustavox
puts the form fields q vc has
– Erlon Charles
I made an answer, but now I saw that there is already an equal question with accepted answer: http://answall.com/questions/23602/como-mail-com-php?rq=1
– gustavox
@Brunogibellino managed to solve?
– gustavox
@gustavox sorry I did not see this your answer but I will already work on it. And thanks in advance
– Bruno Gibellino