Contact Form Does Not Work PHP

Asked

Viewed 526 times

0

I’m with a problem in the contact form of my site, I believe it is configured correctly but it is not sending the email through the form, I would like to know if it is error in the code, it is the only thing that is missing to finish.

The code PHP is 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 thus:

  <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>
  • This PHP code is inside the file sendemail.php? and by the way the name is either sendmail.php (without e)? Another thing, I don’t see the field subject...

  • that sendemail.php is the one in both

  • @Sergio you know what can be?

  • 1

    In your HTML you do not have the "Subject" field and without this information the PHP mail function does not work!

  • and how it could work @igormello ?

2 answers

2


To run the PHP mail function the minimum information for its operation is $to (to whom to send), $Subject (email subject) and $message (the message). In your example in HTML you do not have the Subject input and in php script in line 5 ($_POST['Subject']) you search for it, solution proposal add one more 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 subject directly in php:

$subject = "Assunto default do email";

Additional:

Looking better your code saw that in the headers you use a Array, only that in the mail function the headers are a String then you have to use the function implode to transform into String.

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

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

$from = "[email protected]";
  • 2

    @Victor Gomes helpful answer?

  • Buddy, it still doesn’t work

  • I tried both ways, putting the new input and trying by default

  • you have any idea what it might be?

  • 1

    @Victorgomes generates some error?

  • No, it does not generate, apparently well but the email does not arrive and I have tried to send a normal email to the address where this form and arrives, the problem is when you send the message through the form, there does not work

  • 1

    @Victorgomes improved a little the answer takes a look!

  • Problem solved, thank you Igor

Show 3 more comments

1

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

  $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";
 }

Browser other questions tagged

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