0

I send email from my website using HTML and Ajax, the email is sent sometimes but 90% of the time it doesn't deliver. the messages are not even in the email's spam. this is my code I have it on my website for about 2 years but i never tought it's not working please help :

html :

       <form id="contact-form" class="contact-form" action="#">
            <p class="contact-name">
                <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
            </p>
            <p class="contact-email">
                <input id="contact_email" type="text" placeholder="Email Address" value="" name="email" />
            </p>
            <p class="contact-message">
                <textarea id="contact_message" placeholder="Your Message" name="message" rows="10" cols="40"></textarea>
            </p>
            <p class="contact-submit">
                <a id="contact-submit" class="submit" href="#">Send Your Email</a>
            </p>

            <div id="response">

            </div>
        </form>

Ajax (fichier main.js):

$("#contact-submit").on('click',function() {
    $contact_form = $('#contact-form');

    var fields = $contact_form.serialize();

    $.ajax({
        type: "POST",
        url: "http://musamr.com/wp-content/themes/musamr/include/php/contact.php",
        data: fields,
        dataType: 'json',
        success: function(response) {

            if(response.status){
                $('#contact-form input').val('');
                $('#contact-form textarea').val('');
            }

            $('#response').empty().html(response.html);
        }
    });
    return false;
});

contact.php

<?php
/*
* Contact Form Class
*/
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'monemail@gmail.com'; // Your Email
$message_min_length = 5; // Min Message Length

class Contact_Form{
    function __construct($details, $email_admin, $message_min_length){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Contact from Your Website'; // Subject 
        $this->message = stripslashes($details['message']);

        $this->email_admin = $email_admin;
        $this->message_min_length = $message_min_length;

        $this->response_status = 1;
        $this->response_html = '';
    }

    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }

    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid e-mail address</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = 0;
        }
    }

    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "From: ".$this->name." <".$this->email.">\r\n"
            ."Reply-To: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . phpversion());

        if($mail)
        {
            $this->response_status = 1;
            $this->response_html = '<p>Thank You!</p>';
        }
    }

    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}

$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>
  • Is, assuming you have setup your php email setting prior to using them... then what does your http server log error file say? – arkascha Nov 14 '18 at 14:56
  • have you looked at your mail logs? Many mail providers these days will just blacklist mail coming off private servers for no particular reason - there is a very good chance your mail is simply being rejected by the recipient. This is especially likely if some is going through but not others. software doesn't typically intermittently fail without some knowable factor - in this case the most likely being the recipient mail server – billynoah Nov 14 '18 at 22:56

1 Answers1

0

The mail function is extremely unreliable. You should consider using a SMTP server & a library like PHPMAILER to make integration easier.

phpmailer: https://github.com/PHPMailer/PHPMailer

The mail function will attempt to send but more times than not it will go into the users spam folder or the destination may reject it.

Also instead of that regex you can use filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)

Chad
  • 733
  • 3
  • 19
  • do you mean phpmailer : https://github.com/PHPMailer/PHPMailer ?? – Athchoum Nov 14 '18 at 17:52
  • Yes I meant to say PHPMailer, answer updated. – Chad Nov 14 '18 at 18:11
  • thank you so much I'll look into it :) – Athchoum Nov 14 '18 at 21:12
  • Hello, I finally got it working using SMTP (PHPMailer), but i keep getting a security notification from google with an error in my form : "Errors occur. Please try again later." – Athchoum Nov 18 '18 at 17:41
  • Google the error, I can't recall what it is exactly but I'm pretty sure you need to enable something along the lines of disable security for third party apps. Alternatively, you can register for Zoho & use their SMTP for free. – Chad Nov 19 '18 at 13:14