0

// Java script
var form = $('#main-contact-form');
form.submit(function(event){
 event.preventDefault();
 var form_status = $('<div class="form_status"></div>');
 var $form = $( this );
 $.ajax({
  type: 'POST',
       url: "sendemail.php",
    data:{
    Name:$form.find( "input[name='name']" ).val() , 
    Subject:$form.find( "input[name='subject']" ).val() , 
    Email:$form.find( "input[name='email']" ).val() , 
    message: $form.find("textarea[name=message]").val(),
  },
  beforeSend: function(){
   form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
  }
 }).done(function(data){
  form_status.html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
 });
});

I'm running the form on free plan on 000webhost (trying on herokuapp right now), they say that they support mail function. Php is separated in a different file called sendemail.php, I replace my email in php file but I can't receive any emails, what can be the issue? Any answers with code would be helpful

<!--Html form -->

<form id="main-contact-form" name="contact-form" method="post" action="#">
  <div class="form-group">
    <input type="text" name="name" class="form-control" placeholder="Name" required>
  </div>
  <div class="form-group">
    <input type="email" name="email" class="form-control" placeholder="Email" required>
  </div>
  <div class="form-group">
    <input type="text" name="subject" class="form-control" placeholder="Subject" required>
  </div>
  <div class="form-group">
    <textarea name="message" class="form-control" rows="8" placeholder="Message" required></textarea>
  </div>
  <button type="submit" class="btn btn-primary">Send Message</button>
</form>

sendemail.php file

$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'email@live.com';//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;

1 Answers1

0

Unless you are using javascript to set the form action or the mail handling code is in the same page then you need to set the form action to the mail handler. Also, the headers should be a string rather than an array when passed to mail

You might also wish to sanitize / validate some of what is sent in the POST request before sending mail??

Once the mail has been sent you should do something other than leave the user with a blank page - redirect to a thankyou page for example perhaps.

Contact page

<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" placeholder="Name" required>
    </div>
    <div class="form-group">
        <input type="email" name="email" class="form-control" placeholder="Email" required>
    </div>
    <div class="form-group">
        <input type="text" name="subject" class="form-control" placeholder="Subject" required>
    </div>
    <div class="form-group">
        <textarea name="message" class="form-control" rows="8" placeholder="Message" required></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Send Message</button>
</form>

sendemail.php

<?php

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['name'], $_POST['email'], $_POST['subject'], $_POST['message'] ) ){

        $name       = trim(stripslashes($_POST['name'])); 
        $from       = trim(stripslashes($_POST['email'])); 
        $subject    = trim(stripslashes($_POST['subject'])); 
        $message    = trim(stripslashes($_POST['message'])); 
        $to         = 'email@live.com';//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();

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

        exit( header( 'Location: index.php?mailsent='.$result ) );
    }
?>

Test the very basics of mail using:

mail( 'your.email@example.com','subject foo','message received' );

If you send a message to yourself using the most basic usage of mail and it arrives then all should be good with the code, maybe some tweaks. If nothing arrives in your mailbox then there is a problem and you would need to confirm with your host that the mail function is supported.

Professor Abronsius
  • 30,177
  • 5
  • 29
  • 43