6 # Part of the ScoutLib application support library
7 # Copyright 2012 Edward Almasy and Internet Scout
8 # http://scout.wisc.edu
17 # ---- PUBLIC INTERFACE --------------------------------------------------
36 switch (self::$DeliveryMethod)
38 case self::METHOD_PHPMAIL:
39 $Result = $this->SendViaPhpMail();
42 case self::METHOD_SMTP:
43 $Result = $this->SendViaSmtp();
57 function Body($NewValue = NULL)
59 if ($NewValue !== NULL) { $this->
Body = $NewValue; }
70 if ($NewValue !== NULL) { $this->
Subject = $NewValue; }
71 return $this->Subject;
82 function From($NewAddress = NULL, $NewName = NULL)
84 if ($NewAddress !== NULL)
86 $NewAddress = trim($NewAddress);
87 if ($NewName !== NULL)
89 $NewName = trim($NewName);
90 $this->
From = $NewName.
" <".$NewAddress.
">";
94 $this->
From = $NewAddress;
108 function ReplyTo($NewAddress = NULL, $NewName = NULL)
110 if ($NewAddress !== NULL)
112 $NewAddress = trim($NewAddress);
113 if ($NewName !== NULL)
115 $NewName = trim($NewName);
116 $this->
ReplyTo = $NewName.
" <".$NewAddress.
">";
123 return $this->ReplyTo;
133 function To($NewValue = NULL)
135 if ($NewValue !== NULL)
137 if (!is_array($NewValue))
139 $this->
To = array($NewValue);
143 $this->
To = $NewValue;
156 function CC($NewValue = NULL)
158 if ($NewValue !== NULL)
160 if (!is_array($NewValue))
162 $this->
CC = array($NewValue);
166 $this->
CC = $NewValue;
179 function BCC($NewValue = NULL)
181 if ($NewValue !== NULL)
183 if (!is_array($NewValue))
185 $this->
BCC = array($NewValue);
189 $this->
BCC = $NewValue;
201 # add new headers to list
202 $this->Headers = array_merge($this->Headers, $NewHeaders);
216 if ($NewValue !== NULL)
218 self::$DeliveryMethod = $NewValue;
220 return self::$DeliveryMethod;
232 if ($NewValue !== NULL) { self::$Server = $NewValue; }
233 return self::$Server;
241 static function Port($NewValue = NULL)
243 if ($NewValue !== NULL) { self::$Port = $NewValue; }
254 if ($NewValue !== NULL) { self::$UserName = $NewValue; }
255 return self::$UserName;
265 if ($NewValue !== NULL) { self::$Password = $NewValue; }
266 return self::$Password;
276 if ($NewValue !== NULL) { self::$UseAuthentication = $NewValue; }
277 return self::$UseAuthentication;
289 if ($NewSettings !== NULL)
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"];
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;
308 return serialize($Settings);
321 # start out with error list clear
322 self::$DeliverySettingErrorList = array();
324 # test based on delivery method
325 switch (self::$DeliveryMethod)
327 case self::METHOD_PHPMAIL:
328 # always report success
329 $SettingsOkay = TRUE;
332 case self::METHOD_SMTP:
333 # set up PHPMailer for test
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;
345 $SettingsOkay = $PMail->SmtpConnect();
350 # translate PHPMailer error message to possibly bad settings
351 switch ($Except->getMessage())
353 case 'SMTP Error: Could not authenticate.':
354 self::$DeliverySettingErrorList = array(
361 case 'SMTP Error: Could not connect to SMTP host.':
362 self::$DeliverySettingErrorList = array(
368 case 'Language string failed to load: tls':
369 self::$DeliverySettingErrorList = array(
"TLS");
373 self::$DeliverySettingErrorList = array(
"UNKNOWN");
377 # make sure failure is reported
378 $SettingsOkay = FALSE;
383 # report result to caller
384 return $SettingsOkay;
393 return self::$DeliverySettingErrorList;
397 # ---- PRIVATE INTERFACE -------------------------------------------------
400 private $ReplyTo =
"";
401 private $To = array();
402 private $CC = array();
403 private $BCC = array();
405 private $Subject =
"";
406 private $Headers = array();
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;
416 private function SendViaPhpMail()
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(
425 foreach ($this->Headers as $ExtraHeader)
427 $Headers .= $ExtraHeader.
"\r\n";
430 # build recipient list
433 foreach ($this->
To as $Recipient)
435 $To .= $Separator.$Recipient;
440 $Result = mail($To, $this->
Subject, $this->
Body, $Headers);
442 # report to caller whether attempt to send succeeded
446 private function SendViaSmtp()
448 # create PHPMailer and set up for SMTP
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;
457 # set message attributes
458 if (preg_match(
"/ </", $this->From))
460 $Pieces = explode(
" ", $this->From);
461 $Address = array_pop($Pieces);
462 $Address = preg_replace(
"/[<>]+/",
"", $Address);
463 $Name = trim(implode($Pieces));
468 $Address = $this->From;
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)
476 $PMail->AddAddress($Recipient);
479 # add any extra header lines to message
480 foreach ($this->Headers as $ExtraHeader)
482 $PMail->AddCustomHeader($ExtraHeader);
486 $Result = $PMail->Send();
488 # report to caller whether attempt to send succeeded
492 private function BuildAddresseeLine($Label, $Recipients)
495 if (count($Recipients))
497 $Line .= $Label.
": ";
499 foreach ($Recipients as $Recipient)
501 $FullBody .= $Separator.self::CleanHeaderValue($Recipient);
514 private static function CleanHeaderValue($Value)
516 # (regular expression taken from sanitizeHeaders() function in
518 return preg_replace(
'=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',