Send mail with form data

Asked

Viewed 1,096 times

1

I have a form in a file html and needed to click on Submit the data were sent to an email. I tried to put on form, action="mailto:....", which was the only solution I could find, but when I clicked on Submit was just going to outlook. Does anyone know how to get the data submitted to an email?

<form action="php/mail.php" method="POST" accept-charset="utf-8"> 
  <input type="text" id="nome" name="nome" placeholder="Nome"  required> 
  <input type="email" id="mail" name="email" placeholder="E-mail"  required>
  <textarea id="message" name="mensagem" placeholder="Mensagem"required></textarea>
  <button class="btn btn-primary" type="reset">Apagar</button>
  <button class="btn btn-primary" type="submit">Enviar</button>
</form>

  • Can be with php?

  • @gustavox can be whatever it is, I need it to work just that. And on top of that I’m relatively new at this.

  • 1

    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...

  • puts the form fields q vc has

  • 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

  • @Brunogibellino managed to solve?

  • @gustavox sorry I did not see this your answer but I will already work on it. And thanks in advance

Show 2 more comments

3 answers

0

So friend, the easiest way, is to use a dynamic example php language, Asp, . net, plus some factors are also very crucial the host where your site is hosted because it influences a lot because each host has its setting for sending email. more on google vc find several tutorials on this: look at this example http://www.devmedia.com.br/enviar-formulario-por-e-mail-php/21757 any doubt put your doubt. hug.

0

Bruno, the ideal is for you to use the PHPMailer (in PHP). It’s famous, quite discussed here at Stackoverflow and super simple to implement.

Just create a file (e.g..: phpmailer_enviar.php) and import the class using require_once("phpmailer/class.phpmailer.php"); the content received from the form in the body of the message in $mail->Body. Remember that this file should the action='phpmailer_enviar'.

<?php
// Inclui o arquivo class.phpmailer.php localizado na pasta phpmailer
require_once("phpmailer/class.phpmailer.php");

// Inicia a classe PHPMailer
$mail = new PHPMailer();

// (...)

See a full tutorial on: http://blog.thiagobelem.net/enviar-e-mails-pelo-php-usando-o-phpmailer/

0

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?

Browser other questions tagged

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