Umfragen
email.class.php
Go to the documentation of this file.
1 <?php
2 /*
3  * email.class.php
4  *
5  * Copyright 2013 Johannes ter Haak <jojo@jojo-42>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  *
22  *
23  */
24 
25 require_once("PHPMailer-5.2.6/class.phpmailer.php");
26 
27 
28 class email{
29  private $method='';
30  private $authSecure ='';
31  private $mailer ='';
32 
33  private $message = '';
34  private $addressList = array();
35  private $subject = '';
36 
37 
42  public function __construct(){
43  $this->load_config();
44  }
45 
46 
51  private function load_config(){
52  if( (defined('EMAIL_METHOD')) AND (defined('EMAIL_SEND_METHOD')) AND (defined('EMAIL_AUTH_ENCRYPTION')) ){
53  // email method. group=sending to one address per group
54  if( EMAIL_METHOD == 'group' ){
55  $this->method = 'group';
56  }else{
57  throw new Exception("Fehlkonfiguration des email-moduls! (EMAIL_METHOD)");
58  }
59  // phpmailer config -> send email per ext. smpt or on localhost or whatever
60  if( EMAIL_SEND_METHOD == 'smtp' ){
61  $this->mailer = 'smtp';
62  }else{
63  throw new Exception("Fehlkonfiguration des email-moduls! (EMAIL_SEND_METHOD)");
64  }
65  // password encryption (often required by server)
66  if( EMAIL_AUTH_ENCRYPTION == 'ssl' ){
67  $this->authSecure = 'ssl';
68  }else{
69  throw new Exception("Fehlkonfiguration des email-moduls! (EMAIL_AUTH_ENCRYPTION)");
70  }
71 
72  }else{
73  throw new Exception("Fehlkonfiguration des email-moduls!");
74  }
75 
76  if( (!defined('EMAIL_SMTP_SERVER'))
77  OR (!defined('EMAIL_SMTP_PORT'))
78  OR (!defined('EMAIL_SMTP_USERNAME'))
79  OR (!defined('EMAIL_ADDRESS'))
80  OR (!defined('EMAIL_NAME'))
81  OR (!defined('EMAIL_SMTP_PASSWORD')) ){
82  throw new Exception("Fehlkonfiguration des email-moduls");
83  }
84  if( (EMAIL_SMTP_SERVER == '')
85  OR (EMAIL_SMTP_PORT == '')
86  OR (EMAIL_SMTP_USERNAME == '')
87  OR (EMAIL_ADDRESS == '')
88  OR (EMAIL_NAME == '')
89  OR (EMAIL_SMTP_PASSWORD == '') ){
90  throw new Exception("Fehlkonfiguration des email-moduls");
91  }
92 
93  }
94 
95 
101  public function addAddress( $address, $name){
102  if( (!empty($address)) AND (strpos($address,"@") !== false) AND (!empty($name)) ){
103  $this->addressList[] = array($address,$name);
104  }
105  }
106 
107 
113  public function setSubject($text){
114  $text = strval($text);
115  if( !empty( $text ) ){
116  $this->subject = $text;
117  return true;
118  }
119  return false;
120  }
121 
122 
128  public function setMessage($text){
129  $text = strval($text);
130  if( !empty( $text ) ){
131  $this->message = $text;
132  return true;
133  }
134  return false;
135  }
136 
137 
143  private function sendmail(){
144  if( class_exists("PHPMailer") ){
145  $mail = new PHPMailer(true); // the true param means it will throw Exceptions on errors, which we need to catch
146 
147  if( (empty($this->mailer))
148  OR (empty($this->authSecure)) ){
149  throw new Exception("Fehlkonfiguration des email-moduls");
150  }
151  if( empty($this->addressList) ){
152  throw new Exception("Es fehlt die Adressenliste. Email nicht versendet.");
153  }
154  $mail->IsSMTP(); // telling the class to use SMTP
155  try {
156  // MAIL ACCOUNT
157  $mail->Host = EMAIL_SMTP_SERVER; //"mail.yourdomain.com"; // SMTP server
158  $mail->SMTPDebug = false; // enables SMTP debug information (for testing)
159  $mail->Mailer = $this->mailer;
160  $mail->SMTPSecure = $this->authSecure;
161  $mail->SMTPAuth = true; // enable SMTP authentication
162  $mail->Port = EMAIL_SMTP_PORT; //26; // set the SMTP port for the GMAIL server
163  $mail->Username = EMAIL_SMTP_USERNAME; //"yourname@yourdomain"; // SMTP account username
164  $mail->Password = EMAIL_SMTP_PASSWORD; //"yourpassword"; // SMTP account password
165 
166  // THE EMAIL
167  $mail->ContentType = 'text/plain';
168  $mail->IsHTML(false);
169  foreach( $this->addressList as $add ){
170  if( (is_array($add)) AND (sizeof($add) >= 2 ) ){
171  $mail->AddAddress($add[0], $add[1]);
172  }
173  }
174  $mail->SetFrom(EMAIL_ADDRESS, EMAIL_NAME);
175  $mail->AddReplyTo(EMAIL_ADDRESS, EMAIL_NAME);
176  if( !empty($this->subject) ){
177  $mail->Subject = $this->subject;
178  }else{
179  throw new Exception("Keinen Berteff der Email angegeben!");
180  }
181  if( !empty($this->message) ){
182  $mail->Body = $this->message;
183  }else{
184  throw new Exception("Die Email ist leer!");
185  }
186 
187  $mail->Send();
188  return true;
189  } catch (phpmailerException $e) {
190  return $e->errorMessage(); //Pretty error messages from PHPMailer
191  } catch (Exception $e) {
192  return $e->getMessage(); //Boring error messages from anything else!
193  }
194 
195  }else{
196  throw new Exception("PHPMailer nicht gefunden!");
197  }
198 
199  }
200 
201 
202 
203 
213  public function email($au,$subject,$message,$to=array("all") ){
214  if( $this->method == 'group' ){
215  global $email_groups;
216  /*array(
217  "501" => "user@domain.tld",
218  "505" => "user2@domain.tlde"
219  );*/
220  global $groups_whitelist;
221  if( !is_array($email_groups) ){
222  throw new Exception("Fehlkonfiguration des email-moduls (email_groups)");
223  }
224  if( !defined('TEACHER_GROUP') ){
225  throw new Exception("Lehrer Gruppe in der Konfigurationsdatei nicht angegeben!");
226  }
227  $teachers = explode(",",TEACHER_GROUP);
228  $group_mapping = $au->get_gid_to_name_mapping($groups_whitelist);
229 
230  foreach( $teachers as $t){
231  if( (in_array("all",$to)) OR (in_array($t,$to)) ){
232  if( (isset($email_groups[$t])) AND (isset($group_mapping[$t])) ){
233  $this->addAddress( $email_groups[$t] , $group_mapping[$t]);
234  } // end if mapping groupID to address OK
235  } // end if to
236  } // end foreach teacher
237 
238 
239  if( !($this->setSubject($subject)) ){
240  return "Betreffzeile ist leer. Bitte einen Betreff für die Email eingeben.";
241  }
242  if( !($this->setMessage($message)) ){
243  return "Es ist kein Text für die Email angegeben. Leere Emails werden nicht verschickt.";
244  }
245 
246  try{
247  $res = $this->sendmail();
248  }catch(Exception $e){
249  return $e->getMessage();
250  }
251  return $res;
252 
253  } // END GROUP METHOD
254  }
255 
256 
265  public function email_users($to,$to_name,$subject,$message ){
266 
267  $this->addAddress( $to , $to_name );
268 
269  if( !($this->setSubject($subject)) ){
270  return "Keinen Betreff angegeben";
271  }
272 
273  $message = mb_convert_encoding($message,"ISO-8859-1","UTF-8");
274  if( !($this->setMessage($message)) ){
275  return "Text ist leer";
276  }
277 
278  try{
279  $res = $this->sendmail();
280  }catch(Exception $e){
281  return $e->getMessage();
282  }
283  return $res;
284  }
285 
286 
287 
288 
296  public function display_email_edit($poll,$au,$subject='',$message=''){
297  global $groups_whitelist;
298  global $email_groups;
299  $teachers = explode(",",TEACHER_GROUP);
300  $group_mapping = $au->get_gid_to_name_mapping($groups_whitelist);
301  $to = array();
302  if( in_array("*",$poll->groups) ){
303  $to = $teachers;
304  }else{
305  foreach( $poll->groups as $gr ){
306  if( in_array($gr,$teachers) ){
307  $to[] = $gr;
308  }
309  }
310  }
311 
312  echo "<table>";
313  echo "<tr><td><table style='margin-bottom:1em'>";
314  echo "<tr>
315  <td style='font-weight:bold;'>Von:</td>
316  <td style='border-bottom:1px solid #989898'><span style='color:#163E72'>".EMAIL_NAME."</span> &nbsp;&lt;".EMAIL_ADDRESS."&gt; </td>
317  </tr>";
318  foreach( $teachers as $t ){
319  if( (isset($email_groups[$t])) AND (isset($group_mapping[$t])) AND (in_array($t,$to)) ){
320  echo "<tr>
321  <td style='font-weight:bold;'>An:</td>
322  <td style='border-bottom:1px solid #989898'><span style='color:#163E72'>{$group_mapping[$t]}</span> &nbsp;&lt;{$email_groups[$t]}&gt;</td>
323  </tr>";
324  }
325  }
326  echo "</table></td></tr>";
327  echo "<tr>";
328  echo "<td><span style='font-weight:bold'>Betreff:</span></td>";
329  echo "</tr>";
330  echo "<tr>";
331  echo "<td><input type='text' style='margin-left:2em' size='60' value='$subject' name='email_subject'/></td>";
332  echo "</tr>";
333 
334  echo "<tr>";
335  echo "<td><span style='font-weight:bold'>Text:</span></td>";
336  echo "</tr>";
337  echo "<tr>";
338  echo "<td><textarea cols='70' rows='10' style='margin-left:2em;' name='email_message'>$message</textarea></td>";
339  echo "</tr>";
340 
341  echo "</table>";
342  }
343 
344 
351  public function handle_email_edit($poll,$au){
352  if( (isset($_POST["send_email"])) ){
353  if( (isset($_POST["email_subject"]))
354  //AND ($_POST["email_subject"] != "")
355  //AND ($_POST["email_message"] != "")
356  AND (isset($_POST["email_message"])) ){
357  //$subject = htmlspecialchars( $_POST["email_subject"] , ENT_QUOTES , "UTF-8",true);
358  $subject = strip_tags( $_POST["email_subject"] );
359  $subject = mb_convert_encoding($subject,"ISO-8859-1","UTF-8");
360 
361  //$message = htmlspecialchars( $_POST["email_message"] , ENT_QUOTES , "UTF-8",true);
362  $message = strip_tags( $_POST["email_message"] );
363  $message = mb_convert_encoding($message,"ISO-8859-1","UTF-8");
364 
365  $teachers = explode(",",TEACHER_GROUP);
366  $to = array();
367  if( in_array("*",$poll->groups) ){
368  $to = $teachers;
369  }else{
370  foreach( $poll->groups as $gr ){
371  if( in_array($gr,$teachers) ){
372  $to[] = $gr;
373  }
374  }
375  }
376 
377  $res = $this->email($au,$subject,$message,$to);
378  return $res;
379  }else{ // END if vars
380  return "Keinen Text eingetragen oder der Betreff der Email ist leer. Bitte dies angeben.";
381  }
382  }else{ // end if POST
383  return;
384  }
385  }
386 
387 
388 
389 }
390 // ! no ending newLine
391 ?>