0

creating a simple contact form using ajax and php. after submit , i receive the message sent success message, but the mail is not received.

is there any mistake in this code? help me out. thanks.

html form

<form method="post" class="myform" method="post" action='email.php'>
                           <div class="input-field col-md-4">
                        <input id="name" name="name" type="text" class="validate" required>
                        <label for="first_name">First Name</label>
                    </div>
                    <div class="input-field col-md-4">
                        <input id="email" type="email" name="email" class="validate" required>
                        <label for="mail">E-Mail</label>
                    </div>
                    <div class="input-field col-md-4">
                        <input id="subject" type="text" name="subject" class="validate" required>
                        <label for="subject">Subject</label>
                    </div>
                </div>
                <div>
                    <div class="input-field col-md-12">
                        <textarea id="message" class="materialize-textarea" name="message" required></textarea>
                        <label for="message">Message</label>
                    </div>
                </div>

            <input type="submit" name="send" value="Send"> <span class="output_message"></span>

            </form>

Ajax Code

<code>
<script>
$(document).ready(function() {
    $('.myform').on('submit',function(){

        // Add text 'loading...' right after clicking on the submit button. 
        $('.output_message').text('Sending...'); 

        var form = $(this);
        $.ajax({
            url: form.attr('action'),
            method: form.attr('method'),
            data: form.serialize(),
            success: function(result){
                if (result == 'success'){
                    $('.output_message').text('Message Sent Successfully!');  
                } else {
                    $('.output_message').text('Error Sending email!');
                }
            }
        });

        // Prevents default submission of the form after clicking on the submit button. 
        return false;   
    });
});
</script>
</code>

Php mail

<code>
<?php
if (isset($_REQUEST['name'],$_REQUEST['email'])) {

    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $message = $_REQUEST['message'];

    // Set your email address where you want to receive emails. 
    $to = 'to mail address';

    $subject = 'Contact Request From Website';
    $headers = "From: ".$name." <".$email."> \r\n";

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

    echo ($send_email) ? 'success' : 'error';

}
?>
</code>
  • working on local or web server ? – Demonyowh May 05 '17 at 04:37
  • can you var_dump the $_REQUEST variable please – Spoody May 05 '17 at 04:42
  • I hope this is just to hide the email from us: `$to = 'to mail address'`? Anyway, this could be many different things. `mail()` simply returns true if it successfully adds the email to the email queue, it doesn't mean that it was successfully sent. – Magnus Eriksson May 05 '17 at 04:45
  • What are you using to test mails on local environment? – Ali Niaz May 05 '17 at 04:51
  • Also, I see that you're setting the "from" address with the user supplied address. This is usually a bad idea and will often fail. Many mail servers do check where the email comes from and checks with the addresses mailserver (MX-record lookup) to see if the sending server is allowed to send emails from that address (checking things liks SPF and DKIM ([Read more here](https://blog.woodpecker.co/cold-email/spf-dkim/)). If the emails come from an "untrusted" sender, they either get bounced or get marked as spam. – Magnus Eriksson May 05 '17 at 04:55
  • on local server, i get message "Error Sending email!". in web server "Message Sent Successfully". but not received mail in inbox. – Rajkumar Arumugam May 08 '17 at 12:15
  • in local 'Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini '... on web, mail sent successfully, but not received. no errors showing.. – Rajkumar Arumugam May 08 '17 at 12:48

0 Answers0