Help with PHP email form

Asked

Viewed 92 times

0

I have a problem with a website that is one page, and before I had this php:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = ''; 
    $to = '[email protected]'; 
    $subject = $_POST['subject'];

    $body ="De: $name\nMensagem:\n $message";
    if (!$_POST['name']) {
        $errName = 'Por favor insira o seu nome';
    }

    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Por favor insira um endereço de email válido';
    }

    if (!$_POST['subject']) {
        $errSubject = 'Por favor insira o assunto da sua mensagem';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Por favor escreva a sua mensagem';
    }

    if (!$errName && !$errEmail && !$errSubject && !$errMessage) {
       if (mail ($to, $subject, $body, $email)) {
          $result='<div class="alert alert-success">Obrigado sr/&#40a&#41 '. $name .'! Entraremos em contacto consigo brevemente.</div>';
       } else {
          $result='<div class="alert alert-danger">Desculpa ocorreu um erro ao enviar a sua mensagem. Por favor tente mais tarde.</div>';
       }
    }
}

to send mails together with this form:

<div id="formul" class="row">
            <div class="col-md-offset-1 col-md-10">

            <form class="form-horizontal" role="form" method="post" action="index.php#formul">
              <div class="form-group">
                <div class="col-md-offset-2 col-md-8">
                  <input type="text" class="form-control" id="name" name="name" placeholder="Nome:" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                  <?php echo "<p class='text-danger'>$errName</p>";?>
                </div>
              </div>
              <div class="form-group">
                <div class="col-md-offset-2 col-md-8">
                  <input type="email" class="form-control" id="email" name="email" placeholder="E-mail:" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                  <?php echo "<p class='text-danger'>$errEmail</p>";?>
                </div>
              </div>
              <div class="form-group">
                <div class="col-md-offset-2 col-md-8">
                  <input type="text" class="form-control" name="subject" placeholder="Assunto:" value="<?php echo htmlspecialchars($_POST['subject']); ?>">
                  <?php echo "<p class='text-danger'>$errSubject</p>";?>
                </div>
              </div>
              <div class="form-group">
                <div class="col-md-offset-2 col-md-8">
                  <textarea class="form-control" rows="3" placeholder="Mensagem:" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                  <?php echo "<p class='text-danger'>$errMessage</p>";?>
                </div>
              </div>
              <div class="form-group">
                <div class="col-md-offset-2 col-md-8">
                 <input id="submit" name="submit" type="submit" value="Enviar Mensagem" class="btn btn-theme btn-lg btn-block">
                </div>
               <div class="form-group">
                    <div class="col-md-offset-2 col-md-8">
                        <?php echo $result; ?>
                    </div>
               </div>
              </div>
            </form>

            </div>

        </div>

It turns out that it stopped sending mails because I heard that Servers do not allow the mail() function, I’ve heard about phpmailer but I don’t understand how it works and I can’t get it to look like this before, someone, can help me?

  • The page keeps loading and does not send anything or give any error.. :/

1 answer

0

Download phpmailer, put it in your project folder and leave the upload code in this style:

<?php
if (isset($_POST["submit"])) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $subject = $_POST['subject'];
    $body ="De: $name\nMensagem:\n $message";

    $from = '[email protected]';
    $password = 'SenhaDoEmailAqui';

    require_once('phpmailer/class.phpmailer.php');

    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->SMTPDebug = 1;
    $mailer->Port = 587;
    $mailer->Host = 'smtp.seusite.com.br';
    $mailer->SMTPAuth = true;
    $mailer->Username = $from;
    $mailer->Password = $password;
    $mailer->FromName = 'Seu Nome ou da Empresa '; //Nome que será exibido para o destinatário
    $mailer->From = $from; //Obrigatório ser a mesma caixa postal indicada em "Username"
    $mailer->AddAddress($email); //Destinatários
    $mailer->Subject = $subject; //Assunto;
    $mailer->IsHTML(true);
    $mailer->Body = $body;

    /*
    Faça suas validações
    */

    if (!$errName && !$errEmail && !$errSubject && !$errMessage) {
        if($mailer->Send()){
            $result='<div class="alert alert-success">Obrigado sr/&#40a&#41 '. $name .'! Entraremos em contacto consigo brevemente.</div>';
        } else {
            $result='<div class="alert alert-danger">Desculpa ocorreu um erro ao enviar a sua mensagem. Por favor tente mais tarde.</div>';
        }
    }
}

Browser other questions tagged

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