CWIS Developer Documentation
Email.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: Email.php
5 #
6 # Part of the ScoutLib application support library
7 # Copyright 2012 Edward Almasy and Internet Scout
8 # http://scout.wisc.edu
9 #
10 
15 class Email {
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18 
22  function __construct()
23  {
24  }
25 
26  /*@(*/
28 
34  function Send()
35  {
36  switch (self::$DeliveryMethod)
37  {
38  case self::METHOD_PHPMAIL:
39  $Result = $this->SendViaPhpMail();
40  break;
41 
42  case self::METHOD_SMTP:
43  $Result = $this->SendViaSmtp();
44  break;
45  }
46  return $Result;
47  }
48 
49  /*@(*/
51 
57  function Body($NewValue = NULL)
58  {
59  if ($NewValue !== NULL) { $this->Body = $NewValue; }
60  return $this->Body;
61  }
62 
68  function Subject($NewValue = NULL)
69  {
70  if ($NewValue !== NULL) { $this->Subject = $NewValue; }
71  return $this->Subject;
72  }
73 
82  function From($NewAddress = NULL, $NewName = NULL)
83  {
84  if ($NewAddress !== NULL)
85  {
86  $NewAddress = trim($NewAddress);
87  if ($NewName !== NULL)
88  {
89  $NewName = trim($NewName);
90  $this->From = $NewName." <".$NewAddress.">";
91  }
92  else
93  {
94  $this->From = $NewAddress;
95  }
96  }
97  return $this->From;
98  }
99 
108  function ReplyTo($NewAddress = NULL, $NewName = NULL)
109  {
110  if ($NewAddress !== NULL)
111  {
112  $NewAddress = trim($NewAddress);
113  if ($NewName !== NULL)
114  {
115  $NewName = trim($NewName);
116  $this->ReplyTo = $NewName." <".$NewAddress.">";
117  }
118  else
119  {
120  $this->ReplyTo = $NewAddress;
121  }
122  }
123  return $this->ReplyTo;
124  }
125 
133  function To($NewValue = NULL)
134  {
135  if ($NewValue !== NULL)
136  {
137  if (!is_array($NewValue))
138  {
139  $this->To = array($NewValue);
140  }
141  else
142  {
143  $this->To = $NewValue;
144  }
145  }
146  return $this->To;
147  }
148 
156  function CC($NewValue = NULL)
157  {
158  if ($NewValue !== NULL)
159  {
160  if (!is_array($NewValue))
161  {
162  $this->CC = array($NewValue);
163  }
164  else
165  {
166  $this->CC = $NewValue;
167  }
168  }
169  return $this->CC;
170  }
171 
179  function BCC($NewValue = NULL)
180  {
181  if ($NewValue !== NULL)
182  {
183  if (!is_array($NewValue))
184  {
185  $this->BCC = array($NewValue);
186  }
187  else
188  {
189  $this->BCC = $NewValue;
190  }
191  }
192  return $this->BCC;
193  }
194 
199  function AddHeaders($NewHeaders)
200  {
201  # add new headers to list
202  $this->Headers = array_merge($this->Headers, $NewHeaders);
203  }
204 
205  /*@(*/
207 
214  static function DeliveryMethod($NewValue = NULL)
215  {
216  if ($NewValue !== NULL)
217  {
218  self::$DeliveryMethod = $NewValue;
219  }
220  return self::$DeliveryMethod;
221  }
222  const METHOD_PHPMAIL = 1;
223  const METHOD_SMTP = 2;
224 
230  static function Server($NewValue = NULL)
231  {
232  if ($NewValue !== NULL) { self::$Server = $NewValue; }
233  return self::$Server;
234  }
235 
241  static function Port($NewValue = NULL)
242  {
243  if ($NewValue !== NULL) { self::$Port = $NewValue; }
244  return self::$Port;
245  }
246 
252  static function UserName($NewValue = NULL)
253  {
254  if ($NewValue !== NULL) { self::$UserName = $NewValue; }
255  return self::$UserName;
256  }
257 
263  static function Password($NewValue = NULL)
264  {
265  if ($NewValue !== NULL) { self::$Password = $NewValue; }
266  return self::$Password;
267  }
268 
274  static function UseAuthentication($NewValue = NULL)
275  {
276  if ($NewValue !== NULL) { self::$UseAuthentication = $NewValue; }
277  return self::$UseAuthentication;
278  }
279 
287  static function DeliverySettings($NewSettings = NULL)
288  {
289  if ($NewSettings !== NULL)
290  {
291  $Settings = unserialize($NewSettings);
292  self::$DeliveryMethod = $Settings["DeliveryMethod"];
293  self::$Server = $Settings["Server"];
294  self::$Port = $Settings["Port"];
295  self::$UserName = $Settings["UserName"];
296  self::$Password = $Settings["Password"];
297  self::$UseAuthentication = $Settings["UseAuthentication"];
298  }
299  else
300  {
301  $Settings["DeliveryMethod"] = self::$DeliveryMethod;
302  $Settings["Server"] = self::$Server;
303  $Settings["Port"] = self::$Port;
304  $Settings["UserName"] = self::$UserName;
305  $Settings["Password"] = self::$Password;
306  $Settings["UseAuthentication"] = self::$UseAuthentication;
307  }
308  return serialize($Settings);
309  }
310 
319  static function DeliverySettingsOkay()
320  {
321  # start out with error list clear
322  self::$DeliverySettingErrorList = array();
323 
324  # test based on delivery method
325  switch (self::$DeliveryMethod)
326  {
327  case self::METHOD_PHPMAIL:
328  # always report success
329  $SettingsOkay = TRUE;
330  break;
331 
332  case self::METHOD_SMTP:
333  # set up PHPMailer for test
334  $PMail = new PHPMailer(TRUE);
335  $PMail->IsSMTP();
336  $PMail->SMTPAuth = self::$UseAuthentication;
337  $PMail->Host = self::$Server;
338  $PMail->Port = self::$Port;
339  $PMail->Username = self::$UserName;
340  $PMail->Password = self::$Password;
341 
342  # test settings
343  try
344  {
345  $SettingsOkay = $PMail->SmtpConnect();
346  }
347  # if test failed
348  catch (phpmailerException $Except)
349  {
350  # translate PHPMailer error message to possibly bad settings
351  switch ($Except->getMessage())
352  {
353  case 'SMTP Error: Could not authenticate.':
354  self::$DeliverySettingErrorList = array(
355  "UseAuthentication",
356  "UserName",
357  "Password",
358  );
359  break;
360 
361  case 'SMTP Error: Could not connect to SMTP host.':
362  self::$DeliverySettingErrorList = array(
363  "Server",
364  "Port",
365  );
366  break;
367 
368  case 'Language string failed to load: tls':
369  self::$DeliverySettingErrorList = array("TLS");
370  break;
371 
372  default:
373  self::$DeliverySettingErrorList = array("UNKNOWN");
374  break;
375  }
376 
377  # make sure failure is reported
378  $SettingsOkay = FALSE;
379  }
380  break;
381  }
382 
383  # report result to caller
384  return $SettingsOkay;
385  }
386 
391  static function DeliverySettingErrors()
392  {
393  return self::$DeliverySettingErrorList;
394  }
395 
396 
397  # ---- PRIVATE INTERFACE -------------------------------------------------
398 
399  private $From = "";
400  private $ReplyTo = "";
401  private $To = array();
402  private $CC = array();
403  private $BCC = array();
404  private $Body = "";
405  private $Subject = "";
406  private $Headers = array();
407 
408  private static $DeliveryMethod = self::METHOD_PHPMAIL;
409  private static $DeliverySettingErrorList = array();
410  private static $Server;
411  private static $Port = 25;
412  private static $UserName = "";
413  private static $Password = "";
414  private static $UseAuthentication = FALSE;
415 
416  private function SendViaPhpMail()
417  {
418  # build headers list
419  $Headers = "From: ".self::CleanHeaderValue($this->From)."\r\n";
420  $Headers .= $this->BuildAddresseeLine("Cc", $this->CC);
421  $Headers .= $this->BuildAddresseeLine("Bcc", $this->BCC);
422  $Headers .= "Reply-To: ".self::CleanHeaderValue(
423  strlen($this->ReplyTo) ? $this->ReplyTo : $this->From)
424  ."\r\n";
425  foreach ($this->Headers as $ExtraHeader)
426  {
427  $Headers .= $ExtraHeader."\r\n";
428  }
429 
430  # build recipient list
431  $To = "";
432  $Separator = "";
433  foreach ($this->To as $Recipient)
434  {
435  $To .= $Separator.$Recipient;
436  $Separator = ", ";
437  }
438 
439  # send message
440  $Result = mail($To, $this->Subject, $this->Body, $Headers);
441 
442  # report to caller whether attempt to send succeeded
443  return $Result;
444  }
445 
446  private function SendViaSmtp()
447  {
448  # create PHPMailer and set up for SMTP
449  $PMail = new PHPMailer();
450  $PMail->IsSMTP();
451  $PMail->SMTPAuth = self::$UseAuthentication;
452  $PMail->Host = self::$Server;
453  $PMail->Port = self::$Port;
454  $PMail->Username = self::$UserName;
455  $PMail->Password = self::$Password;
456 
457  # set message attributes
458  if (preg_match("/ </", $this->From))
459  {
460  $Pieces = explode(" ", $this->From);
461  $Address = array_pop($Pieces);
462  $Address = preg_replace("/[<>]+/", "", $Address);
463  $Name = trim(implode($Pieces));
464  }
465  else
466  {
467  $Name = "";
468  $Address = $this->From;
469  }
470  $PMail->SetFrom($Address, $Name);
471  $PMail->Subject = $this->Subject;
472  $PMail->Body = $this->Body;
473  $PMail->IsHTML(FALSE);
474  foreach ($this->To as $Recipient)
475  {
476  $PMail->AddAddress($Recipient);
477  }
478 
479  # add any extra header lines to message
480  foreach ($this->Headers as $ExtraHeader)
481  {
482  $PMail->AddCustomHeader($ExtraHeader);
483  }
484 
485  # send message
486  $Result = $PMail->Send();
487 
488  # report to caller whether attempt to send succeeded
489  return $Result;
490  }
491 
492  private function BuildAddresseeLine($Label, $Recipients)
493  {
494  $Line = "";
495  if (count($Recipients))
496  {
497  $Line .= $Label.": ";
498  $Separator = "";
499  foreach ($Recipients as $Recipient)
500  {
501  $FullBody .= $Separator.self::CleanHeaderValue($Recipient);
502  $Separator = ", ";
503  }
504  $Line .= "\r\n";
505  }
506  return $Line;
507  }
508 
514  private static function CleanHeaderValue($Value)
515  {
516  # (regular expression taken from sanitizeHeaders() function in
517  # Mail PEAR package)
518  return preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
519  "", $Value);
520  }
521 
522 }
523 
524