Skip to content

Instantly share code, notes, and snippets.

@carsten
Created May 11, 2009 14:35
Show Gist options
  • Save carsten/109999 to your computer and use it in GitHub Desktop.
Save carsten/109999 to your computer and use it in GitHub Desktop.
Let someone send you an email. Based on Symphony 1.7 send_email event by Alistair Kearney, http://www.pointybeard.com, alistair@pointybeard.com.
<?php
Class eventSend_Email Extends Event{
function __construct($args = array()){
parent::__construct($args);
}
public static function about(){
return array(
"name" => "Send Email",
"description" => "Send someone an email. Based on Symphony 1.7 send_email event by Alistair Kearney, http://www.pointybeard.com, alistair@pointybeard.com.",
"author" => array("name" => "Carsten de Vries",
"website" => "http://www.vrieswerk.nl",
"email" => "carsten@vrieswerk.nl"),
"version" => "0.1",
"release-date" => "2009-05-11",
"trigger-condition" => "action[send-email] field",
"recognised-fields" => array(
array("name", true),
array("email", true),
array("subject", true),
array("message", true),
array("ip", true),
// array("saynotospam", true),
array("recipient-username", true))
);
}
public static function allowEditorToParse(){
return true;
}
public static function documentation(){
return '
<h3>Description</h3>
<p>This event allows you to send an e-mail without saving the message like the standard save message event does.</p>
<p>Based on Symphony 1.7 send_email event by Alistair Kearney, http://www.pointybeard.com, alistair@pointybeard.com.</p>
<h3>Usage</h3>
<p>Include the event on your page and make sure that you include the default required fields (name, email, subject, message).</p>
<p>Note: error reporting could be improved. The event returns either success or failure to the page XML, but does not specify what is wrong.</p>
<p>I have commented out support for the saynotospam field. Remove the comments in the source code and include a checkbox with
name="field[\'saynotospam\]" and value="idislikeit" to include an anti-spam measure. Change the name of the saynotospam field
throughout this event to improve protection against spammers.</p>
<h3>Example Front-end Form Markup</h3>
<p>This is an example of the form markup you can use on your frontend:</p>
<pre class="XML"><code>&lt;form method="post" action="">
&lt;label>Name
&lt;input name="fields[name]" type="text" />
&lt;/label>
&lt;label>Email
&lt;input name="fields[email]" type="text" />
&lt;/label>
&lt;label>Subject
&lt;input name="fields[subject]" type="text" />
&lt;/label>
&lt;label>Message
&lt;textarea name="fields[message]" rows="15" cols="50">&lt;/textarea>
&lt;/label>
&lt;label>Anti-spam
&lt;input name="fields[saynotospam]" type="checkbox" value="idislikeit" />
&lt;/label>
&lt;input name="action[send-email]" type="submit" value="Submit" />
&lt;/form></code></pre>';
}
public function load(){
if(isset($_POST['action']['send-email'])) return $this->__trigger();
return;
}
protected function __trigger(){
$result = new XMLElement("send-email");
$fields['recipient_username'] = $_POST['recipient-username'];
$fields['email'] = $_POST['email'];
$fields['name'] = $_POST['name'];
// $fields['saynotospam'] = $_POST['saynotospam'];
$fields['ip'] = $_SERVER['REMOTE_ADDR'];
$fields['subject'] = stripslashes(strip_tags($_POST['subject']));
$fields['message'] = stripslashes(strip_tags($_POST['message']));
$fields = array_map("trim", $fields);
$cookie = new XMLElement("cookie");
$cookie->appendChild(new XMLElement("name", $fields['name']));
$cookie->appendChild(new XMLElement("email", $fields['email']));
$cookie->appendChild(new XMLElement("subject", $fields['subject']));
$cookie->appendChild(new XMLElement("message", General::sanitize($fields['message'])));
$result->appendChild($cookie);
$fields['recipient'] = preg_split('/\,/i', $fields['recipient_username'], -1, PREG_SPLIT_NO_EMPTY);
$fields['recipient'] = array_map('trim', $fields['recipient']);
$fields['recipient'] = $this->_Parent->Database->fetch("SELECT `email`, `first_name` FROM `tbl_authors` WHERE `username` IN ('".@implode("', '", $fields['recipient'])."') ");
$canProceed = true;
if($fields['email'] == ""
|| $fields['name'] == ""
|| $fields['subject'] == ""
|| $fields['message'] == ""
// || $fields['saynotospam'] != "idislikeit"
){
$xMissing = new XMLElement("missing");
if($fields['email'] == "") {
$missing = new XMLElement("input");
$missing->setAttribute("name", "E-mail address");
$xMissing->appendChild($missing);
}
if($fields['name'] == "") {
$missing = new XMLElement("input");
$missing->setAttribute("name", "Name");
$xMissing->appendChild($missing);
}
if($fields['subject'] == "") {
$missing = new XMLElement("input");
$missing->setAttribute("name", "Subject");
$xMissing->appendChild($missing);
}
if($fields['message'] == "") {
$missing = new XMLElement("input");
$missing->setAttribute("name", "Message");
$xMissing->appendChild($missing);
}
// if($fields['saynotospam'] == "") {
// $missing = new XMLElement("input");
// $missing->setAttribute("name", "Anti-spam check");
// $xMissing->appendChild($missing);
// }
$result->appendChild($xMissing);
$canProceed = false;
}
if(!ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $fields['email'])){
$invalid = new XMLElement("invalid");
$xInvalid = new XMLElement("input");
$xInvalid->setAttribute("name", "E-mail address");
$invalid->appendChild($xInvalid);
$result->appendChild($invalid);
$canProceed = false;
}
if(!$canProceed)
$result->setAttribute("result", "error");
else{
$errors = array();
foreach($fields['recipient'] as $r){
list($email, $name) = array_values($r);
if(!General::sendEmail($email,
$fields['email'],
$fields['name'],
$fields['subject'],
$fields['message'] . "\n\nIP-address: " . $fields['ip']))
$errors[] = $email;
}
if(!empty($errors)){
$result->appendChild(new XMLElement("notice", "E-mail could not be sent. An unknown error occurred."));
$result->setAttribute("result", "error");
}else{
$result->appendChild(new XMLElement("notice", "E-mail sent successfully"));
$result->setAttribute("result", "success");
}
}
return $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment