| Table of Contents | This Page was viewed: 5795 Times |
In this example I will show you how to send emails from MindTouch (yes even core) utilizing a combination of DekiScript, jQuery, and PHP. Although this example will be a simple contact form you will easily be able to change it to serve other needs as well. I have only setup and tested this on an Ubuntu system but, hopefully this tutorial should lay the foundation for any operating system capable of PHP. Well Lets get started.
All discussion should be directed to this forum thread.
| Version | Date | Author | Description |
| 1.0 | 10/25/2009 | rberinger | Initial Release |
NOTE: The below instructions are for Ubuntu but may work on other systems as well.
For all the examples on this page, the code is shown before the working example. The code is shown with the syntax extension, and looks like this:
ContactForm()
This means that the actual code on the page should be enclosed in a DekiScript block. If you want to copy the code from this page, then use the same procedure as described in steps 2-4 above.
Just make a call to the template on the page you wish to create a contact form.
{{ ContactForm(); }}
| Name | Type | Default | Description |
| None. | |||
Below is a snapshot of what the form looks like. It has been done using id's, containers, and classes so customizing the look should be easy enough.

All discussion should be directed to this fourm thread.
Special thanks to the MindTouch crew not only for creating this great product and releasing it as open source but also giving us a place to share our results.
// Validation Icons
var validyes = 'http://deki.YouSite.com/@api/deki/files/582/=validyes.png';
var validno = 'http://deki.YourSite.com/@api/deki/files/581/=validno.png';
// Site non-Specific
var mySiteURI = site.uri;
<div id="emailcontainer">
<form id="emailform">
<table width="500px">
<tr>
<td class="label reqtext">
<label>'Your Name:'</label>
</td>
<td class="txtbox">
<div><input type="textbox" id="sendername" /><span class="validation" id="sendernamevalid"></span></div>
</td>
</tr>
<tr>
<td class="label reqtext">
<label>'Your Email:'</label>
</td>
<td class="txtbox">
<div><input type="textbox" id="senderemail" /><span class="validation" id="senderemailvalid"></span></div>
</td>
</tr>
<tr>
<td class="label reqtext">
<label>'Subject:'</label>
</td>
<td class="txtbox">
<div><input type="textbox" id="emailsubj" /><span class="validation" id="emailsubjvalid"></span></div>
</td>
</tr>
<tr>
<td class="label reqtext">
<label>'Message:'</label>
</td>
<td class="txtbox">
<div><textarea id="emailmessage" rows="10" cols="50" /><span class="validation" id="emailmessagevalid"></span></div>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center; ">
<input type="button" value="Submit" ctor="when($this.click) { SendNotice(); }" />
</td>
</tr>
</table>
</form>
</div>
<html><head>
<script type="text/javascript">"
var SendNotice = function() {
if(!ValidateText('#sendername', '#sendernamevalid')) {
return false;
} else {
var fromwho = $('#sendername').val();
}
if(!ValidateEmail('#senderemail', '#senderemailvalid')) {
return false;
} else {
var fromemail = $('#senderemail').val();
}
if(!ValidateText('#emailsubj', '#emailsubjvalid')) {
return false;
} else {
var theSubject = $('#emailsubj').val();
}
if(!ValidateText('#emailmessage', '#emailmessagevalid')) {
return false;
} else {
var theMessage = $('#emailmessage').val();
}
var theSiteURI = "..json.emit(mySiteURI)..";
$.post(theSiteURI + '/deki/sendemail/sendmail.php',{sendername:fromwho, senderemail:fromemail, subj:theSubject, message:theMessage});
alert('Thank you for Contacting us!');
return true;
}
var ValidateEmail = function isValidEmailAddress(fieldid, checkid) {
// Not the best email validation pattern but works for the most part.
var pattern = new RegExp(/^([a-zA-Z0-9_.\-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/);
var emailAddress = $(fieldid).val();
if(pattern.test(emailAddress) != 0) {
$(checkid).css({ 'background-image':'url("..json.emit(validyes)..")' });
} else {
$(checkid).css({ 'background-image':'url("..json.emit(validno)..")' });
}
return pattern.test(emailAddress);
}
var ValidateText = function(fieldid, checkid) {
// Here we are just checking that there is some text
// but you could change to whatever is needed
var thetext = $(fieldid).val();
if(thetext != 0) {
$(checkid).css({ 'background-image':'url("..json.emit(validyes)..")' });
return true;
} else {
$(checkid).css({ 'background-image':'url("..json.emit(validno)..")' });
return false;
}
}
"</script>
<style type="text/css">"
div td.reqtext { color:red; }
div td.label { font-weight:bold; text-align:right; }
div span.validation {
margin-top: 4px;
margin-left: 9px;
position: absolute;
width: 16px;
height: 16px;
}
"</style>
</head></html>
This form is setup to send an email to a particular address. If you need other addresses I would create a copy of the file make your changes and then update the SendNotice function in the template. This will make it harder for spammers to gather the email address.
If you want to send an email to various people then switch the logic between the $from and $to lines in the php file.
<?php
require_once "Mail.php";
$from = $_POST['sendername'] . " <" . $_POST['senderemail'] . ">";
$to = "Your Name <YourEmail@YourDomain.com>";
$subject = $_POST['subj'];
$body = $_POST['message'];
$host = "smtp.YourDomain.com";
$username = "";
$password = "";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Although this works great on my setup your mileage may vary.
| File | Version | Size | Modified | |
|---|---|---|---|---|
| ||||
| ||||
| ||||
| ||||
Copyright © 2011 MindTouch, Inc. Powered by