Search code examples
phpjqueryemailmime

Mail/mime email not sent when displaying form with jquery


I have a relatively basic email form put together which I intend to email to a recipient using mail mime. This approach has worked for me in the past when the form was displayed on its own page i.e. a contact page.

Here I am using jquery to display a hidden div which contains my form from the home page. The problem is that although my validations pass the email is not being sent. I am at a loss for why and hope someone here can help me out!

The relevant php is as follows:

$path = get_include_path() . PATH_SEPARATOR . '/home/host/php';
set_include_path($path);
include('Mail.php');
include('Mail/mime.php');
$recipient_email = 'somebody@somewhere.com';
//validations are here

//send the email
$name = $_POST['name'];
$visitor_email = $_POST['from'];
$subject = $_POST['subject'];
$user_message = $_POST['message'];
$to = $recipient_email;
$from = $visitor_email;
$text = "From: $name \n Subject: $subject \n Message $user_message";

$message = new Mail_mime(); 
$message->setTXTBody($text); 
$body = $message->get();
$extraheaders = array("From"=>$name, "To"=>$recipient_email, "Subject"=>$subject, "Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
echo "Success";

The jquery to display the form looks like this:

jQuery.fn.fadeToggle = function(speed, easing, callback) {
return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};

$(document).ready(function() {
$('#tellfriend').hide();
$('li a.email, #tellfriend a.close').click(function() {
$("#tellfriend").fadeToggle('slow');
});
}); 

One of my thoughts (and I'll experiment with it) is that because when the link is clicked to display form, the link is appended to the URL and so it may affect my form action? Not sure.. Any help is appreciated!


Solution

  • I was able to solve my problem by following a previous question I eventually found on Stack Overflow HERE

    It turns out that I had to specify an outgoing mail server in order to send the form out. Still, it is odd that I have had it working in the past where I didn't need to follow such steps.