A project I'm working on requires me to build a mail form inside Joomla.
Some people on SO's chat suggested me using PHPmailer - but I noticed it is included in the joomla package and I don't really want to load it a second time, so I read some documentation about jFactory and I went that way.
I wanted to prsonalize the input fields with some javascript so I also used JS (specifically, the ajax call you can read below) to command the server to send me a mail. I have no idea if it is possible/easier to pass the mail fields to the mailer part if it resides in the same php file that the form is on, so I created a html form that calls the JS and a separate php with the actual sender:
- the html page contains the form and an empty div.
- the jQuery script modifies the form on the run (mainly changing dropdown options based on other choices) and does a variable validation when the Send button is clicked, in order to either send the mail (via ajax call to an exteal php file) or output an error message (i.e. "Mail address is required").
- the php file actually sends the mail using jFactory.
The whole thing works fine, but I'm going to need some more testing and I can't access the php echo from anywhere, including chrome's inspector.
I read that echo gets automatically saved to a "data" variable in the javascript, but this variable does not exist, and if I create it (possibly in the wrong place), it doesn't get populated.
I've tried (inside the php script) both the try/catch and the if($mail->Send()) approaches to generate the echo messages but, since I never run the php page in the browser, I have no idea if either is working.
How do I get the echo messages back to my JS so I can use console.log() to read it? I caot install extensions on Chrome, or use any other browser in the near future. But if that is the only option I have, I want to know it.
I will leave out headers, the _JEXEC function and a large part of the files in order to only show the php calling part.
js:
function send(datastr) {
$.ajax({
type: "POST",
url: "formsend.php",
data: datastr,
cache: false,
success: function(html) {
$("#systemMessage").fadeIn("slow");
$("#systemMessage").html('<span>Message successfully sent.</span>');
$("#systemMessage").css("background-color", "#e1ffc0");
setTimeout($("#systemMessage").fadeOut("slow"), 2000);
console.log(data);
}
});
}
php:
try {
define('JPATH_BASE', "../");
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require ('libraries/joomla/factory.php');
# require_once '../class.phpmailer.php';
$name=$_REQUEST['name'];
$subject=$_REQUEST['subject'];
$body=$_REQUEST['body'];
$from=$_REQUEST['from'];
$to = "mymail@me.com";;
# Invoke JMail Class
$mail = JFactory::getMailer();
# $mail->isSMTP; is not working
# Set sender array so that my name will show up neatly in your inbox
$sender = array($user, $name);
$mail->setSender($sender);
# Add a recipient
$mail->addRecipient($to);
$mail->ClearCCs();
$mail->ClearBCCs();
$mail->setSubject($subject);
$mail->setBody($body);
$mail->ClearAttachments();
$mail->ClearCustomHeaders();
# Send once you have set all of your options
$mail->Send();
echo "Message Sent OKn";
}
catch (Exception $e) {
echo $e->getMessage();
}
