I have a web form that calls a PHP PEAR Mail script via AJAX. When I invoke my script directly (by typing the full path to the script in the browser) it works. When I call the script from the web form it sends the first time, but not subsequent times.
I am using smtp. The hosting has SSL.
The web hosting company confirmed that the script is working ok, that the path to the PEAR package works etc.
I've tried many things and read a ton of questions and answers around this topic.
It seems that running the script from the browser direct does something that the jquery / ajax set-up is not doing. Gut feel (bad I know ..) says that something needs to be re-initialized when the form is used. Is that true?
The other clue is that the Ajax Post call success function does not generate the alert (see below). I write the echo to a text file on the sever - this is also intermittent.
Any help or ideas would be greatly received.
Here is the mail script:
<?php
/*
contact_form.php
*/
require "Mail.php";
$message = "";
$body = "";
$name = "";
$f_email = "";
$subject = "This is an email";
$name = $_POST['f_name']; // come from jquery
$f_email = $_POST['f_email']; // come from jquery
$message = $_POST['f_message']; // come from jquery
$body = "Sender name: " . $name . "\n";
$body .= "Senders email address: " . $f_email . "\n\n";
$body .= $message;
$from = "Mail from the web form <info@mydomain.com>";
$to = "Info <info@mydomain.com>"; // tried different email accounts including gmail
$host = "smtp.mydomain.com";
$username = "email-username";
$password = "the-password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
$fp = fopen('myerrs.txt', 'a');
fwrite($fp, $body);
fclose($fp);
echo "sent";
?>
Here is the call:
$('#contactform').submit(function(){
var f_name = $('#f_name').val();
var f_email = $('#f_email').val();
var f_message = $('#f_message').val();
// an alert works here and the variables are passed correctly.
$.post("thepathto/contact_form.php",
{
f_name:f_name,
f_email:f_email,
f_message:f_message
},
function(data){
alert('data = ' + data); // this alert does not work
});
});
Thanks.