-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.mailex.php
84 lines (64 loc) · 2.28 KB
/
class.mailex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
* Road to V::class.mailex.php
*
* Created on Feb 11, 2009
*
*/
require_once(WORKSPACE . '/api/class.databasemanipulator.php');
class MailEx
{
const REPLACEMENT_EXPRESSION = '/\{\$(.+)\}/';
private $_section_id = null;
private $_fields = null;
private $_transformed_fields = null;
private $_transformations = null;
private static $default_fields = array("handle", "title", "body");
/*
** Dynamic constrction
*/
public function __construct( $parent, $section_name, $entry_handle, $field_names = null )
{
if (!$section_name || !$entry_handle || !$parent)
throw new Exception(__CLASS__ . ":: required constructor arguments not passed");
// Assign defaults
if (!$field_names) $field_names = self::$default_fields;
// Set up the manipulator
DatabaseManipulator::associateParent($parent);
// This can be either a handle or a full name
$this->_section_id = DatabaseManipulator::getSectionIDByName( $section_name );
// Get a list of ids mapped to handles
$field_ids = DatabaseManipulator::getFieldIDsByName( $field_names, $this->_section_id );
// Get all the entries for the section where the first field name matches entry_handle
$this->_fields = DatabaseManipulator::getEntries( $this->_section_id, $field_ids, reset($field_names), $entry_handle );
}
/*
** Process the email section's templating values
*/
public function transform( $transformations )
{
$this->_transformations = $transformations;
foreach ($this->_fields as $key => $value)
$this->_transformed_fields[$key] = preg_replace_callback(self::REPLACEMENT_EXPRESSION, array($this, "callback"), $value['value']);
}
/*
** Send out the email
*/
public function send( $adressee, $sender )
{
if (!$this->_transformed_fields)
throw new Exception(__CLASS__ . ":: transformation has not been run");
$admin_email = $sender;
$headers = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: '.$admin_email."\r\n" .
'Reply-To: '.$admin_email;
return mail($adressee, $this->_transformed_fields['title'], $this->_transformed_fields['body'], $headers);
}
// Callback for preg_replacement
private function callback( $matches )
{
return $this->_transformations[$matches[1]];
}
}
?>