Code to send email message by Send button

Asked

Viewed 6,155 times

1

I already have the form with the message box and etc, I also have the send button, but I need the code to when click send, the message be sent to certain e-mail.

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>
  • If my answer doesn’t help, you could put your code ? It would be easier to help.

3 answers

2

I’ll explain how it’s going to work, you can give a read on the function mail to better understand later.

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

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

To:

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

View code in operation.

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

Thus remaining:

<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 with name enviar.php and place within it the following code:

<?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 the PHPMailer we can help too.

1

You can use it like 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 Request is used, but you can use $_POST.

  • Where do I put this code? Inside my index and inside <script> ?

  • That code I introduced, that’s where your boot will point. Please edit your question and enter your HTML code with your form. Then I help with the code.

1

The plugin Phpmailer implements all the PHP methods necessary for you to be able to send the email.

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

In short:

1. Download the plugin.

2. Add to your project.

3. Create a file EQUAL to of this example.

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

This should work. If it doesn’t work, comment on my answer :)

Browser other questions tagged

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