Contact Form does not work PHP

0

I have a problem in the contact form of my site, I believe it is configured properly but it is not sending the email through the form, I wonder if it is an error in the code, it is the only one something that is missing to finish.

The code PHP looks like this:

  <?php

  $name       = @trim(stripslashes($_POST['name'])); 
  $from       = @trim(stripslashes($_POST['email'])); 
  $subject    = @trim(stripslashes($_POST['subject'])); 
  $message    = @trim(stripslashes($_POST['message'])); 
  $to           = "[email protected]";//replace with your email

  $headers   = array();
  $headers[] = "MIME-Version: 1.0";
  $headers[] = "Content-type: text/plain; charset=iso-8859-1";
  $headers[] = "From: {$name} <{$from}>";
  $headers[] = "Reply-To: <{$from}>";
  $headers[] = "Subject: {$subject}";
  $headers[] = "X-Mailer: PHP/".phpversion();

  mail($to, $subject, $message, $headers);

  die;

  ?>

And the html like this:

  <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
  <div class="form-group">
  <input type="text" name="name" class="form-control" required="required" placeholder="Nome">
  </div>
  <div class="form-group">
  <input type="email" name="email" class="form-control" required="required" placeholder="Email">
  </div>
  <div class="form-group">
  <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Sua Mensagem"></textarea>
  </div>                        
  <div class="form-group">
  <input type="submit" name="submit" class="btn btn-submit" value="Enviar">
   </div>
  </form>
    
asked by anonymous 09.04.2016 / 21:59

2 answers

2

$ subject (mail subject) $ message (the message). In your example in HTML you do not have the subject input and in the php script in line 5 ($ _ POST ['subject']) you search for it, solution proposal add another input in your html:

<div class="form-group">
  <input type="text" name="subject" id="subject" required="required" class="form-control" placeholder="Assunto do emial"></input>
</div>

Another alternative would be to add a default direct subject in php:

$subject = "Assunto default do email";

Additional:

Looking better at your code I see that in headers you use Array , but in the mail function the headers are String so you have to use the implode function to transform into String.

mail($to, $subject, $message, implode("\r\n", $headers));

I also noticed that you use $from this email that is used there has to be a valid email and created on your server that is hosted this script.

$from = "[email protected]";
    
10.04.2016 / 17:56
1

The problem is that you are doing an array of the header, which should be a string, see if doing as below the problem solves:

  $name       = @trim(stripslashes($_POST['name'])); 
  $from       = @trim(stripslashes($_POST['email'])); 
  $subject    = @trim(stripslashes($_POST['subject'])); 
  $message    = @trim(stripslashes($_POST['message'])); 
  $to         = "[email protected]";//replace with your email

  $headers   = array();
  $headers[] = "MIME-Version: 1.0";
  $headers[] = "Content-type: text/plain; charset=iso-8859-1";
  $headers[] = "From: {$name} <{$from}>";
  $headers[] = "Reply-To: <{$from}>";
  $headers[] = "Subject: {$subject}";
  $headers[] = "X-Mailer: PHP/".phpversion();
  /* 
     A função "implode" irá concatenar os
     valores do array e converter em uma
     string com a intersecção: "\r\n" 
  */
 $header = implode("\r\n", $headers);
 if (mail($to, $subject, $message, $header)) {
    echo "enviado";
 }
    
11.04.2016 / 15:15