PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/sgl/includes/qcubed/_core/framework/QEmailServer.class.php

http://logisticsouth.googlecode.com/
PHP | 594 lines | 360 code | 90 blank | 144 comment | 101 complexity | 95b486fc856d24feaafd131aa03881dd MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * This EmailServer (and its dependent EmailMessage class) allows the application to send
  4. * messages via any accessible SMTP server.
  5. *
  6. * The QEmailServer class, specifically, is an abstract class and is NOT meant to be instantiated.
  7. * It has one public static method, Send, which takes in a QEmailMessage object.
  8. */
  9. abstract class QEmailServer extends QBaseClass {
  10. /**
  11. * Server Hostname or IP Address of the server running the SMTP service.
  12. * Using an IP address is slightly faster, but using a Hostname is easier to manage.
  13. * Defaults to "localhost".
  14. *
  15. * @var string SmtpServer
  16. */
  17. public static $SmtpServer = 'localhost';
  18. /**
  19. * Port of the SMTP Service on the SmtpServer, usually 25
  20. *
  21. * @var integer SmtpPort
  22. */
  23. public static $SmtpPort = 25;
  24. /**
  25. * IP Address of the Originating Server (e.g. the IP address of this server)
  26. * used for the EHLO command in the SMTP protocol. Defaults to the
  27. * QApplication::$ServerAddress variable, which uses the PHP $_SERVER
  28. * constants to determine the correct IP address.
  29. *
  30. * @var string OriginatingServerIp
  31. */
  32. public static $OriginatingServerIp;
  33. /**
  34. * Whether or not we are running in Test Mode. Test Mode allows you
  35. * to develop e-mail-based applications without actually having access to
  36. * an SMTP server or the Internet. Instead of messages being sent out,
  37. * the messages and corresponding SMTP communication will be saved to disk.
  38. *
  39. * @var boolean $TestMode
  40. */
  41. public static $TestMode = false;
  42. /**
  43. * The directory where TestMode e-mail files will be saved to. The process
  44. * running the webserver *must* have write access to this directory. Default
  45. * is "/tmp", which makes sense in unix/linux/mac environments. Windows users
  46. * will likely need to set up their own temp directories.
  47. *
  48. * @var string $TestModeDirectory
  49. */
  50. public static $TestModeDirectory = '/tmp';
  51. /**
  52. * Boolean flag signifying whether SMTP's AUTH PLAIN should be used
  53. *
  54. * @var bool $AuthPlain
  55. */
  56. public static $AuthPlain = false;
  57. /**
  58. * Boolean flag signifying whether SMTP's AUTH LOGIN should be used
  59. *
  60. * @var bool $AuthLogin
  61. */
  62. public static $AuthLogin = false;
  63. /**
  64. * SMTP Username to use for AUTH PLAIN or LOGIN
  65. *
  66. * @var string $SmtpUsername
  67. */
  68. public static $SmtpUsername = '';
  69. /**
  70. * SMTP Password to use for AUTH PLAIN or LOGIN
  71. *
  72. * @var string $SmtpPassword
  73. */
  74. public static $SmtpPassword = '';
  75. /**
  76. * Encoding Type (if null, will default to the QApplication::$EncodingType)
  77. *
  78. * @var string $EncodingType
  79. */
  80. public static $EncodingType = null;
  81. /**
  82. * Uses regular expression matching to return an array of valid e-mail addresses
  83. *
  84. * @param string $strAddresses Single string containing e-mail addresses and anything else
  85. * @return string[] An array of e-mail addresses only, or NULL if none
  86. */
  87. public static function GetEmailAddresses($strAddresses) {
  88. $strAddressArray = null;
  89. // Address Lines cannot have any linebreaks
  90. if ((strpos($strAddresses, "\r") !== false) ||
  91. (strpos($strAddresses, "\n") !== false))
  92. return null;
  93. preg_match_all ("/[a-zA-Z0-9_.'%+-]+[@][\-a-zA-Z0-9_.]+/", $strAddresses, $strAddressArray);
  94. if ((is_array($strAddressArray)) &&
  95. (array_key_exists(0, $strAddressArray)) &&
  96. (is_array($strAddressArray[0])) &&
  97. (array_key_exists(0, $strAddressArray[0]))) {
  98. return $strAddressArray[0];
  99. }
  100. // If we're here, then no addresses were found in $strAddress
  101. // so return null
  102. return null;
  103. }
  104. /**
  105. * This will check to see if an email address is considered "Valid" according to RFC 2822.
  106. * It utilizes the GetEmailAddresses static method, which does the actual logic work of checking.
  107. * @param string $strEmailAddress
  108. * @return boolean
  109. */
  110. public static function IsEmailValid($strEmailAddress) {
  111. $strEmailAddressArray = QEmailServer::GetEmailAddresses($strEmailAddress);
  112. return ((count($strEmailAddressArray) == 1) && ($strEmailAddressArray[0] == $strEmailAddress));
  113. }
  114. /**
  115. * Encodes given 8 bit string to a quoted-printable string,
  116. * @param string $strString
  117. * @return encoded string
  118. */
  119. private static function QuotedPrintableEncode($strString) {
  120. if ( function_exists('quoted_printable_encode') )
  121. $strText = quoted_printable_encode($strString);
  122. else
  123. $strText = preg_replace( '/[^\x21-\x3C\x3E-\x7E\x09\x20]/e', 'sprintf( "=%02X", ord ( "$0" ) ) ;', $strString );
  124. preg_match_all( '/.{1,73}([^=]{0,2})?/', $strText, $arrMatch );
  125. $strText = implode( '=' . "\r\n", $arrMatch[0] );
  126. return $strText;
  127. }
  128. /**
  129. * Sends a message out via SMTP according to the server, ip, etc. preferences
  130. * as set up on the class. Takes in a QEmailMessage object.
  131. *
  132. * Will throw a QEmailException exception on any error.
  133. *
  134. * @param QEmailMessage $objMessage Message to Send
  135. * @return void
  136. */
  137. public static function Send(QEmailMessage $objMessage) {
  138. $objResource = null;
  139. if (QEmailServer::$TestMode) {
  140. // Open up a File Resource to the TestModeDirectory
  141. $strArray = explode(' ', microtime());
  142. $strFileName = sprintf('%s/email_%s%s.txt', QEmailServer::$TestModeDirectory, $strArray[1], substr($strArray[0], 1));
  143. $objResource = fopen($strFileName, 'w');
  144. if (!$objResource)
  145. throw new QEmailException(sprintf('Unable to open Test SMTP connection to: %s', $strFileName));
  146. // Clear the Read Buffer
  147. if (!feof($objResource))
  148. fgets($objResource, 4096);
  149. // Write the Connection Command
  150. fwrite($objResource, sprintf("telnet %s %s\n", QEmailServer::$SmtpServer, QEmailServer::$SmtpPort));
  151. } else {
  152. $objResource = fsockopen(QEmailServer::$SmtpServer, QEmailServer::$SmtpPort);
  153. if (!$objResource)
  154. throw new QEmailException(sprintf('Unable to open SMTP connection to: %s %s', QEmailServer::$SmtpServer, QEmailServer::$SmtpPort));
  155. }
  156. // Connect
  157. $strResponse = null;
  158. if (!feof($objResource)) {
  159. $strResponse = fgets($objResource, 4096);
  160. // Iterate through all "220-" responses (stop at "220 ")
  161. while ((substr($strResponse, 0, 3) == "220") && (substr($strResponse, 0, 4) != "220 "))
  162. if (!feof($objResource))
  163. $strResponse = fgets($objResource, 4096);
  164. // Check for a "220" response
  165. if (!QEmailServer::$TestMode)
  166. if ((strpos($strResponse, "220") === false) || (strpos($strResponse, "220") != 0))
  167. throw new QEmailException(sprintf('Error Response on Connect: %s', $strResponse));
  168. }
  169. // Send: EHLO
  170. fwrite($objResource, sprintf("EHLO %s\n", QEmailServer::$OriginatingServerIp));
  171. if (!feof($objResource)) {
  172. $strResponse = fgets($objResource, 4096);
  173. // Iterate through all "250-" responses (stop at "250 ")
  174. while ((substr($strResponse, 0, 3) == "250") && (substr($strResponse, 0, 4) != "250 "))
  175. if (!feof($objResource))
  176. $strResponse = fgets($objResource, 4096);
  177. // Check for a "250" response
  178. if (!QEmailServer::$TestMode)
  179. if ((strpos($strResponse, "250") === false) || (strpos($strResponse, "250") != 0))
  180. throw new QEmailException(sprintf('Error Response on EHLO: %s', $strResponse));
  181. }
  182. // Send Authentication
  183. if (QEmailServer::$AuthPlain) {
  184. fwrite($objResource, "AUTH PLAIN " . base64_encode(QEmailServer::$SmtpUsername . "\0" . QEmailServer::$SmtpUsername . "\0" . QEmailServer::$SmtpPassword) . "\n");
  185. if (!feof($objResource)) {
  186. $strResponse = fgets($objResource, 4096);
  187. if ((strpos($strResponse, "235") === false) || (strpos($strResponse, "235") != 0))
  188. throw new QEmailException(sprintf('Error in response from AUTH PLAIN: %s', $strResponse));
  189. }
  190. }
  191. if (QEmailServer::$AuthLogin) {
  192. fwrite($objResource,"AUTH LOGIN\n");
  193. if (!feof($objResource)) {
  194. $strResponse = fgets($objResource, 4096);
  195. if (!QEmailServer::$TestMode)
  196. if ((strpos($strResponse, "334") === false) || (strpos($strResponse, "334") != 0))
  197. throw new QEmailException(sprintf('Error in response from AUTH LOGIN: %s', $strResponse));
  198. }
  199. fwrite($objResource, base64_encode(QEmailServer::$SmtpUsername) . "\n");
  200. if (!feof($objResource)) {
  201. $strResponse = fgets($objResource, 4096);
  202. if (!QEmailServer::$TestMode)
  203. if ((strpos($strResponse, "334") === false) || (strpos($strResponse, "334") != 0))
  204. throw new QEmailException(sprintf('Error in response from AUTH LOGIN: %s', $strResponse));
  205. }
  206. fwrite($objResource, base64_encode(QEmailServer::$SmtpPassword) . "\n");
  207. if (!feof($objResource)) {
  208. $strResponse = fgets($objResource, 4096);
  209. if (!QEmailServer::$TestMode)
  210. if ((strpos($strResponse, "235") === false) || (strpos($strResponse, "235") != 0))
  211. throw new QEmailException(sprintf('Error in response from AUTH LOGIN: %s', $strResponse));
  212. }
  213. }
  214. // Setup MAIL FROM line
  215. $strAddressArray = QEmailServer::GetEmailAddresses($objMessage->From);
  216. if (count($strAddressArray) != 1)
  217. throw new QEmailException(sprintf('Not a valid From address: %s', $objMessage->From));
  218. // Send: MAIL FROM line
  219. fwrite($objResource, sprintf("MAIL FROM: <%s>\n", $strAddressArray[0]));
  220. if (!feof($objResource)) {
  221. $strResponse = fgets($objResource, 4096);
  222. // Check for a "250" response
  223. if (!QEmailServer::$TestMode)
  224. if ((strpos($strResponse, "250") === false) || (strpos($strResponse, "250") != 0))
  225. throw new QEmailException(sprintf('Error Response on MAIL FROM: %s', $strResponse));
  226. }
  227. // Setup RCPT TO line(s)
  228. $strAddressToArray = QEmailServer::GetEmailAddresses($objMessage->To);
  229. if (!$strAddressToArray)
  230. throw new QEmailException(sprintf('Not a valid To address: %s', $objMessage->To));
  231. $strAddressCcArray = QEmailServer::GetEmailAddresses($objMessage->Cc);
  232. if (!$strAddressCcArray)
  233. $strAddressCcArray = array();
  234. $strAddressBccArray = QEmailServer::GetEmailAddresses($objMessage->Bcc);
  235. if (!$strAddressBccArray)
  236. $strAddressBccArray = array();
  237. $strAddressCcBccArray = array_merge($strAddressCcArray, $strAddressBccArray);
  238. $strAddressArray = array_merge($strAddressToArray, $strAddressCcBccArray);
  239. // Send: RCPT TO line(s)
  240. foreach ($strAddressArray as $strAddress) {
  241. fwrite($objResource, sprintf("RCPT TO: <%s>\n", $strAddress));
  242. if (!feof($objResource)) {
  243. $strResponse = fgets($objResource, 4096);
  244. // Check for a "250" response
  245. if (!QEmailServer::$TestMode)
  246. if ((strpos($strResponse, "250") === false) || (strpos($strResponse, "250") != 0))
  247. throw new QEmailException(sprintf('Error Response on RCPT TO: %s', $strResponse));
  248. }
  249. }
  250. // Send: DATA
  251. fwrite($objResource, "DATA\n");
  252. if (!feof($objResource)) {
  253. $strResponse = fgets($objResource, 4096);
  254. // Check for a "354" response
  255. if (!QEmailServer::$TestMode)
  256. if ((strpos($strResponse, "354") === false) || (strpos($strResponse, "354") != 0))
  257. throw new QEmailException(sprintf('Error Response on DATA: %s', $strResponse));
  258. }
  259. // Send: Required Headers
  260. fwrite($objResource, sprintf("Date: %s\n", QDateTime::NowToString(QDateTime::FormatRfc822)));
  261. fwrite($objResource, sprintf("To: %s\n", $objMessage->To));
  262. fwrite($objResource, sprintf("From: %s\n", $objMessage->From));
  263. // Setup Encoding Type (use QEmailServer if specified, otherwise default to QApplication's)
  264. if (!($strEncodingType = QEmailServer::$EncodingType))
  265. $strEncodingType = QApplication::$EncodingType;
  266. // Send: Optional Headers
  267. if ($objMessage->Subject)
  268. fwrite($objResource, sprintf("Subject: =?%s?Q?%s?=\n", $strEncodingType, self::QuotedPrintableEncode($objMessage->Subject)));
  269. if ($objMessage->Cc)
  270. fwrite($objResource, sprintf("Cc: %s\n", $objMessage->Cc));
  271. // Send: Content-Type Header (if applicable)
  272. // First, setup boundaries (may be needed if multipart)
  273. $strBoundary = sprintf('qcodo_mixed_boundary_%s', md5(microtime()));
  274. $strAltBoundary = sprintf('qcodo_alt_boundary_%s', md5(microtime()));
  275. // Send: Other Headers (if any)
  276. foreach ($objArray = $objMessage->HeaderArray as $strKey => $strValue)
  277. fwrite($objResource, sprintf("%s: %s\n", $strKey, $strValue));
  278. // if we are adding an html or files to the message we need these headers.
  279. if ($objMessage->HasFiles || $objMessage->HtmlBody) {
  280. fwrite($objResource, "MIME-Version: 1.0\n");
  281. fwrite($objResource, sprintf("Content-Type: multipart/mixed;\n boundary=\"%s\"\n", $strBoundary));
  282. fwrite($objResource, sprintf("This is a multipart message in MIME format.\n\n", $strBoundary));
  283. fwrite($objResource, sprintf("--%s\n", $strBoundary));
  284. }
  285. // Send: Body
  286. if ($objMessage->HtmlBody) {
  287. fwrite($objResource, sprintf("Content-Type: multipart/alternative;\n boundary=\"%s\"\n\n", $strAltBoundary));
  288. fwrite($objResource, sprintf("--%s\n", $strAltBoundary));
  289. fwrite($objResource, sprintf("Content-Type: text/plain; charset=\"%s\"\n", $strEncodingType));
  290. fwrite($objResource, sprintf("Content-Transfer-Encoding: quoted-printable\n\n"));
  291. fwrite($objResource, self::QuotedPrintableEncode($objMessage->Body));
  292. fwrite($objResource, "\n\n");
  293. fwrite($objResource, sprintf("--%s\n", $strAltBoundary));
  294. fwrite($objResource, sprintf("Content-Type: text/html; charset=\"%s\"\n", $strEncodingType));
  295. fwrite($objResource, sprintf("Content-Transfer-Encoding: quoted-printable\n\n"));
  296. fwrite($objResource, self::QuotedPrintableEncode($objMessage->HtmlBody));
  297. fwrite($objResource, "\n\n");
  298. fwrite($objResource, sprintf("--%s--\n", $strAltBoundary));
  299. } else if($objMessage->HasFiles) {
  300. fwrite($objResource, sprintf("Content-Type: multipart/alternative;\n boundary=\"%s\"\n\n", $strAltBoundary));
  301. fwrite($objResource, sprintf("--%s\n", $strAltBoundary));
  302. fwrite($objResource, sprintf("Content-Type: text/plain; charset=\"%s\"\n", $strEncodingType));
  303. fwrite($objResource, sprintf("Content-Transfer-Encoding: quoted-printable\n\n"));
  304. fwrite($objResource, self::QuotedPrintableEncode($objMessage->Body));
  305. fwrite($objResource, "\n\n");
  306. fwrite($objResource, sprintf("--%s--\n", $strAltBoundary));
  307. } else {
  308. fwrite($objResource, sprintf("Content-Type: text/plain; charset=\"%s\"\n", $strEncodingType));
  309. fwrite($objResource, sprintf("Content-Transfer-Encoding: quoted-printable\n\n"));
  310. fwrite($objResource, "\n" . self::QuotedPrintableEncode($objMessage->Body));
  311. }
  312. // Send: File Attachments
  313. if($objMessage->HasFiles) {
  314. foreach ($objArray = $objMessage->FileArray as $objFile) {
  315. fwrite($objResource, sprintf("--%s\n", $strBoundary));
  316. fwrite($objResource, sprintf("Content-Type: %s;\n", $objFile->MimeType ));
  317. fwrite($objResource, sprintf(" name=\"%s\"\n", $objFile->FileName ));
  318. fwrite($objResource, "Content-Transfer-Encoding: base64\n");
  319. fwrite($objResource, sprintf("Content-Length: %s\n", strlen($objFile->EncodedFileData)));
  320. fwrite($objResource, "Content-Disposition: attachment;\n");
  321. fwrite($objResource, sprintf(" filename=\"%s\"\n\n", $objFile->FileName));
  322. fwrite($objResource, $objFile->EncodedFileData);
  323. // foreach (explode("\n", $objFile->EncodedFileData) as $strLine) {
  324. // $strLine = trim($strLine);
  325. // fwrite($objResource, $strLine . "\n");
  326. // }
  327. }
  328. }
  329. // close a message with these boundaries if the message had files or had html
  330. if($objMessage->HasFiles || $objMessage->HtmlBody)
  331. fwrite($objResource, sprintf("\n\n--%s--\n", $strBoundary)); // send end of file attachments...
  332. // Send: Message End
  333. fwrite($objResource, "\n.\n");
  334. if (!feof($objResource)) {
  335. $strResponse = fgets($objResource, 4096);
  336. // Check for a "250" response
  337. if (!QEmailServer::$TestMode)
  338. if ((strpos($strResponse, "250") === false) || (strpos($strResponse, "250") != 0))
  339. throw new QEmailException(sprintf('Error Response on DATA finish: %s', $strResponse));
  340. }
  341. // Send: QUIT
  342. fwrite($objResource, "QUIT\n");
  343. if (!feof($objResource))
  344. $strResponse = fgets($objResource, 4096);
  345. // Close the Resource
  346. fclose($objResource);
  347. if (QEmailServer::$TestMode)
  348. chmod($strFileName, 0777);
  349. }
  350. }
  351. // PHP does not allow Static Class Variables to be set to non-constants.
  352. // So we set QEmailServer's OriginatingServerIp to QApplication's ServerAddress here.
  353. QEmailServer::$OriginatingServerIp = QApplication::$ServerAddress;
  354. class QEmailException extends QCallerException {}
  355. class QEmailAttachment extends QBaseClass {
  356. protected $strFilePath;
  357. protected $strMimeType;
  358. protected $strFileName;
  359. protected $strEncodedFileData;
  360. public function __construct($strFilePath, $strSpecifiedMimeType = null, $strSpecifiedFileName = null) {
  361. // Set File Path
  362. if (!is_file(realpath($strFilePath)))
  363. throw new QCallerException('File Not Found: ' . $strFilePath);
  364. $this->strFilePath = realpath($strFilePath);
  365. // Set the File MIME Type -- if Explicitly Set, use it
  366. if ($strSpecifiedMimeType)
  367. $this->strMimeType = $strSpecifiedMimeType;
  368. // otherwise, use QMimeType to determine
  369. else
  370. $this->strMimeType = QMimeType::GetMimeTypeForFile($this->strFilePath);
  371. // Set the File Name -- if explicitly set, use it
  372. if ($strSpecifiedFileName)
  373. $this->strFileName = $strSpecifiedFileName;
  374. // Otherwise, use basename() to determine
  375. else
  376. $this->strFileName = basename($this->strFilePath);
  377. // Read file into a Base64 Encoded Data Stream
  378. $strFileContents = file_get_contents($this->strFilePath, false);
  379. $this->strEncodedFileData = chunk_split(base64_encode($strFileContents));
  380. }
  381. public function __get($strName) {
  382. switch ($strName) {
  383. case 'FilePath': return $this->strFilePath;
  384. case 'MimeType': return $this->strMimeType;
  385. case 'FileName': return $this->strFileName;
  386. case 'EncodedFileData': return $this->strEncodedFileData;
  387. default:
  388. try {
  389. return parent::__get($strName);
  390. } catch (QCallerException $objExc) {
  391. $objExc->IncrementOffset();
  392. throw $objExc;
  393. }
  394. }
  395. }
  396. }
  397. class QEmailStringAttachment extends QEmailAttachment {
  398. public function __construct($strContent, $strSpecifiedMimeType, $strSpecifiedFileName) {
  399. // Set the File MIME Type -- if Explicitly Set, use it
  400. if ($strSpecifiedMimeType) {
  401. $this->strMimeType = $strSpecifiedMimeType;
  402. }
  403. // Set the File Name -- if explicitly set, use it
  404. if ($strSpecifiedFileName) {
  405. $this->strFileName = $strSpecifiedFileName;
  406. }
  407. // Read file into a Base64 Encoded Data Stream
  408. $this->strEncodedFileData = chunk_split(base64_encode($strContent));
  409. }
  410. }
  411. class QEmailMessage extends QBaseClass {
  412. protected $strFrom;
  413. protected $strTo;
  414. protected $strSubject;
  415. protected $strBody;
  416. protected $strHtmlBody;
  417. protected $strCc;
  418. protected $strBcc;
  419. protected $strHeaderArray = array();
  420. protected $objFileArray = array();
  421. public function AddAttachment(QEmailAttachment $objFile) {
  422. $this->objFileArray[$objFile->FileName] = $objFile;
  423. }
  424. public function Attach($strFilePath, $strSpecifiedMimeType = null, $strSpecifiedFileName = null) {
  425. $this->AddAttachment(new QEmailAttachment($strFilePath, $strSpecifiedMimeType, $strSpecifiedFileName));
  426. }
  427. public function RemoveAttachment($strFileName) {
  428. if (array_key_exists($strName, $this->objFileArray))
  429. unset($this->objFileArray[$strName]);
  430. }
  431. public function SetHeader($strName, $strValue) {
  432. $this->strHeaderArray[$strName] = $strValue;
  433. }
  434. public function GetHeader($strName) {
  435. if (array_key_exists($strName, $this->strHeaderArray))
  436. return $this->strHeaderArray[$strName];
  437. return null;
  438. }
  439. public function RemoveHeader($strName, $strValue) {
  440. if (array_key_exists($strName, $this->strHeaderArray))
  441. unset($this->strHeaderArray[$strName]);
  442. }
  443. public function __construct($strFrom = null, $strTo = null, $strSubject = null, $strBody = null) {
  444. $this->strFrom = $strFrom;
  445. $this->strTo = $strTo;
  446. // We must cleanup the Subject and Body -- use the Property to set
  447. $this->Subject = $strSubject;
  448. $this->Body = $strBody;
  449. }
  450. public function __get($strName) {
  451. switch ($strName) {
  452. case 'From': return $this->strFrom;
  453. case 'To': return $this->strTo;
  454. case 'Subject': return $this->strSubject;
  455. case 'Body': return $this->strBody;
  456. case 'HtmlBody': return $this->strHtmlBody;
  457. case 'Cc': return $this->strCc;
  458. case 'Bcc': return $this->strBcc;
  459. case 'HeaderArray': return $this->strHeaderArray;
  460. case 'FileArray': return $this->objFileArray;
  461. case 'HasFiles': return (count($this->objFileArray) > 0) ? true : false;
  462. default:
  463. try {
  464. return parent::__get($strName);
  465. } catch (QCallerException $objExc) {
  466. $objExc->IncrementOffset();
  467. throw $objExc;
  468. }
  469. }
  470. }
  471. public function __set($strName, $mixValue) {
  472. try {
  473. switch ($strName) {
  474. case 'From': return ($this->strFrom = QType::Cast($mixValue, QType::String));
  475. case 'To': return ($this->strTo = QType::Cast($mixValue, QType::String));
  476. case 'Subject':
  477. $strSubject = trim(QType::Cast($mixValue, QType::String));
  478. $strSubject = str_replace("\r", "", $strSubject);
  479. $strSubject = str_replace("\n", " ", $strSubject);
  480. return ($this->strSubject = $strSubject);
  481. case 'Body':
  482. $strBody = QType::Cast($mixValue, QType::String);
  483. $strBody = str_replace("\r", "", $strBody);
  484. $strBody = str_replace("\n", "\n", $strBody);
  485. $strBody = str_replace("\n.", "\n..", $strBody);
  486. return ($this->strBody = $strBody);
  487. case 'HtmlBody':
  488. $strHtmlBody = QType::Cast($mixValue, QType::String);
  489. $strHtmlBody = str_replace("\r", "", $strHtmlBody);
  490. $strHtmlBody = str_replace("\n", "\n", $strHtmlBody);
  491. $strHtmlBody = str_replace("\n.", "\n..", $strHtmlBody);
  492. return ($this->strHtmlBody = $strHtmlBody);
  493. case 'Cc': return ($this->strCc = QType::Cast($mixValue, QType::String));
  494. case 'Bcc': return ($this->strBcc = QType::Cast($mixValue, QType::String));
  495. default: return (parent::__set($strName, $mixValue));
  496. }
  497. } catch (QInvalidCastException $objExc) {
  498. $objExc->IncrementOffset();
  499. throw $objExc;
  500. }
  501. }
  502. }
  503. ?>