jonny pixel
jonny pixel

Reputation: 267

AJAX/PHP emailer doesn't send email

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.

Upvotes: 0

Views: 464

Answers (3)

Mihai Iorga
Mihai Iorga

Reputation: 39704

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);
}
?>

Upvotes: 2

Ja͢ck
Ja͢ck

Reputation: 173582

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.

Upvotes: 1

kushalbhaktajoshi
kushalbhaktajoshi

Reputation: 4678

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;}
    });   

});

Upvotes: 1

Related Questions