Script to send email [duplicate]

4

I am creating a page, where you have the contact part, I have already made the form, etc., but at the time of sending, I can not get a function to send to my email.

Could someone help?

<article id="contact">
    <h2 class="major">Contato</h2>
    <form method="post" action="#">
        <div class="field half first">
            <label for="name">Nome</label>
            <input type="text" name="name" id="name" />
        </div>
        <div class="field half">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" />
        </div>
        <div class="field">
            <label for="message">Mensagem</label>
            <textarea name="message" id="message" rows="4"></textarea>
        </div>
        <ul class="actions">
            <li><input type="submit" value="Enviar mensagem" class="special" /></li>
            <li><input type="reset" value="Apagar tudo" /></li>
        </ul>
    </form>
    <ul class="icons">

        <li><a href="#" class="icon fa-facebook"><span class="label">Facebook</span></a></li>
        <li><a href="#" class="icon fa-whatsapp"><span class="label">Whatsapp</span></a></li>
    </ul>
</article>

Contact screen http://image.prntscr.com/image/4afccbd1a7c24f23a9c12cecef401839.png

    
asked by anonymous 30.01.2017 / 14:31

3 answers

3

You can use PHPMailer to perform this task.

HTML

<form method="post" action="#" id="form">
    <div class="field half first">
        <label for="name">Nome</label>
        <input type="text" name="name" id="name" />
    </div>
    <div class="field half">
        <label for="email">Email</label>
        <input type="text" name="email" id="email" />
    </div>
    <div class="field">
        <label for="message">Mensagem</label>
        <textarea name="message" id="message" rows="4"></textarea>
    </div>
    <ul class="actions">
        <li><input type="submit" value="Enviar mensagem" class="special" /></li>
        <li><input type="reset" value="Apagar tudo" /></li>
    </ul>
</form>

JS

$( "#form" ).submit(function( event ) {
       event.preventDefault();

       $.ajax({
          type : 'post',
          url : 'seu_arquivo_php.php',
          data: $("#form").serialize(),
          success: function( response ) {
               console.log( response );
          }
      });           
});

PHP

 <?php

      require 'PHPMailerAutoload.php';

      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];

       $mail = new PHPMailer;

       $mail->isSMTP();                                      // Habilita SMTP
       $mail->Host = 'seusmtp.com.br';  // Especifique o SMTP que você contratou
       $mail->SMTPAuth = true;                               // Habilita autenticação SMTP
       $mail->Username = '[email protected]';                 // Usuário do SMTP
       $mail->Password = 'secret';                           // Senha do SMTP
       $mail->SMTPSecure = 'tls';                            // criptografia (tls ou ssl)
       $mail->Port = 587;                                    // Porta de conexão SMTP

       $mail->addAddress($email, $name);     // Para quem?

       $mail->addReplyTo('[email protected]', 'Information'); //Resposta pra qm?
       //$mail->addCC('[email protected]'); //Cópia do email
      // $mail->addBCC('[email protected]'); //Cópia Oculta


       $mail->isHTML(true);                                  // Formata o E-mail em html

       $mail->Subject = 'Teste de envio'; //Assunto
       $mail->Body    =  $message ;//Mensagem
       $mail->AltBody =  $message; // texto simples para clientes de correio não-HTML

       if(!$mail->send()) {
           echo 'Não foi possível enviar.'.'Mailer Error: ' . $mail->ErrorInfo;
       } else {
            echo 'Enviado com sucesso!';
       }
 ?>
    
31.01.2017 / 14:50
2

I think it would look something like this:

PHP

<?php
session_start();

$nome = $_POST["/*Nome aqui*/"];
$email = $_POST["/*Email aqui*/"];
$mensagem = $_POST["/*Mensagem aqui*/"];
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: /*Seu nome aqui*/" . "\r\n";
$headers .= "Responder à /*Seu email aqui*/" . "\r\n\r\n";

$mailBody = "/*Aqui você pode estrurar um corpo em HTML caso queira uma mensagem mais bonita, sem esquecer o CSS também*/";

if(mail("{$nome} <{$email}>, /*Seu nome aqui*/ </*Seu email aqui*/>", "/*Seu nome aqui*/", $mailBody, $headers)){
    $_SESSION["success"] = "Email enviado com sucesso";
    header("location:index.php");
}else{
    $_SESSION["error"];
    header("location:index.php");
};

* Note that this is just an example form, in this case, you may be seeing a similar one in% itself of%

* Your server should allow forms to be sent, if it is php.net , it may not work, to test recommend a free plan on hostinger or other hostsites

* Use this script as localhost of your action="" and form

link: link

    
31.01.2017 / 14:48
-5

With only front-end languages you can only get through tools like formspree which was what I did with a site my, how I used pops up to host, and he only accepts front-end language, I had to do this.

You can even do with the javascript, but personal information (such as passwords and api key for example) will be exposed.

Example of how to get formspree:

<form method="post" action="https://formspree.io/[email protected]">
        <div class="field half first">
            <label for="name">Nome</label>
            <input type="text" name="name" id="name" />
        </div>
        <div class="field half">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" />
        </div>
        <div class="field">
            <label for="message">Mensagem</label>
            <textarea name="message" id="message" rows="4"></textarea>
        </div>
        <ul class="actions">
            <li><input type="submit" value="Enviar mensagem" class="special" /></li>
            <li><input type="reset" value="Apagar tudo" /></li>
        </ul>
    </form>
    
30.01.2017 / 16:55