php form not sending out email

Status
Not open for further replies.

gnsdeve2

New Member
Messages
3
Reaction score
0
Points
1
Hi,

I have created a PHP contact form using Ajax for my website but the problem is it is not sending out an email. I have checked the code over and over but I can't see where the problem is.

I have written a small piece of test code to send an email to my email address which works.

The code is:
I have included my correct email below in place of myemailaddress
<php
mail('myemailadress', 'Test', 'This is a test!');
?>

Anybody have any ideas and thanks in advance.
 

gnsdeve2

New Member
Messages
3
Reaction score
0
Points
1
Thank you inadim8956 for responding.

contacts.html

HTML:
<div class="span8">
                            <h2 class="title"><span>Get In Touch</span></h2>
                            <div class="contact_form">
                                <div id="note"></div>
                                <div id="fields">

                                    <!--<script type="text/javascript" src="https://form.jotformeu.com/jsform/70226386417356"></script>-->
                              
                                    <form id="ajax-contact-form" action="">
                                        <input class="span7" type="text" name="name" value="" placeholder="Name (required)" />
                                        <input class="span7" type="text" name="email" value="" placeholder="Email (required)" />
                                        <input class="span7" type="text" name="subject" value="" placeholder="Subject" />
                                        <textarea name="message" id="message" class="span8" placeholder="Message"></textarea>
                                        <div class="clear"></div>
                                        <input type="reset" class="btn dark_btn" value="Clear form" />
                                        <input type="submit" class="btn send_btn" value="Submit" />
                                        <div class="clear"></div>
                                    </form>
                                </div>
                            </div>                 
                        </div>


mail.php

PHP:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
// Where will you get the forms' results?
define("CONTACT_FORM", 'nigelsmith060@gmail.com');
?>


contact_process.php

PHP:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

include dirname(dirname(__FILE__)).'/mail.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
include 'email_validation.php';

$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);


$error = '';

// Check name

if(!$name)
{
$error .= 'Please enter your name.<br />';
}

// Check email

if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}

if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}

// Check message (length)

if(!$message || strlen($message) < 10)
{
$error .= "Please enter your message. It should have at least 10 characters.<br />";
}


if(!$error)
{
$mail = mail(CONTACT_FORM, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


if($mail)
{
echo 'OK';
}

}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}

}
?>


email_validation.php

PHP:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
function ValidateEmail($value)
{
    $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

    if($value == '') {
        return false;
    } else {
        $string = preg_replace($regex, '', $value);
    }

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

Hope you can help.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The From header (the sending e-mail address) must be an e-mail address that actually exists on your hosting account. "Spoofing" (sending an email that appears to be from someone else) is not permitted on the Free Hosting servers, and the mail will be discarded. You can use the Reply-To header field to capture the user's e-mail address and make replying easy.
 

gnsdeve2

New Member
Messages
3
Reaction score
0
Points
1
Hi inadim8956

As you suggested I replaced the Gmail email address that I used with the correct one on the server however it still does not work, I used the script below as mailtest.php in the root directory with my Gmail email which does work.

PHP:
<php
mail('myemailadress', 'Test', 'This is a test!');
?>


Thanks again in advance.
 

lylex10h

Active Member
Messages
982
Reaction score
71
Points
28
HTML:
<form id="ajax-contact-form" action="">
Isn't there suppose to be something in the action quotes?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The empty string is equivalent to "use the current URL", but it will only have meaning in any case if JS is disabled or fails to load. The root problem is the From header.
 

inadim8956

Member
Messages
107
Reaction score
0
Points
16
Hi,

In the mail function, when you use your gmail, it is the TO email, not the from

boolmail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Moreover, in mail.php, set an email created under your CPanel as advised by essellar .. Yet, I can't confirm that this is the root cause of the issue.

In addition to this, in the file contact_process.php, the function below is suspected to be the cause:

if(!$error)
{
$mail = mail(CONTACT_FORM, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());

Change the $email in the From header, to an email created under the CPanel. This is what essellar was talking about in regards to spoofing. I had this issue for a while until I found out about the anti-spoofing feature on X10 servers.
 
Status
Not open for further replies.
Top