4 Replies - 1080 Views - Last Post: 20 April 2012 - 01:59 PM

#1 Asperant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 11

Ajax And Php Mail() Function

Posted 20 April 2012 - 11:59 AM

There is a simple php-script:

<?php
  $email = "shift.mail@yandex.ru";
  $headers = "From: ".$_SERVER['SERVER_NAME']." <'no_reply@***.info'>\r\n";
  $headers = $headers."Content-type: text/html; charset=\"utf-8\"\r\n";
  $headers = $headers."Return-path: <'no_reply@***.info'>\r\n";
  $message = '<p>e-mail notify<p>';
  $subject = 'e-mail notify';
  $res =   mail( $email, $subject, $message, $headers );
?>


I need it for e-mail notify of my site. If I use it by url-field of my browser - it works fine (e-mail is sent). But if I try to use it by AJAX request, nothing works:

$.ajax({

        url: "email_notify.php",

    });


Also, I tried to use $.getJSON, $.post etc.

Could someone tell me, what is wrong with my code?

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Ajax And Php Mail() Function

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,574
  • Joined: 08-June 10

Re: Ajax And Php Mail() Function

Posted 20 April 2012 - 12:20 PM

PHP doesn't care in the slightest whether AJAX is being used or not. I see no reason that PHP script wouldn't work when requested by AJAX.

Have you verified that the AJAX request is executing successfully? If not, try using something like the Firebug addon in Firefox, or the developer tools for the other browsers, to monitor the request and see whether it is successfully requesting your PHP page.

Also, have your PHP page print out a message based on the success of the mail function and log that in your Javascript. It should help you see if there is a problem during the script execution.
Was This Post Helpful? 0
  • +
  • -

#3 Asperant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 11

Re: Ajax And Php Mail() Function

Posted 20 April 2012 - 01:20 PM

FireBug shows RED string of the request in the console. it seems to me an error appears when $.ajax tries to start the php-script. But nevertheless I can not to correct this error. I see, that there is an error, but where the cause is?
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,574
  • Joined: 08-June 10

Re: Ajax And Php Mail() Function

Posted 20 April 2012 - 01:48 PM

Have you tried using the "error" property of the jQuery.ajax() call?

To just print the errors, this should be fine:
$.ajax({
	url: "email_notify.php",
	error: function(xhr, status, err) {
		console.log("Error: " + status);
		if (err) {
			console.log(" - " + err.message);
		}
	}
});


Was This Post Helpful? 1
  • +
  • -

#5 Asperant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 11

Re: Ajax And Php Mail() Function

Posted 20 April 2012 - 01:59 PM

The request began work fine when I added parameters {async: false, type:'POST'} for $.ajax function
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1