0

Here is my PHP mailer code:

if ($_POST) {
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $budget=$_POST['budget'];
    $text=$_POST['text'];
    $to="[email protected]";

    mail($to, "Piece of Cake = ".$subject, $text, "Budget:".$budget, "From:".$email );
}

And here is my AJAX email function:

$(function (){
    $('button#send_brief').click(function(){
        var email = $('#brief_email');
        var subject = $('#brief_type');
        var budget = $('#brief_budget');
        var text = $('#brief_comments');
        var emailerData = 'email' + email +'&subject' + subject + '&budget' + budget+ '&text' + text;

        if(text=='') {
            $('#brief_email').css({'background-color':'red'});
         }
         else {

        $.ajax({
            type: "POST",
            url:"emailer.php",
            data: emailerData,
            success: function(){
                $('.success').fadeIn(1000);
                setTimeout (function(){
                $('.brief').slideUp({
                    duration: 1000,
                    easing: 'easeInSine'});}
                            ,2000);
                setTimeout (function(){
                $('#brief').fadeIn();}
                            ,3000);
                setTimeout (function(){
                $('.success').fadeOut(1000);}
                            ,2000);
            }

        });

        return false;}
    });   

});

And here my form:

<form method="POST" action="" >
<fieldset>

            <label for="brief_email">Your email</label>
            <input type="text" id="brief_email" placeholder="your contact email here"/>
             etc ...
</fieldset>
</form>
<button id="send_brief">Send it</button>

First of all when I leave all fields empty, it still show me Success screen. And second of all I don't get emails, that it sends, even when I enter valid data.

I have reviewed the code many times and seems OK to me.

Please help me find where I am wrong.

1
  • It will always show success screen if the AJAX is ok. Your problem is in the php mailer code .. try to access it directly with already added variables and see the error .. point error here Commented Aug 31, 2012 at 10:35

3 Answers 3

2

your problem is in the AJAX construct, you lack getting value with: val()

var email = $('#brief_email').val();
var subject = $('#brief_type').val();
var budget = $('#brief_budget').val();
var text = $('#brief_comments').val();

Without val() you will send an object to the PHP script and not a value.

And vars construct you lack =

var emailerData = 'email=' + email +'&subject=' + subject + '&budget=' + budget+ '&text=' + text;

Also your mail() construct is bad, you cannot send any other values than it requires check mail(). And you check for $_POST, $_POST is always true, you need to check at least one post value.

<?php

if (isset($_POST['email'])) {
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $budget=$_POST['budget'];
    $text=$_POST['text'];
    $to      = '[email protected]';

    $subject = "Piece of Cake = ".$_POST['subject'];
    $message = "New message from website

    Email: $email
    Budget: $budget
    Comments: $text";

    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to check the email sent

if ($_POST) {
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $budget=$_POST['budget'];
    $text=$_POST['text'];
    $to="[email protected]";

    if(mail($to, "Piece of Cake = ".$subject, $text, "Budget:".$budget, "From:".$email )) echo '1';
    else echo '0';

}

Check the email sent response in ajax response

$(function (){
    $('button#send_brief').click(function(){
        var email = $('#brief_email');
        var subject = $('#brief_type');
        var budget = $('#brief_budget');
        var text = $('#brief_comments');
        var emailerData = 'email' + email +'&subject' + subject + '&budget' + budget+ '&text' + text;

        if(text=='') {
            $('#brief_email').css({'background-color':'red'});
         }
         else {

        $.ajax({
            type: "POST",
            url:"emailer.php",
            data: emailerData,
            success: function(result){
            if(result == 1){
    $('.success').fadeIn(1000);
                setTimeout (function(){
                $('.brief').slideUp({
                    duration: 1000,
                    easing: 'easeInSine'});}
                            ,2000);
                setTimeout (function(){
                $('#brief').fadeIn();}
                            ,3000);
                setTimeout (function(){
                $('.success').fadeOut(1000);}
                            ,2000);
}
            }

        });

        return false;}
    });   

});

Comments

1

Your email sending code has two problems:

mail($to, "Piece of Cake = ".$subject, $text, "Budget:".$budget, "From:".$email );

First problem is that you're not checking the return value:

if (false === mail( ... )) { echo "Yelp! Mail not sent"; }

Second problem is that your email headers should be combined into the fourth parameters:

mail($to, "Piece of Cake = ".$subject, $text, "Budget: $budget\r\nFrom: $email");

That's because the fifth parameter is meant for sendmail, it's not yet another field for email headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.