1

I need to send email asynchronously.

For that I have a jQuery ajax client and PHP backend to actually send eMail, still AJAX's success method is not being called. I understand it is simple, but yet doesn't seem to be working. Can somebody please take a look at my code and see what I'm doing wrong? Really appreciate it.

$.ajax({
    type: "POST",
    url: "mail.php",
    data: {name: "manu", email: "abc@abc.com"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {  
        alert('mail sent');  
        $('#divToBeWorkedOn').html(msg);
    }
});

and I have mail.php

<?php

header("Content-type: application/json");
$name = trim($_POST['name']);
echo $name
$email = trim($_POST['email']);

$msg="my email";
$emailTo = 'myemail@gmail.com';
$subject = 'Contact Form Submission from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nMessage: $message";
$headers = 'From: Saddi website <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);
echo 1;
?>
Preview
  • 35,317
  • 10
  • 92
  • 112
CalZone
  • 1,701
  • 1
  • 17
  • 29
  • What HTTP response code do you get for your AJAX call? Do yo have any errors showing up in web server logs? – Mike Brant Nov 27 '13 at 21:52
  • There are already lots of questions on this topic - I answered one [here](http://stackoverflow.com/questions/17021070/send-email-on-click/17021597#17021597) with a simple example. – Wez Nov 27 '13 at 21:53
  • @CalZone Typically you can track all network activity the browser initiates in the "Net" or "Network" panel of your favorite browser-based web developer tools. – Mike Brant Nov 27 '13 at 21:53
  • add error:function(msg){console.log(msg)} to your ajax call to see the error – Steve Nov 27 '13 at 21:54
  • 1
    you set `dataType` json, but don't return `json`. Will cause parse error – charlietfl Nov 27 '13 at 21:54
  • I can also say that, at a very minimum, you won't be able to rely on `$_POST` as you are using `application/json` ContentType header. `$_POST` is not populated in this case. You will need to read POSTed data from PHP raw input. This is other question I had provided answer to on how to do this. http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined/15485690#15485690 – Mike Brant Nov 27 '13 at 21:55
  • @Wezly Thanks for your link, will probably solve my problem, though its an email, and mine is more of a contact form. – CalZone Nov 27 '13 at 21:59
  • 1
    While that is not part of your question, I strongly recommend you to read some articles about the _dos and don'ts_ of email web-forms. The way you create the subject and headers makes it (if the MTA e.g. supports BCC fields) extremely easy to use your form to submit spam. – t.niese Nov 27 '13 at 21:59
  • @charlietfl Yup. I think thats what it is. Thanks for bringing that up. – CalZone Nov 27 '13 at 22:02
  • @t.niese Yes I understand, but as of now I'm still trying to understand basics about ajax. There is long time before anything goes to production. – CalZone Nov 27 '13 at 22:04
  • 1
    @CalZone I hope so, there are still many web hosting service, that allow the usage of `bcc` headers (mainly to e.g. support a resource saving way for sending newsletters). In that case - with your example - it would be sufficient to set `email` to something like this: `\r\nmail1@example.com;mail2@example.com;[some more mail addresses];mail10000@example.com;\r\n` and your form would submit thousands of emails to any foreign address. – t.niese Nov 27 '13 at 22:12

1 Answers1

1

I see 2 problems.

  1. you're echoing just 1, its not JSON. So in your PHP code do json_encode("msg"=>"1");

  2. In my experience, if you have this

header("Content-type: application/json"); OR header("Content-type: json/text");

You won't get any result in jquery success or error blocks.

success: function(msg) {  
    alert(msg);  
}

the msg would return null value (or nothing). The alert will happen but will show something like object object.

user2443329
  • 118
  • 4