Script para enviar email

Asked

Viewed 2,535 times

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’t find 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>

Tela de contato

  • 2

    What’s the language, young man?

  • 2

    You won’t be able to do this only with HTML and Javascript. You need to use a server side language like ASP, PHP, C#, JSP,...

  • Would you have any solution with PHP? I’m studying PHP yet, let’s say I’m not yet able to develop it, even if it’s simple. But for this my code above, which PHP function would solve? Thanks!

  • Try using Phpmailer, it will make your job easier and it’s easy to set up, https://github.com/PHPMailer/PHPMailer

  • Could you give me an example in PHP with my code? Put is a little complicated with the topic that the boy went through. On my server, for example, I will have to create some folder?

  • Check this link http://blog.thiagobelem.net/enviar-e-mails-pelo-php-usando-o-phpmailer

Show 1 more comment

3 answers

3

You can use the 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!';
       }
 ?>

2

I think it would be 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");
};

*Noting that this is just an example form, in this case, you may be viewing a similar one in the php.net

*Your server must allow the sending of forms, if it is localhost, may not work, to test recommend a free plan at Hostinger or other hostsites

*Use this script as the action="" of his form and method="post"

link: https://secure.php.net/manual/en/tutorial.forms.php

  • I recommend avoiding answering questions that could be duplicates, see the tips on how to find the ideial answer

  • Sometimes his problem may be another, so the duplicate answer may not solve the problem

  • I’m not here to play games, I’m here to give you a tip smooth functioning from the site, so please take as a constructive tip. I am grateful ;)

  • I’m not stupid, vlw flw

-5

Only with front-end languages can you get only through tools like formspree which is what I did with a website my, how I used the emerges to host, and it only accepts front-end language, I had to do this.

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

Example of how you would look with 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>
  • I just saw here, and the server that we will use does not support PHP, I can only use Javascript, which in case it is obvious put is client side. Is there any way (any way) I can do this? Thank you!

  • As I explained there, there are tools (like formspree) that send the email to you with the information filled in the forms (the <form> tag) the only thing you should do is add the action attribute, redirecting to the https://formspree.io/[email protected] link and that’s it, the site makes the back-end for you and sends an e-mail in table form with the name of inputs and values. If you see in the code I put, I’ve already made the change the only thing you need to do is change your email to the email you want.

Browser other questions tagged

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