Code to send message to email by push button

1

I already have the form with the message box and etc, I already have the send button, but I need the code to when I click send, the message will be sent to a certain email.

My code:

<form action="#" method="post">
    <div class="col-md-9">
      <div class="col-md-4">
        <input type="text" name="name" id="name" class="name" placeholder="Seu Nome">
      </div>
      <div class="col-md-4">
        <input type="text" name="email" id="email" class="email" placeholder="Seu E-mail">
      </div>
      <div class="col-md-4">
        <input type="text" name="subject" id="subject" class="subject" placeholder="Assunto">
      </div>
      <div class="col-md-12">
        <textarea name="message" cols="1" rows="1" class="message" placeholder="Sua mensagem..." id="message"></textarea>
      </div>
      <div class="col-md-4">
        <input type="submit" name="send" value="Enviar Mensagem" id="submit" class="button templatemo_sendbtn">
      </div>
    </div>
  </form>
    
asked by anonymous 06.10.2016 / 17:58

3 answers

2

I'll explain how it's going to stay, you can read about the mail to better understand later.

We assume your page is index.html or contato.html , so just change the line of your current code to html of:

<form action="#" method="post">

To:

<form action="enviar.php" method="post">

See the code in operation .

If you want to put your email, I update the code so you can see how the message arrives in your email.

Looking like this:

<form action="enviar.php" method="post">
<div class="col-md-9">
  <div class="col-md-4">
    <input type="text" name="name" id="name" class="name" placeholder="Seu Nome">
  </div>
  <div class="col-md-4">
    <input type="text" name="email" id="email" class="email" placeholder="Seu E-mail">
  </div>
  <div class="col-md-4">
    <input type="text" name="subject" id="subject" class="subject" placeholder="Assunto">
  </div>
  <div class="col-md-12">
    <textarea name="message" cols="1" rows="1" class="message" placeholder="Sua mensagem..." id="message"></textarea>
  </div>
  <div class="col-md-4">
    <input type="submit" name="send" value="Enviar Mensagem" id="submit" class="button templatemo_sendbtn">
  </div>
</div>
</form>

Now create a new page named enviar.php and put the following code inside it:

<?php

    $para = "[email protected]";
    $name = $_POST['message'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $mensagem = "Nome: $name<br>";
    $mensagem .= "Email: $email<br>";
    $mensagem .= "Assunto: $subject<br>";
    $mensagem .= "Mensagem: $message<br>";
    $headers = 'From: '.$email."\r\n". 'Reply-To: '.$email."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    if (mail($para, $subject, $mensagem, $headers)){
        echo "Sua mensagem foi enviada com sucesso!";
    }
    else{
        echo "Aconteceu um erro, tente novamente mais tarde.";
    }

?>

Note: If you want help using PHPMailer we can help too.

    
06.10.2016 / 18:52
1

You can use this:

<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email']))  {

//Email information
$admin_email = "[email protected]";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];

//send email
mail($admin_email, "$subject", $comment, "From:" . $email);

//Email response
echo "Thank you for contacting us!";
}

?>

Note that in the example the Request is used, but you can use $ _POST.

    
06.10.2016 / 18:03
1

The PHPMailer plugin implements all PHP methods needed for you to send the email.

By clicking here you can see an example that exactly solves your problem.

In summary:

1. Download the plugin.

2. Add to your project.

3. Create a file EQUAL to this example .

4. In the action of your form, put path to the file you created in step 3 (something like action="php/enviar-email.php" )

This should work. If it does not work, comment on my answer:)

    
06.10.2016 / 18:28