Search code examples
phpajaxmailer

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="hello@mymail.com";

    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.


Solution

  • 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      = 'mail@example.com';
    
        $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);
    }
    ?>