Jump to content


Photo

php jquery email send

php jquery email send

  • Please log in to reply
11 replies to this topic

#1 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 22 June 2014 - 12:29 PM

Hi I have a following code, validation part and jquery works perfectly in the code but it dosent send the email 

I have jquery.js file in the head section of html and it work perfectly

 

contact.php

 
<div id="emailcontainer">

<div id="ack"> </div>

<div id="emailsend"><img src="images/emailsend.gif"></img><br> <p>We will get back to you soon</p></div>



<form id="contactform">

<div>

<label for="name">NAME:</label>

<input type="text" name="name" id="name" placeholder="John Doe" />

</div>

<div>

<label for="email">EMAIL:</label>

<input type="text" name="email" id="email" placeholder="JohnDoe@example.com" />

</div>

<div>

<label for="comment">MESSAGE:</label>

<textarea name="comment" rows="10" cols="50" id="comment"></textarea>

</div>

<input type="submit" name="submit" value="SEND" id="submit" />

</form>

</div><!---div emailcontainer end here--->

</div><!---div main end here--->
 

this page does the validation part from php

 

test.php

 
 
<?php

 if(isset($_POST["submit"])) {



    $errors = array();

    $name = $_POST['name'];

    $email = $_POST['email'];

    $comment = $_POST['comment'];

    $to = "reboosttechnologies@gmail.com";

    $from = $_POST['email'];

    $headers = "From: $from";

    $flag = false;

    $success = mail($to,$comment,$headers);

    $emailReg = '/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ ';



    if(!($name) || !($email) || !($comment)){

        if(!$name){

            $errors[]="Missing Name";

            }

        if(!$email){

            $errors[]="Missing Email";

            }

        if(!$comment){

            $errors[]="Missing Comment";

            }

            $json_response = json_encode($errors);

                 

        }elseif(!preg_match($emailReg,$email)){

            $errors[] = "Not a valid email address";

            $json_response = json_encode($errors);

            }elseif($success){

                  $flag = true;



                return $flag;}else{

                

                $json_response = json_encode("Thanks for your comment");

                }

                echo $json_response;





}

?>

this file does the jquery validation without page refresh

function.js

 
$(function(){

    $("#emailsend").hide();

    $("#submit").click(function(event) {

   event.preventDefault();

        var emailReg = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;

        var errors = false;

        var name = $('[name=name]').val();

        var email = $('[name=email]').val();

        var comment = $('[name=comment]').val();

        $('.errors').remove();

        if($("#name").val() == ""){

            $("#name").after("<span class= 'errors'> Missing Name </span>");

            errors = true;

            }

        if($("#email").val() == ""){

            $("#email").after("<span class= 'errors'> Missing Email </span>");

            errors = true;

            }else if(!emailReg.test($('#email').val())){

                $("#email").after("<span class= 'errors'> Not valid Email </span>");

                errors = true;

                }

        if($("#comment").val() == ""){

            $("#comment").after("<span class= 'errors'> Missing Comment </span>");

            errors = true;

            }

if(errors == true) {

     return false;

  } else {

     input_data = {

        "name"   : name,

        "email"  : email,

        "comment": comment,

        "submit" : true

     }

    $.ajax({

       type: 'post',

       url: 'test.php',

       data: input_data,

       dataType: 'json',

       success: function( msg ) {

          $("#ack").html(msg);

          $("#contactform").delay(500).slideUp(500);

          $("#emailsend").show(700);

       },

       error: function() {

          $("#ack").html('ERROR!');

       }

    });  

}





                

        

        });

    });

 
 


#2 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 30 June 2014 - 02:00 PM

So many views without single help



#3 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 436 posts
  • LocationMinnesota

Posted 30 June 2014 - 04:06 PM

You're missing a few hearders that an email needs to effectively be delivered.  Also you are blindly sending the email BEFORE even checking the variables that were posted.  You need to only send the email once you have done all the validation checks and ONLY if they all pass, so you need to rearrange your code.  The best way to send email in php is to use a php library like phpmailer which will automatically assign the proper headers to "help" with delivery.  Granted delivery is a whole other issue in itself and is a long subject to discuss.

 

This is a function I used to send emails before switching to phpmailer.  It has the most basic headers needed to send an email.  You can modify it to your needs.  The function sends the email in HTML format, not plain text, so you can google the proper header for plain text if you want that option.

// Builds an email from given parameters
function buildEmail($to, $subject, $body, $from, $reply = NULL)
{
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
	$headers .= 'From: "'.$from.'" <'.$from.'>' . "\r\n";
	if(!empty($reply)){$headers .= 'Reply-To: '.$reply.'' . "\r\n";}
	
	return (mail($to, $subject, $body, $headers) === TRUE) ? TRUE : FALSE;
}


You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com


#4 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 01 July 2014 - 09:16 AM

Thanks for the message fastsol:

I have modified the test page code as

 

<?PHP

if(isset($_POST["submit"])) {

    $errors = array();
    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    $emailReg = '/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ ';

    if(!($name) || !($email) || !($comment)){
        if(!$name){
            $errors[]="Missing Name";
            }
        if(!$email){
            $errors[]="Missing Email";
            }
        if(!$comment){
            $errors[]="Missing Comment";
            }
            $json_response = json_encode($errors);
                 
        }elseif(!preg_match($emailReg,$email)){
            $errors[] = "Not a valid email address";
            $json_response = json_encode($errors);
            
                
                $json_response = json_encode("Thanks for your comment");
                function buildEmail($to, $subject, $body, $from, $reply = NULL)
{
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
    $headers .= 'From: "'.$from.'" <'.$from.'>' . "\r\n";
    if(!empty($reply)){$headers .= 'Reply-To: '.$reply.'' . "\r\n";}
    $to = "mymail@gmail.com";
    $subject ="Msg for you";
    return (mail($to, $subject, $body, $headers) === TRUE) ? TRUE : FALSE;
}
                }
                echo $json_response;

}
?>

 

still dosen't work



#5 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 436 posts
  • LocationMinnesota

Posted 01 July 2014 - 09:38 AM

Do you understand how to use a function?  You don't put the function I gave you in the code block you already had and expect it to work.  The function should be put either before your first if() or in a separate file that is included before the if().  Then you call the function in your if() at the appropriate time and provide the parameters it needs to run.

 

You should check out some info I have on validating a form properly, at least the way I like to do it, that is logical to understand.  There are some here that will debate my use of some vars in the script but they do serve a specific purpose on how it works.  If nothing else, look at the structure and understand how yours can be improved to accomplish your end result and the flaws you currently have.

http://amecms.com/ar...m-the-right-way


You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com


#6 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 01 July 2014 - 03:49 PM

fastsol

Sir I am through with validation part, the only thing is bothering me here is where i have to put code to send the email

so where i have to put the function so it will sen the mail

 

thanks



#7 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 03 July 2014 - 03:20 PM

Hi Fastsol,

 

I did following changes to the test.php file but it still dosen't work

 

<?PHP

if(isset($_POST["submit"])) {

    $errors = array();
    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    $emailReg = '/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ ';


    if(!($name) || !($email) || !($comment)){
        if(!$name){
            $errors[]="Missing Name";
            }
        if(!$email){
            $errors[]="Missing Email";
            }
        if(!$comment){
            $errors[]="Missing Comment";
            }
            $json_response = json_encode($errors);
                 
        }elseif(!preg_match($emailReg,$email)){
            $errors[] = "Not a valid email address";
            $json_response = json_encode($errors);
            
                
                
                
                $to = "myemail@gmail.com";
                $from = "demo@yahoo.com";
                $headers  = 'MIME-Version: 1.0' . '\n';
                $headers .= 'From:$from' . '\n';
                $subject = 'Contact From Submission\n';
                $body = 'Name: ' .$name . '\n';
                $body .='Email: ' .$email . '\n';
                $body .='Message: ' .$message . '\n';
                if(mail($to,$subject,$body,$headers)){
                    $return['error'] = false;
                    //$json_response = json_encode("Thanks for your comment");
                    $return['msg'] ="<p>Thanks for your comment</p>";
                    echo json_encode($return);
                    }else{
                        $return['error'] = false;
                    //$json_response = json_encode("Thanks for your comment");
                    $return['msg'] ="<p>Thanks for your comment</p>";
                    echo json_encode($return);
                        //$json_response = json_encode("Sorry email not send try again");
                        }
                
                }
                echo $json_response;

}
?>
 



#8 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 436 posts
  • LocationMinnesota

Posted 03 July 2014 - 05:41 PM

You had many things wrong in that code.  I editted it and included my mailing function I gave you and tested the whole thing on my server and it works as expected.  You just need to remove the form at the bottom after you test it.

<?php
// Builds an email from given parameters
function buildEmail($to, $subject, $body, $from, $reply = NULL)
{
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
	$headers .= 'From: "'.$from.'" <'.$from.'>' . "\r\n";
	if(!empty($reply)){$headers .= 'Reply-To: '.$reply.'' . "\r\n";}
	
	return (mail($to, $subject, $body, $headers) === TRUE) ? TRUE : FALSE;
}

if(isset($_POST["submit"])) 
{

    $errors = array();
    $name = htmlentities($_POST['name']);
    $email = $_POST['email'];
    $comment = htmlentities($_POST['comment']);
    $emailReg = '/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/';


    if(empty($name) || empty($email) || empty($comment))
    {
        $errors[] = 'All fields are required.';
	}
	elseif(!preg_match($emailReg, $email))
	{
	    $errors[] = "Not a valid email address";
	}
	
	if(!empty($errors))
	{ echo json_encode($errors); }
	else
	{      
        $to = "myemail@mydomain.com"; // Edit this to your actual email address you want the emails sent to.
        $from = $email;
        $subject = 'Contact From Submission';
        
        $body  = 'Name: ' .$name . '<br>';
        $body .= 'Email: ' .$email . '<br>';
        $body .= 'Message: ' .$comment . '<br>';
        
        if(buildEmail($to, $subject, $body, $from, $from) === TRUE)
        {
            $return['error'] = false;
            $return['msg'] = "Thanks for your comment";
            echo json_encode($return);
        }
        else
        {
            $return['error'] = true;
            $return['msg'] ="Sorry email not send try again";
            echo json_encode($return);
         }
    }
}

?>

<form action="" method="post">
	<input type="text" name="name">
	<input type="text" name="email">
	<textarea name="comment"></textarea>
	<input type="submit" name="submit" value="Submit">
</form>


Edited by fastsol, 03 July 2014 - 05:42 PM.

You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com


#9 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 04 July 2014 - 02:11 PM

Hi Fastsol

 

I have replace the code with my email and also remove the form as

per your suggestion but it still doesn't work.



#10 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 436 posts
  • LocationMinnesota

Posted 05 July 2014 - 08:44 AM

Then you have something else going on.  You need to be more specific than "it doesn't work".  That statement doesnt tell us anything.  What doesn't it do?  Which json values are returned? 


You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com


#11 prash91

prash91

    Member

  • Members
  • PipPip
  • 11 posts

Posted 05 July 2014 - 02:28 PM

Hi Fastsol

 

following is the code, it shows me the message "Thanks for the email" but email dosen't come in my inbox

<?php
// Builds an email from given parameters
function buildEmail($to, $subject, $body, $from, $reply = NULL)
{
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
    $headers .= 'From: "'.$from.'" <'.$from.'>' . "\r\n";
    if(!empty($reply)){$headers .= 'Reply-To: '.$reply.'' . "\r\n";}
    
    return (mail($to, $subject, $body, $headers) === TRUE) ? TRUE : FALSE;
}

if(isset($_POST["submit"]))
{

    $errors = array();
    $name = htmlentities($_POST['name']);
    $email = $_POST['email'];
    $comment = htmlentities($_POST['comment']);
    $emailReg = '/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/';


    if(empty($name) || empty($email) || empty($comment))
    {
        $errors[] = 'All fields are required.';
    }
    elseif(!preg_match($emailReg, $email))
    {
        $errors[] = "Not a valid email address";
    }
    
    if(!empty($errors))
    { echo json_encode($errors); }
    else
    {      
        $to = "myemail@gmail.com"; // Edit this to your actual email address you want the emails sent to.
        $from = $email;
        $subject = 'Contact From Submission';
        
        $body  = 'Name: ' .$name . '<br>';
        $body .= 'Email: ' .$email . '<br>';
        $body .= 'Message: ' .$comment . '<br>';
        
        if(buildEmail($to, $subject, $body, $from, $from) === TRUE)
        {
            $return['error'] = false;
            $return['msg'] = "Thanks for your comment";
            echo json_encode($return);
        }
        else
        {
            $return['error'] = true;
            $return['msg'] ="Sorry email not send try again";
            echo json_encode($return);
         }
    }
}

?>

Edited by mac_gyver, 05 July 2014 - 03:13 PM.
code tags please


#12 fastsol

fastsol

    Advanced Member

  • Members
  • PipPipPip
  • 436 posts
  • LocationMinnesota

Posted 05 July 2014 - 03:11 PM

Alright, first off you need to learn to use the forum code tags to post code, it's the rules of the forum and it makes it a lot easier for anyone to read the code.  The code tag button is the <> just under the smiley face in the reply textarea.

 

Now, "Thanks for the email" doesn't tell me anything since that text is not in the code that I gave you, so I have no idea how or where that is coming from.  You really need to start thinking logically if you want anyone to help figure this out with you.  You need to start doing basic diagnostics to trace down where in the code the fault is before you post saying "it doesn't work".

 

But, have you checked your spam box for the email?  I said it before, you really should use a library for this kind of thing, like phpmailer.  It might seem difficult to use, but it's honestly not.  They have working examples on the site so you can see how to piece the code together.  At this point from what you have told me, the only thing to know if it actually sent the email via the script is if the json that was returned has "Thanks for your comment" as the msg array item.  If it's returning that message then the php is working correctly and the issue lies in the server or your email program.  As I said before, delivery to an email inbox is a whole nother subject and pretty in depth.  Email in todays market doesn't just "appear" cause you asked it to, it goes through a huge amount of filtering and BS before it's even considered for delivery.  If you don't have the basics of the info a email program wants to see, it won't even think about delivering it and you likely won't see it in your spam box either.

 

If you want a ready made contact form that uses phpmailer and ajax submission, I distribute one at - http://amecms.com/ar...with-validation


You never know everything, I learn everday!

http://amecms.com

http://amewebdesigns.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com