-
Notifications
You must be signed in to change notification settings - Fork 0
/
xNewsletter.php
executable file
·183 lines (150 loc) · 3.99 KB
/
xNewsletter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @name Newsletter
* @desc Send a Newsletter with site content
* @version v0.1(160710)
* @author @xopherdeep
* @price $40
* @icon News.png
* @mini bullhorn
* @see copy
* @link newsletter
* @todo
* @beta
*/
class xNewsletter extends Xengine{
// function autoRun(){
//
// }
function installSql(){
// Check for table.
$table = $this->q()->Q('show tables like "Newsletters"');
if(empty($table)){
// Install Table!
$this->q()->Q("CREATE TABLE `Newsletters`
(`id` int not_null AUTO_INCREMENT,
`title` char(50),
`content` blob,
`date` varchar(10),
`sent` int,
PRIMARY KEY (id))
");
}
}
/**
* List News Items for both public and Admin.
* templates will give links to either, readmore, edit, etc.
*/
function index(){
$this->q()->mBy = array('date'=>'DESC');
$list = $this->q()->Select('*','Newsletters');
$this->set('list',$list);
}
function article($id,$title=null,$date=null){
$article = $this->q()->Select('*','Newsletters',array('id'=>$id));
$this->set('article',$article[0]);
$this->set('PAGE_TITLE','Newsletters');
}
function add($send=false){
$this->installSql();
if(isset($_POST['news'])){
if($send){
$_POST['news']['sent'] = 1;
}
if($_POST['news']['id'] > 0){
$this->q()->Update('Newsletters',$_POST['news'],array('id'=>$_POST['news']['id']));
$this->set('success',true);
}else{
unset($_POST['news']['id']);
$this->q()->Insert('Newsletters',$_POST['news']);
$this->set('success',true);
}
if($send == 1){
$this->sendLetter($_POST['news']);
}
}
}
function sendLetter($news){
// Get list of everyone.
$users = $this->q()->Select('id,email,username,first_name,last_name','Users',array(
'newsletter' => 1
));
// Send to people who have yet to recieve this.
if(!empty($users)){
foreach($users as $k => $v){
$bcc = ($bcc) ? ', '.$v['email'] : $v['email'] ;
}
}
// multiple recipients
// subject
$subject = $news['title'];
// message
$message = "
<html>
<head>
<title>$news[title]</title>
</head>
<body>
$news[content]
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$host = $_SERVER['HTTP_HOST'];
//$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= "From: $host Newsletter <newsletter@$host>" . "\r\n";
//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= "Bcc: $bcc" . "\r\n";
// Mail it
mail("newsletter@$host", $subject, $message, $headers);
// Get list of everyone who has recieved this letter.
$this->set('success',true);
$this->set('sent',true);
}
function edit($id){
$data = $this->q()->Select('*','Newsletters',array(
'id' => $id
));
//$news['news'] = $data[0];
foreach($data[0] as $k => $v){
$news["news[$k]"] = $v;
}
$out = array(
'success' => true,
'data' => $news
);
//$this->set('success',true);
//$this->set('data',$news);
//$this->out($out);
echo json_encode($out);
exit;
}
function delete($id=null){
if($id > 0){
$this->q()->Delete('Newsletters',array('id'=>$id));
}
header('Location: /@/newsletter');
}
function addUserEmail(){
$q = $this->q();
$user = $q->Select('id','Users',array(
'email' => $_POST['email']
));
if(!empty($user[0])){
return array(
'success' => false,
'error' => 'That email already exists in our database.'
);
}else{
$_POST['last_login'] = time();
return array(
'success' => true,
'msg' => $q->Insert('Users',$_POST)
);
}
}
}
?>