PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/core/class/smtps.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 1806 lines | 903 code | 190 blank | 713 comment | 99 complexity | e9316b1a914706660beb7b8d96e2a13c MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. * Copyright (C) Walter Torres <walter@torres.ws> [with a *lot* of help!]
  4. * Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2006-2011 Regis Houssin
  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 3 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, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/class/smtps.class.php
  22. * \brief Class to construct and send SMTP compliant email, even to a secure
  23. * SMTP server, regardless of platform.
  24. * Goals:
  25. * - mime compliant
  26. * - multiple Reciptiants
  27. * - TO
  28. * - CC
  29. * - BCC
  30. * - multi-part message
  31. * - plain text
  32. * - HTML
  33. * - inline attachments
  34. * - attachments
  35. * - GPG access
  36. * This Class is based off of 'SMTP PHP MAIL' by Dirk Paehl, http://www.paehl.de
  37. */
  38. /**
  39. * Class to construct and send SMTP compliant email, even
  40. * to a secure SMTP server, regardless of platform.
  41. */
  42. class SMTPs
  43. {
  44. /**
  45. * Host Name or IP of SMTP Server to use
  46. */
  47. var $_smtpsHost = 'localhost';
  48. /**
  49. * SMTP Server Port definition. 25 is default value
  50. * This can be defined via a INI file or via a setter method
  51. */
  52. var $_smtpsPort = '25';
  53. /**
  54. * Secure SMTP Server access ID
  55. * This can be defined via a INI file or via a setter method
  56. */
  57. var $_smtpsID = null;
  58. /**
  59. * Secure SMTP Server access Password
  60. * This can be defined via a INI file or via a setter method
  61. */
  62. var $_smtpsPW = null;
  63. /**
  64. * Who sent the Message
  65. * This can be defined via a INI file or via a setter method
  66. */
  67. var $_msgFrom = null;
  68. /**
  69. * Where are replies and errors to be sent to
  70. * This can be defined via a INI file or via a setter method
  71. */
  72. var $_msgReplyTo = null;
  73. /**
  74. * Who will the Message be sent to; TO, CC, BCC
  75. * Multi-diminsional array containg addresses the message will
  76. * be sent TO, CC or BCC
  77. */
  78. var $_msgRecipients = null;
  79. /**
  80. * Message Subject
  81. */
  82. var $_msgSubject = null;
  83. /**
  84. * Message Content
  85. */
  86. var $_msgContent = null;
  87. /**
  88. * Custom X-Headers
  89. */
  90. var $_msgXheader = null;
  91. /**
  92. * Character set
  93. * Defaulted to 'iso-8859-1'
  94. */
  95. var $_smtpsCharSet = 'iso-8859-1';
  96. /**
  97. * Message Sensitivity
  98. * Defaults to ZERO - None
  99. */
  100. var $_msgSensitivity = 0;
  101. /**
  102. * Message Sensitivity
  103. */
  104. var $_arySensitivity = array ( false,
  105. 'Personal',
  106. 'Private',
  107. 'Company Confidential' );
  108. /**
  109. * Message Sensitivity
  110. * Defaults to 3 - Normal
  111. */
  112. var $_msgPriority = 3;
  113. /**
  114. * Message Priority
  115. */
  116. var $_aryPriority = array ( 'Bulk',
  117. 'Highest',
  118. 'High',
  119. 'Normal',
  120. 'Low',
  121. 'Lowest' );
  122. /**
  123. * Content-Transfer-Encoding
  124. * Defaulted to 0 - 7bit
  125. */
  126. var $_smtpsTransEncodeType = 0;
  127. /**
  128. * Content-Transfer-Encoding
  129. */
  130. var $_smtpsTransEncodeTypes = array( '7bit', // Simple 7-bit ASCII
  131. '8bit', // 8-bit coding with line termination characters
  132. 'base64', // 3 octets encoded into 4 sextets with offset
  133. 'binary', // Arbitrary binary stream
  134. 'mac-binhex40', // Macintosh binary to hex encoding
  135. 'quoted-printable', // Mostly 7-bit, with 8-bit characters encoded as "=HH"
  136. 'uuencode' ); // UUENCODE encoding
  137. /**
  138. * Content-Transfer-Encoding
  139. * Defaulted to '7bit'
  140. */
  141. var $_smtpsTransEncode = '7bit';
  142. /**
  143. * Boundary String for MIME seperation
  144. */
  145. var $_smtpsBoundary = null;
  146. /**
  147. * Related Boundary
  148. */
  149. var $_smtpsRelatedBoundary = null;
  150. /**
  151. * Alternative Boundary
  152. */
  153. var $_smtpsAlternativeBoundary = null;
  154. /**
  155. * Determines the method inwhich the message are to be sent.
  156. * - 'sockets' [0] - conect via network to SMTP server - default
  157. * - 'pipe [1] - use UNIX path to EXE
  158. * - 'phpmail [2] - use the PHP built-in mail function
  159. * NOTE: Only 'sockets' is implemented
  160. */
  161. var $_transportType = 0;
  162. /**
  163. * If '$_transportType' is set to '1', then this variable is used
  164. * to define the UNIX file system path to the sendmail execuable
  165. */
  166. var $_mailPath = '/usr/lib/sendmail';
  167. /**
  168. * Sets the SMTP server timeout in seconds.
  169. */
  170. var $_smtpTimeout = 10;
  171. /**
  172. * Determines whether to calculate message MD5 checksum.
  173. */
  174. var $_smtpMD5 = false;
  175. /**
  176. * Class error codes and messages
  177. */
  178. var $_smtpsErrors = null;
  179. /**
  180. * Defines log level
  181. * 0 - no logging
  182. * 1 - connectivity logging
  183. * 2 - message generation logging
  184. * 3 - detail logging
  185. */
  186. var $_log_level = 0;
  187. /**
  188. * Place Class in" debug" mode
  189. */
  190. var $_debug = false;
  191. // DOL_CHANGE LDR
  192. var $log = '';
  193. var $_errorsTo = '';
  194. var $_deliveryReceipt = 0;
  195. /**
  196. * Set delivery receipt
  197. *
  198. * @param int $_val Value
  199. * @return void
  200. */
  201. function setDeliveryReceipt($_val = 0)
  202. {
  203. $this->_deliveryReceipt = $_val;
  204. }
  205. /**
  206. * get delivery receipt
  207. *
  208. * @return int Delivery receipt
  209. */
  210. function getDeliveryReceipt()
  211. {
  212. return $this->_deliveryReceipt;
  213. }
  214. /**
  215. * Set errors to
  216. *
  217. * @param string $_strErrorsTo Errors to
  218. * @return void
  219. */
  220. function setErrorsTo($_strErrorsTo)
  221. {
  222. if ( $_strErrorsTo )
  223. $this->_errorsTo = $this->_strip_email($_strErrorsTo);
  224. }
  225. /**
  226. * Get errors to
  227. *
  228. * @param boolean $_part Variant
  229. * @return string Errors to
  230. */
  231. function getErrorsTo($_part = true )
  232. {
  233. $_retValue = '';
  234. if ( $_part === true )
  235. $_retValue = $this->_errorsTo;
  236. else
  237. $_retValue = $this->_errorsTo[$_part];
  238. return $_retValue;
  239. }
  240. /**
  241. * Set debug
  242. *
  243. * @param boolean $_vDebug Value for debug
  244. * @return void
  245. */
  246. function setDebug($_vDebug = false )
  247. {
  248. $this->_debug = $_vDebug;
  249. }
  250. /**
  251. * build RECIPIENT List, all addresses who will recieve this message
  252. *
  253. * @return void
  254. */
  255. function buildRCPTlist()
  256. {
  257. // Pull TO list
  258. $_aryToList = $this->getTO();
  259. }
  260. /**
  261. * Attempt a connection to mail server
  262. *
  263. * @return mixed $_retVal Boolean indicating success or failure on connection
  264. */
  265. function _server_connect()
  266. {
  267. // Default return value
  268. $_retVal = true;
  269. // We have to make sure the HOST given is valid
  270. // This is done here because '@fsockopen' will not give me this
  271. // information if it failes to connect because it can't find the HOST
  272. $host=$this->getHost();
  273. $host=preg_replace('@tcp://@i','',$host); // Remove prefix
  274. $host=preg_replace('@ssl://@i','',$host); // Remove prefix
  275. // DOL_CHANGE LDR
  276. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  277. if ( (! is_ip($host)) && ((gethostbyname($host)) == $host))
  278. {
  279. $this->_setErr(99, $host . ' is either offline or is an invalid host name.');
  280. $_retVal = false;
  281. }
  282. else
  283. {
  284. //See if we can connect to the SMTP server
  285. if ($this->socket = @fsockopen(
  286. $this->getHost(), // Host to 'hit', IP or domain
  287. $this->getPort(), // which Port number to use
  288. $this->errno, // actual system level error
  289. $this->errstr, // and any text that goes with the error
  290. $this->_smtpTimeout
  291. )) // timeout for reading/writing data over the socket
  292. {
  293. // Fix from PHP SMTP class by 'Chris Ryan'
  294. // Sometimes the SMTP server takes a little longer to respond
  295. // so we will give it a longer timeout for the first read
  296. // Windows still does not have support for this timeout function
  297. if (function_exists('stream_set_timeout')) stream_set_timeout($this->socket, $this->_smtpTimeout, 0);
  298. // Check response from Server
  299. if ( $_retVal = $this->server_parse($this->socket, "220") )
  300. $_retVal = $this->socket;
  301. }
  302. // This connection attempt failed.
  303. else
  304. {
  305. // DOL_CHANGE LDR
  306. if (empty($this->errstr)) $this->errstr='Failed to connect with fsockopen host='.$this->getHost().' port='.$this->getPort();
  307. $this->_setErr($this->errno, $this->errstr);
  308. $_retVal = false;
  309. }
  310. }
  311. return $_retVal;
  312. }
  313. /**
  314. * Attempt mail server authentication for a secure connection
  315. *
  316. * @return mixed $_retVal Boolean indicating success or failure of authentication
  317. */
  318. function _server_authenticate()
  319. {
  320. // Send the RFC2554 specified EHLO.
  321. // This improvment as provided by 'SirSir' to
  322. // accomodate both SMTP AND ESMTP capable servers
  323. $host=$this->getHost();
  324. $host=preg_replace('@tcp://@i','',$host); // Remove prefix
  325. $host=preg_replace('@ssl://@i','',$host); // Remove prefix
  326. if ( $_retVal = $this->socket_send_str('EHLO ' . $host, '250') )
  327. {
  328. // Send Authentication to Server
  329. // Check for errors along the way
  330. $this->socket_send_str('AUTH LOGIN', '334');
  331. // User name will not return any error, server will take anything we give it.
  332. $this->socket_send_str(base64_encode($this->_smtpsID), '334');
  333. // The error here just means the ID/password combo doesn't work.
  334. // There is not a method to determine which is the problem, ID or password
  335. if ( ! $_retVal = $this->socket_send_str(base64_encode($this->_smtpsPW), '235') )
  336. $this->_setErr(130, 'Invalid Authentication Credentials.');
  337. }
  338. else
  339. {
  340. $this->_setErr(126, '"' . $host . '" does not support authenticated connections.');
  341. }
  342. return $_retVal;
  343. }
  344. /**
  345. * Now send the message
  346. *
  347. * @param boolean $_bolTestMsg whether to run this method in 'Test' mode.
  348. * @param boolean $_bolDebug whether to log all communication between this Class and the Mail Server.
  349. * @return mixed void
  350. * $_strMsg If this is run in 'Test' mode, the actual message structure will be returned
  351. */
  352. function sendMsg($_bolTestMsg = false, $_bolDebug = false)
  353. {
  354. /**
  355. * Default return value
  356. */
  357. $_retVal = false;
  358. // Connect to Server
  359. if ( $this->socket = $this->_server_connect() )
  360. {
  361. // If a User ID *and* a password is given, assume Authentication is desired
  362. if( !empty($this->_smtpsID) && !empty($this->_smtpsPW) )
  363. {
  364. // Send the RFC2554 specified EHLO.
  365. $_retVal = $this->_server_authenticate();
  366. }
  367. // This is a "normal" SMTP Server "handshack"
  368. else
  369. {
  370. // Send the RFC821 specified HELO.
  371. $host=$this->getHost();
  372. $host=preg_replace('@tcp://@i','',$host); // Remove prefix
  373. $host=preg_replace('@ssl://@i','',$host); // Remove prefix
  374. $_retVal = $this->socket_send_str('HELO ' . $host, '250');
  375. }
  376. // Well, did we get to the server?
  377. if ( $_retVal )
  378. {
  379. // From this point onward most server response codes should be 250
  380. // Specify who the mail is from....
  381. // This has to be the raw email address, strip the "name" off
  382. $this->socket_send_str('MAIL FROM: ' . $this->getFrom('addr'), '250');
  383. // 'RCPT TO:' must be given a single address, so this has to loop
  384. // through the list of addresses, regardless of TO, CC or BCC
  385. // and send it out "single file"
  386. foreach ( $this->get_RCPT_list() as $_address )
  387. {
  388. /*
  389. * TODO
  390. * After each 'RCPT TO:' is sent, we need to make sure it was kosher,
  391. * if not, the whole message will fail
  392. * If any email address fails, we will need to RESET the connection,
  393. * mark the last address as "bad" and start the address loop over again.
  394. * If any address fails, the entire message fails.
  395. */
  396. $this->socket_send_str('RCPT TO: <' . $_address . '>', '250');
  397. }
  398. // Tell the server we are ready to start sending data
  399. // with any custom headers...
  400. // This is the last response code we look for until the end of the message.
  401. $this->socket_send_str('DATA', '354');
  402. // Now we are ready for the message...
  403. // Ok, all the ingredients are mixed in let's cook this puppy...
  404. $this->socket_send_str($this->getHeader().$this->getBodyContent() . "\r\n" . '.', '250');
  405. // Now tell the server we are done and close the socket...
  406. fputs($this->socket, 'QUIT');
  407. fclose($this->socket);
  408. }
  409. }
  410. return $_retVal;
  411. }
  412. // =============================================================
  413. // ** Setter & Getter methods
  414. // ** Basic System configuration
  415. /**
  416. * setConfig() is used to populate select class properties from either
  417. * a user defined INI file or the systems 'php.ini' file
  418. *
  419. * If a user defined INI is to be used, the files complete path is passed
  420. * as the method single parameter. The INI can define any class and/or
  421. * user properties. Only properties defined within this file will be setter
  422. * and/or orverwritten
  423. *
  424. * If the systems 'php.ini' file is to be used, the method is called without
  425. * parameters. In this case, only HOST, PORT and FROM properties will be set
  426. * as they are the only properties that are defined within the 'php.ini'.
  427. *
  428. * If secure SMTP is to be used, the user ID and Password can be defined with
  429. * the user INI file, but the properties are not defined with the systems
  430. * 'php.ini'file, they must be defined via their setter methods
  431. *
  432. * This method can be called twice, if desired. Once without a parameter to
  433. * load the properties as defined within the systems 'php.ini' file, and a
  434. * second time, with a path to a user INI file for other properties to be
  435. * defined.
  436. *
  437. * @param mixed $_strConfigPath path to config file or VOID
  438. * @return void
  439. */
  440. function setConfig ( $_strConfigPath = null )
  441. {
  442. /**
  443. * Returns constructed SELECT Object string or boolean upon failure
  444. * Default value is set at TRUE
  445. */
  446. $_retVal = true;
  447. // if we have a path...
  448. if ( ! empty ($_strConfigPath) )
  449. {
  450. // If the path is not valid, this will NOT generate an error,
  451. // it will simply return FALSE.
  452. if ( ! @include ( $_strConfigPath ) )
  453. {
  454. $this->_setErr(110, '"' . $_strConfigPath . '" is not a valid path.');
  455. $_retVal = false;
  456. }
  457. }
  458. // Read the Systems php.ini file
  459. else
  460. {
  461. // Set these properties ONLY if they are set in the php.ini file.
  462. // Otherwise the default values will be used.
  463. if ( $_host = ini_get('SMTPs') )
  464. $this->setHost($_host);
  465. if ( $_port = ini_get('smtp_port') )
  466. $this->setPort($_port);
  467. if ( $_from = ini_get('sendmail_from') )
  468. $this->setFrom($_from);
  469. }
  470. // Send back what we have
  471. return $_retVal;
  472. }
  473. /**
  474. * Determines the method inwhich the messages are to be sent.
  475. * - 'sockets' [0] - conect via network to SMTP server
  476. * - 'pipe [1] - use UNIX path to EXE
  477. * - 'phpmail [2] - use the PHP built-in mail function
  478. *
  479. * @param int $_type Interger value representing Mail Transport Type
  480. * @return void
  481. */
  482. function setTransportType($_type = 0)
  483. {
  484. if ( ( is_numeric($_type) ) &&
  485. ( ( $_type >= 0 ) && ( $_type <= 3 ) ) )
  486. $this->_transportType = $_type;
  487. }
  488. /**
  489. * Return the method inwhich the message is to be sent.
  490. * - 'sockets' [0] - conect via network to SMTP server
  491. * - 'pipe [1] - use UNIX path to EXE
  492. * - 'phpmail [2] - use the PHP built-in mail function
  493. *
  494. * @return int $_strHost Host Name or IP of the Mail Server to use
  495. */
  496. function getTransportType()
  497. {
  498. return $this->_transportType;
  499. }
  500. /**
  501. * Path to the sendmail execuable
  502. *
  503. * @param string $_path Path to the sendmail execuable
  504. * @return void
  505. *
  506. */
  507. function setMailPath($_path)
  508. {
  509. // This feature is not yet implemented
  510. return true;
  511. //if ( $_path ) $this->_mailPath = $_path;
  512. }
  513. /**
  514. * Defines the Host Name or IP of the Mail Server to use.
  515. * This is defaulted to 'localhost'
  516. * This is used only with 'socket' based mail transmission
  517. *
  518. * @param string $_strHost Host Name or IP of the Mail Server to use
  519. * @return void
  520. */
  521. function setHost($_strHost)
  522. {
  523. if ( $_strHost )
  524. $this->_smtpsHost = $_strHost;
  525. }
  526. /**
  527. * Retrieves the Host Name or IP of the Mail Server to use
  528. * This is used only with 'socket' based mail transmission
  529. *
  530. * @return string $_strHost Host Name or IP of the Mail Server to use
  531. */
  532. function getHost()
  533. {
  534. return $this->_smtpsHost;
  535. }
  536. /**
  537. * Defines the Port Number of the Mail Server to use
  538. * This is defaulted to '25'
  539. * This is used only with 'socket' based mail transmission
  540. *
  541. * @param int $_intPort Port Number of the Mail Server to use
  542. * @return void
  543. */
  544. function setPort($_intPort)
  545. {
  546. if ( ( is_numeric($_intPort) ) &&
  547. ( ( $_intPort >= 1 ) && ( $_intPort <= 65536 ) ) )
  548. $this->_smtpsPort = $_intPort;
  549. }
  550. /**
  551. * Retrieves the Port Number of the Mail Server to use
  552. * This is used only with 'socket' based mail transmission
  553. *
  554. * @return string Port Number of the Mail Server to use
  555. */
  556. function getPort()
  557. {
  558. return $this->_smtpsPort;
  559. }
  560. /**
  561. * User Name for authentication on Mail Server
  562. *
  563. * @param string $_strID User Name for authentication on Mail Server
  564. * @return void
  565. */
  566. function setID($_strID)
  567. {
  568. $this->_smtpsID = $_strID;
  569. }
  570. /**
  571. * Retrieves the User Name for authentication on Mail Server
  572. *
  573. * @return string User Name for authentication on Mail Server
  574. */
  575. function getID()
  576. {
  577. return $this->_smtpsID;
  578. }
  579. /**
  580. * User Password for authentication on Mail Server
  581. *
  582. * @param string $_strPW User Password for authentication on Mail Server
  583. * @return void
  584. */
  585. function setPW($_strPW)
  586. {
  587. $this->_smtpsPW = $_strPW;
  588. }
  589. /**
  590. * Retrieves the User Password for authentication on Mail Server
  591. *
  592. * @return string User Password for authentication on Mail Server
  593. */
  594. function getPW()
  595. {
  596. return $this->_smtpsPW;
  597. }
  598. /**
  599. * Character set used for current message
  600. * Character set is defaulted to 'iso-8859-1';
  601. *
  602. * @param string $_strCharSet Character set used for current message
  603. * @return void
  604. */
  605. function setCharSet($_strCharSet)
  606. {
  607. if ( $_strCharSet )
  608. $this->_smtpsCharSet = $_strCharSet;
  609. }
  610. /**
  611. * Retrieves the Character set used for current message
  612. *
  613. * @return string $_smtpsCharSet Character set used for current message
  614. */
  615. function getCharSet()
  616. {
  617. return $this->_smtpsCharSet;
  618. }
  619. /**
  620. * Content-Transfer-Encoding, Defaulted to '7bit'
  621. * This can be changed for 2byte characers sets
  622. * Known Encode Types
  623. * - 7bit Simple 7-bit ASCII
  624. * - 8bit 8-bit coding with line termination characters
  625. * - base64 3 octets encoded into 4 sextets with offset
  626. * - binary Arbitrary binary stream
  627. * - mac-binhex40 Macintosh binary to hex encoding
  628. * - quoted-printable Mostly 7-bit, with 8-bit characters encoded as "=HH"
  629. * - uuencode UUENCODE encoding
  630. *
  631. * @param string $_strTransEncode Content-Transfer-Encoding
  632. * @return void
  633. */
  634. function setTransEncode ( $_strTransEncode )
  635. {
  636. if (array_search($_strTransEncode, $this->_smtpsTransEncodeTypes))
  637. $this->_smtpsTransEncode = $_strTransEncode;
  638. }
  639. /**
  640. * Retrieves the Content-Transfer-Encoding
  641. *
  642. * @return string $_smtpsTransEncode Content-Transfer-Encoding
  643. */
  644. function getTransEncode()
  645. {
  646. return $this->_smtpsTransEncode;
  647. }
  648. /**
  649. * Content-Transfer-Encoding, Defaulted to '0' [ZERO]
  650. * This can be changed for 2byte characers sets
  651. * Known Encode Types
  652. * - [0] 7bit Simple 7-bit ASCII
  653. * - [1] 8bit 8-bit coding with line termination characters
  654. * - [2] base64 3 octets encoded into 4 sextets with offset
  655. * - [3] binary Arbitrary binary stream
  656. * - [4] mac-binhex40 Macintosh binary to hex encoding
  657. * - [5] quoted-printable Mostly 7-bit, with 8-bit characters encoded as "=HH"
  658. * - [6] uuencode UUENCODE encoding
  659. *
  660. * @param string $_strTransEncodeType Content-Transfer-Encoding
  661. * @return void
  662. *
  663. */
  664. function setTransEncodeType($_strTransEncodeType)
  665. {
  666. if (array_search($_strTransEncodeType, $this->_smtpsTransEncodeTypes))
  667. $this->_smtpsTransEncodeType = $_strTransEncodeType;
  668. }
  669. /**
  670. * Retrieves the Content-Transfer-Encoding
  671. *
  672. * @return string Content-Transfer-Encoding
  673. */
  674. function getTransEncodeType()
  675. {
  676. return $this->_smtpsTransEncodeTypes[$this->_smtpsTransEncodeType];
  677. }
  678. // ** Message Construction
  679. /**
  680. * FROM Address from which mail will be sent
  681. *
  682. * @param string $_strFrom Address from which mail will be sent
  683. * @return void
  684. */
  685. function setFrom($_strFrom)
  686. {
  687. if ( $_strFrom )
  688. $this->_msgFrom = $this->_strip_email($_strFrom);
  689. }
  690. /**
  691. * Retrieves the Address from which mail will be sent
  692. *
  693. * @param boolean $_part To "strip" 'Real name' from address
  694. * @return string Address from which mail will be sent
  695. */
  696. function getFrom($_part = true)
  697. {
  698. $_retValue = '';
  699. if ( $_part === true )
  700. $_retValue = $this->_msgFrom;
  701. else
  702. $_retValue = $this->_msgFrom[$_part];
  703. return $_retValue;
  704. }
  705. /**
  706. * Inserts given addresses into structured format.
  707. * This method takes a list of given addresses, via an array
  708. * or a COMMA delimted string, and inserts them into a highly
  709. * structured array. This array is designed to remove duplicate
  710. * addresses and to sort them by Domain.
  711. *
  712. * @param string $_type TO, CC, or BCC lists to add addrresses into
  713. * @param mixed $_addrList Array or COMMA delimited string of addresses
  714. * @return void
  715. *
  716. */
  717. function _buildAddrList( $_type, $_addrList )
  718. {
  719. // Pull existing list
  720. $aryHost = $this->_msgRecipients;
  721. // Only run this if we have something
  722. if ( !empty ($_addrList ))
  723. {
  724. // $_addrList can be a STRING or an array
  725. if ( is_string($_addrList) )
  726. {
  727. // This could be a COMMA delimited string
  728. if ( strstr($_addrList, ',') )
  729. // "explode "list" into an array
  730. $_addrList = explode(',', $_addrList);
  731. // Stick it in an array
  732. else
  733. $_addrList = array($_addrList);
  734. }
  735. // take the array of addresses and split them further
  736. foreach ( $_addrList as $_strAddr )
  737. {
  738. // Strip off the end '>'
  739. $_strAddr = str_replace('>', '', $_strAddr);
  740. // Seperate "Real Name" from eMail address
  741. $_tmpaddr = null;
  742. $_tmpaddr = explode('<', $_strAddr);
  743. // We have a "Real Name" and eMail address
  744. if ( count($_tmpaddr) == 2 )
  745. {
  746. $_tmpHost = explode('@', $_tmpaddr[1]);
  747. $_tmpaddr[0] = trim($_tmpaddr[0], ' ">');
  748. $aryHost[$_tmpHost[1]][$_type][$_tmpHost[0]] = $_tmpaddr[0];
  749. }
  750. // We only have an eMail address
  751. else
  752. {
  753. // Strip off the beggining '<'
  754. $_strAddr = str_replace('<', '', $_strAddr);
  755. $_tmpHost = explode('@', $_strAddr);
  756. $_tmpHost[0] = trim($_tmpHost[0]);
  757. $_tmpHost[1] = trim($_tmpHost[1]);
  758. $aryHost[$_tmpHost[1]][$_type][$_tmpHost[0]] = '';
  759. }
  760. }
  761. }
  762. // replace list
  763. $this->_msgRecipients = $aryHost;
  764. }
  765. /**
  766. * Returns an array of the various parts of an email address
  767. * This assumes a well formed address:
  768. * - "Real name" <username@domain.tld>
  769. * - "Real Name" is optional
  770. * - if "Real Name" does not exist, the angle brackets are optional
  771. * This will split an email address into 4 or 5 parts.
  772. * - $_aryEmail[org] = orignal string
  773. * - $_aryEmail[real] = "real name" - if there is one
  774. * - $_aryEmail[addr] = address part "username@domain.tld"
  775. * - $_aryEmail[host] = "domain.tld"
  776. * - $_aryEmail[user] = "userName"
  777. *
  778. * @param string $_strAddr Email address
  779. * @return array An array of the various parts of an email address
  780. */
  781. function _strip_email($_strAddr)
  782. {
  783. // Keep the orginal
  784. $_aryEmail['org'] = $_strAddr;
  785. // Set entire string to Lower Case
  786. $_strAddr = strtolower($_strAddr);
  787. // Drop "stuff' off the end
  788. $_strAddr = trim($_strAddr, ' ">');
  789. // Seperate "Real Name" from eMail address, if we have one
  790. $_tmpAry = explode('<', $_strAddr);
  791. // Do we have a "Real name"
  792. if ( count($_tmpAry) == 2 )
  793. {
  794. // We may not really have a "Real Name"
  795. if ( $_tmpAry[0])
  796. $_aryEmail['real'] = trim($_tmpAry[0], ' ">');
  797. $_aryEmail['addr'] = $_tmpAry[1];
  798. }
  799. else
  800. $_aryEmail['addr'] = $_tmpAry[0];
  801. // Pull User Name and Host.tld apart
  802. list($_aryEmail['user'], $_aryEmail['host'] ) = explode('@', $_aryEmail['addr']);
  803. // Put the brackets back around the address
  804. $_aryEmail['addr'] = '<' . $_aryEmail['addr'] . '>';
  805. return $_aryEmail;
  806. }
  807. /**
  808. * Returns an array of bares addresses for use with 'RCPT TO:'
  809. * This is a "build as you go" method. Each time this method is called
  810. * the underlaying array is destroyed and reconstructed.
  811. *
  812. * @return array Returns an array of bares addresses
  813. */
  814. function get_RCPT_list()
  815. {
  816. /**
  817. * An array of bares addresses for use with 'RCPT TO:'
  818. */
  819. $_RCPT_list=array();
  820. // walk down Recipients array and pull just email addresses
  821. foreach ( $this->_msgRecipients as $_host => $_list )
  822. {
  823. foreach ( $_list as $_subList )
  824. {
  825. foreach ( $_subList as $_name => $_addr )
  826. {
  827. // build RCPT list
  828. $_RCPT_list[] = $_name . '@' . $_host;
  829. }
  830. }
  831. }
  832. return $_RCPT_list;
  833. }
  834. /**
  835. * Returns an array of addresses for a specific type; TO, CC or BCC
  836. *
  837. * @param mixed $_which Which collection of adresses to return
  838. * @return array Array of emaill address
  839. */
  840. function get_email_list($_which = null)
  841. {
  842. // We need to know which address segment to pull
  843. if ( $_which )
  844. {
  845. // Make sure we have addresses to process
  846. if ( $this->_msgRecipients )
  847. {
  848. $_RCPT_list=array();
  849. // walk down Recipients array and pull just email addresses
  850. foreach ( $this->_msgRecipients as $_host => $_list )
  851. {
  852. if ( $this->_msgRecipients[$_host][$_which] )
  853. {
  854. foreach ( $this->_msgRecipients[$_host][$_which] as $_addr => $_realName )
  855. {
  856. if ( $_realName ) // DOL_CHANGE FIX
  857. {
  858. $_realName = '"' . $_realName . '"';
  859. $_RCPT_list[] = $_realName . ' <' . $_addr . '@' . $_host . '>';
  860. }
  861. else
  862. {
  863. $_RCPT_list[] = $_addr . '@' . $_host;
  864. }
  865. }
  866. }
  867. }
  868. return implode(', ', $_RCPT_list);
  869. }
  870. else
  871. {
  872. $this->_setErr(101, 'No eMail Address for message to be sent to.');
  873. return false;
  874. }
  875. }
  876. else
  877. {
  878. $this->_setErr(102, 'eMail type not defined.');
  879. return false;
  880. }
  881. }
  882. /**
  883. * TO Address[es] inwhich to send mail to
  884. *
  885. * @param mixed $_addrTo TO Address[es] inwhich to send mail to
  886. * @return void
  887. */
  888. function setTO($_addrTo)
  889. {
  890. if ( $_addrTo )
  891. $this->_buildAddrList('to', $_addrTo);
  892. }
  893. /**
  894. * Retrieves the TO Address[es] inwhich to send mail to
  895. *
  896. * @return string TO Address[es] inwhich to send mail to
  897. */
  898. function getTo()
  899. {
  900. return $this->get_email_list('to');
  901. }
  902. /**
  903. * CC Address[es] inwhich to send mail to
  904. *
  905. * @param string $_strCC CC Address[es] inwhich to send mail to
  906. * @return void
  907. */
  908. function setCC($_strCC)
  909. {
  910. if ( $_strCC )
  911. $this->_buildAddrList('cc', $_strCC);
  912. }
  913. /**
  914. * Retrieves the CC Address[es] inwhich to send mail to
  915. *
  916. * @return string CC Address[es] inwhich to send mail to
  917. */
  918. function getCC()
  919. {
  920. return $this->get_email_list('cc');
  921. }
  922. /**
  923. * BCC Address[es] inwhich to send mail to
  924. *
  925. * @param string $_strBCC Recipients BCC Address[es] inwhich to send mail to
  926. * @return void
  927. */
  928. function setBCC($_strBCC)
  929. {
  930. if ( $_strBCC )
  931. $this->_buildAddrList('bcc', $_strBCC);
  932. }
  933. /**
  934. * Retrieves the BCC Address[es] inwhich to send mail to
  935. *
  936. * @return string BCC Address[es] inwhich to send mail to
  937. */
  938. function getBCC()
  939. {
  940. return $this->get_email_list('bcc');
  941. }
  942. /**
  943. * Message Subject
  944. *
  945. * @param string $_strSubject Message Subject
  946. * @return void
  947. */
  948. function setSubject($_strSubject = '')
  949. {
  950. if ( $_strSubject )
  951. $this->_msgSubject = $_strSubject;
  952. }
  953. /**
  954. * Retrieves the Message Subject
  955. *
  956. * @return string Message Subject
  957. */
  958. function getSubject()
  959. {
  960. return $this->_msgSubject;
  961. }
  962. /**
  963. * Constructes and returns message header
  964. *
  965. * @return string Complete message header
  966. */
  967. function getHeader()
  968. {
  969. $_header = 'From: ' . $this->getFrom('org') . "\r\n"
  970. . 'To: ' . $this->getTO() . "\r\n";
  971. if ( $this->getCC() )
  972. $_header .= 'Cc: ' . $this->getCC() . "\r\n";
  973. if ( $this->getBCC() )
  974. $_header .= 'Bcc: ' . $this->getBCC() . "\r\n";
  975. $host=$this->getHost();
  976. $host=preg_replace('@tcp://@i','',$host); // Remove prefix
  977. $host=preg_replace('@ssl://@i','',$host); // Remove prefix
  978. //NOTE: Message-ID should probably contain the username of the user who sent the msg
  979. $_header .= 'Subject: ' . $this->getSubject() . "\r\n"
  980. . 'Date: ' . date("r") . "\r\n"
  981. . 'Message-ID: <' . time() . '.SMTPs@' . $host . ">\r\n";
  982. // . 'Read-Receipt-To: ' . $this->getFrom( 'org' ) . "\r\n"
  983. // . 'Return-Receipt-To: ' . $this->getFrom( 'org' ) . "\r\n";
  984. if ( $this->getSensitivity() )
  985. $_header .= 'Sensitivity: ' . $this->getSensitivity() . "\r\n";
  986. if ( $this->_msgPriority != 3 )
  987. $_header .= $this->getPriority();
  988. // DOL_CHANGE LDR
  989. if ( $this->getDeliveryReceipt() )
  990. $_header .= 'Disposition-Notification-To: '.$this->getFrom('addr') . "\r\n";
  991. if ( $this->getErrorsTo() )
  992. $_header .= 'Errors-To: '.$this->getErrorsTo('addr') . "\r\n";
  993. $_header .= 'X-Mailer: Dolibarr version ' . DOL_VERSION .' (using SMTPs Mailer)' . "\r\n"
  994. . 'Mime-Version: 1.0' . "\r\n";
  995. return $_header;
  996. }
  997. /**
  998. * Message Content
  999. *
  1000. * @param string $strContent Message Content
  1001. * @param string $strType Type
  1002. * @return void
  1003. */
  1004. function setBodyContent($strContent, $strType = 'plain')
  1005. {
  1006. //if ( $strContent )
  1007. //{
  1008. if ( $strType == 'html' )
  1009. $strMimeType = 'text/html';
  1010. else
  1011. $strMimeType = 'text/plain';
  1012. // Make RFC821 Compliant, replace bare linefeeds
  1013. $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $strContent);
  1014. $strContent = rtrim(wordwrap($strContent, 75, "\r\n"));
  1015. $this->_msgContent[$strType] = array();
  1016. $this->_msgContent[$strType]['mimeType'] = $strMimeType;
  1017. $this->_msgContent[$strType]['data'] = $strContent;
  1018. if ( $this->getMD5flag() )
  1019. $this->_msgContent[$strType]['md5'] = dol_hash($strContent);
  1020. //}
  1021. }
  1022. /**
  1023. * Retrieves the Message Content
  1024. *
  1025. * @return string Message Content
  1026. */
  1027. function getBodyContent()
  1028. {
  1029. // Generate a new Boundary string
  1030. $this->_setBoundary();
  1031. // What type[s] of content do we have
  1032. $_types = array_keys($this->_msgContent);
  1033. // How many content types do we have
  1034. $keyCount = count($_types);
  1035. // If we have ZERO, we have a problem
  1036. if( $keyCount === 0 )
  1037. die ("Sorry, no content");
  1038. // If we have ONE, we can use the simple format
  1039. else if( $keyCount === 1 )
  1040. {
  1041. $_msgData = $this->_msgContent;
  1042. $_msgData = $_msgData[$_types[0]];
  1043. $content = 'Content-Type: ' . $_msgData['mimeType'] . '; charset="' . $this->getCharSet() . '"' . "\r\n"
  1044. . 'Content-Transfer-Encoding: ' . $this->getTransEncodeType() . "\r\n"
  1045. . 'Content-Disposition: inline' . "\r\n"
  1046. . 'Content-Description: message' . "\r\n";
  1047. if ( $this->getMD5flag() )
  1048. $content .= 'Content-MD5: ' . $_msgData['md5'] . "\r\n";
  1049. $content .= "\r\n"
  1050. . $_msgData['data'] . "\r\n";
  1051. }
  1052. // If we have more than ONE, we use the multi-part format
  1053. else if( $keyCount > 1 )
  1054. {
  1055. // Since this is an actual multi-part message
  1056. // We need to define a content message Boundary
  1057. // NOTE: This was 'multipart/alternative', but Windows based
  1058. // mail servers have issues with this.
  1059. /*
  1060. * TODO Investigate "nested" boundary message parts
  1061. */
  1062. //$content = 'Content-Type: multipart/related; boundary="' . $this->_getBoundary() . '"' . "\r\n";
  1063. $content = 'Content-Type: multipart/mixed; boundary="' . $this->_getBoundary('mixed') . '"' . "\r\n";
  1064. // . "\r\n"
  1065. // . 'This is a multi-part message in MIME format.' . "\r\n";
  1066. $content .= "Content-Transfer-Encoding: 8bit\r\n";
  1067. $content .= "\r\n";
  1068. $content .= "--" . $this->_getBoundary('mixed') . "\r\n";
  1069. if (key_exists('image', $this->_msgContent))
  1070. {
  1071. $content.= 'Content-Type: multipart/alternative; boundary="'.$this->_getBoundary('alternative').'"' . "\r\n";
  1072. $content .= "\r\n";
  1073. $content .= "--" . $this->_getBoundary('alternative') . "\r\n";
  1074. }
  1075. // Loop through message content array
  1076. foreach ($this->_msgContent as $type => $_content )
  1077. {
  1078. if ( $type == 'attachment' )
  1079. {
  1080. // loop through all attachments
  1081. foreach ( $_content as $_file => $_data )
  1082. {
  1083. $content .= "--" . $this->_getBoundary('mixed') . "\r\n"
  1084. . 'Content-Disposition: attachment; filename="' . $_data['fileName'] . '"' . "\r\n"
  1085. . 'Content-Type: ' . $_data['mimeType'] . '; name="' . $_data['fileName'] . '"' . "\r\n"
  1086. . 'Content-Transfer-Encoding: base64' . "\r\n"
  1087. . 'Content-Description: File Attachment' . "\r\n";
  1088. if ( $this->getMD5flag() )
  1089. $content .= 'Content-MD5: ' . $_data['md5'] . "\r\n";
  1090. $content .= "\r\n" . $_data['data'] . "\r\n\r\n";
  1091. }
  1092. }
  1093. // DOL_CHANGE LDR
  1094. else if ( $type == 'image' )
  1095. {
  1096. // loop through all images
  1097. foreach ( $_content as $_image => $_data )
  1098. {
  1099. $content .= "--" . $this->_getBoundary('related') . "\r\n"; // always related for an inline image
  1100. $content .= 'Content-Type: ' . $_data['mimeType'] . '; name="' . $_data['imageName'] . '"' . "\r\n"
  1101. . 'Content-Transfer-Encoding: base64' . "\r\n"
  1102. . 'Content-Disposition: inline; filename="' . $_data['imageName'] . '"' . "\r\n"
  1103. . 'Content-ID: <' . $_data['cid'] . '> ' . "\r\n";
  1104. if ( $this->getMD5flag() )
  1105. $content .= 'Content-MD5: ' . $_data['md5'] . "\r\n";
  1106. $content .= "\r\n"
  1107. . $_data['data'] . "\r\n";
  1108. }
  1109. // always end related and end alternative after inline images
  1110. $content.= "--" . $this->_getBoundary('related') . "--" . "\r\n";
  1111. $content.= "\r\n" . "--" . $this->_getBoundary('alternative') . "--" . "\r\n";
  1112. $content.= "\r\n";
  1113. }
  1114. else
  1115. {
  1116. if (key_exists('image', $this->_msgContent))
  1117. {
  1118. $content.= "Content-Type: text/plain; charset=" . $this->getCharSet() . "\r\n";
  1119. $content.= "\r\n" . strip_tags($_content['data']) . "\r\n"; // Add plain text message
  1120. $content.= "--" . $this->_getBoundary('alternative') . "\r\n";
  1121. $content.= 'Content-Type: multipart/related; boundary="' . $this->_getBoundary('related') . '"' . "\r\n";
  1122. $content.= "\r\n";
  1123. $content.= "--" . $this->_getBoundary('related') . "\r\n";
  1124. }
  1125. $content .= 'Content-Type: ' . $_content['mimeType'] . '; '
  1126. // . 'charset="' . $this->getCharSet() . '"';
  1127. . 'charset=' . $this->getCharSet() . '';
  1128. // $content .= ( $type == 'html') ? '; name="HTML Part"' : '';
  1129. $content .= "\r\n";
  1130. // $content .= 'Content-Transfer-Encoding: ';
  1131. // $content .= ($type == 'html') ? 'quoted-printable' : $this->getTransEncodeType();
  1132. // $content .= "\r\n"
  1133. // . 'Content-Disposition: inline' . "\r\n"
  1134. // . 'Content-Description: ' . $type . ' message' . "\r\n";
  1135. if ( $this->getMD5flag() )
  1136. $content .= 'Content-MD5: ' . $_content['md5'] . "\r\n";
  1137. $content .= "\r\n" . $_content['data'] . "\r\n\r\n";
  1138. }
  1139. }
  1140. // Close message boundries
  1141. // $content .= "\r\n--" . $this->_getBoundary() . '--' . "\r\n" ;
  1142. $content .= "--" . $this->_getBoundary('mixed') . '--' . "\r\n" ;
  1143. }
  1144. return $content;
  1145. }
  1146. /**
  1147. * File attachments are added to the content array as sub-arrays,
  1148. * allowing for multiple attachments for each outbound email
  1149. *
  1150. * @param string $strContent File data to attach to message
  1151. * @param string $strFileName File Name to give to attachment
  1152. * @param string $strMimeType File Mime Type of attachment
  1153. * @return void
  1154. */
  1155. function setAttachment($strContent, $strFileName = 'unknown', $strMimeType = 'unknown')
  1156. {
  1157. if ( $strContent )
  1158. {
  1159. $strContent = rtrim(chunk_split(base64_encode($strContent), 76, "\r\n")); // 76 max is defined into http://tools.ietf.org/html/rfc2047
  1160. $this->_msgContent['attachment'][$strFileName]['mimeType'] = $strMimeType;
  1161. $this->_msgContent['attachment'][$strFileName]['fileName'] = $strFileName;
  1162. $this->_msgContent['attachment'][$strFileName]['data'] = $strContent;
  1163. if ( $this->getMD5flag() )
  1164. $this->_msgContent['attachment'][$strFileName]['md5'] = dol_hash($strContent);
  1165. }
  1166. }
  1167. // DOL_CHANGE LDR
  1168. /**
  1169. * Image attachments are added to the content array as sub-arrays,
  1170. * allowing for multiple images for each outbound email
  1171. *
  1172. * @param string $strContent Image data to attach to message
  1173. * @param string $strImageName Image Name to give to attachment
  1174. * @param string $strMimeType Image Mime Type of attachment
  1175. * @param string $strImageCid CID
  1176. * @return void
  1177. */
  1178. function setImageInline($strContent, $strImageName = 'unknown', $strMimeType = 'unknown', $strImageCid = 'unknown')
  1179. {
  1180. if ($strContent)
  1181. {
  1182. $this->_msgContent['image'][$strImageName]['mimeType'] = $strMimeType;
  1183. $this->_msgContent['image'][$strImageName]['imageName'] = $strImageName;
  1184. $this->_msgContent['image'][$strImageName]['cid'] = $strImageCid;
  1185. $this->_msgContent['image'][$strImageName]['data'] = $strContent;
  1186. if ( $this->getMD5flag() )
  1187. $this->_msgContent['image'][$strImageName]['md5'] = dol_hash($strContent);
  1188. }
  1189. }
  1190. // END DOL_CHANGE LDR
  1191. /**
  1192. * Message Content Sensitivity
  1193. * Message Sensitivity values:
  1194. * - [0] None - default
  1195. * - [1] Personal
  1196. * - [2] Private
  1197. * - [3] Company Confidential
  1198. *
  1199. * @param string $_value Message Sensitivity
  1200. * @return void
  1201. */
  1202. function setSensitivity($_value = 0)
  1203. {
  1204. if ( ( is_numeric($_value) ) &&
  1205. ( ( $_value >= 0 ) && ( $_value <= 3 ) ) )
  1206. $this->_msgSensitivity = $_value;
  1207. }
  1208. /**
  1209. * Returns Message Content Sensitivity string
  1210. * Message Sensitivity values:
  1211. * - [0] None - default
  1212. * - [1] Personal
  1213. * - [2] Private
  1214. * - [3] Company Confidential
  1215. *
  1216. * @return void
  1217. */
  1218. function getSensitivity()
  1219. {
  1220. return $this->_arySensitivity[$this->_msgSensitivity];
  1221. }
  1222. /**
  1223. * Message Content Priority
  1224. * Message Priority values:
  1225. * - [0] 'Bulk'
  1226. * - [1] 'Highest'
  1227. * - [2] 'High'
  1228. * - [3] 'Normal' - default
  1229. * - [4] 'Low'
  1230. * - [5] 'Lowest'
  1231. *
  1232. * @param string $_value Message Priority
  1233. * @return void
  1234. */
  1235. function setPriority ( $_value = 3 )
  1236. {
  1237. if ( ( is_numeric($_value) ) &&
  1238. ( ( $_value >= 0 ) && ( $_value <= 5 ) ) )
  1239. $this->_msgPriority = $_value;
  1240. }
  1241. /**
  1242. * Message Content Priority
  1243. * Message Priority values:
  1244. * - [0] 'Bulk'
  1245. * - [1] 'Highest'
  1246. * - [2] 'High'
  1247. * - [3] 'Normal' - default
  1248. * - [4] 'Low'
  1249. * - [5] 'Lowest'
  1250. *
  1251. * @return void
  1252. */
  1253. function getPriority()
  1254. {
  1255. return 'Importance: ' . $this->_aryPriority[$this->_msgPriority] . "\r\n"
  1256. . 'Priority: ' . $this->_aryPriority[$this->_msgPriority] . "\r\n"
  1257. . 'X-Priority: ' . $this->_msgPriority . ' (' . $this->_aryPriority[$this->_msgPriority] . ')' . "\r\n";
  1258. }
  1259. /**
  1260. * Set flag which determines whether to calculate message MD5 checksum.
  1261. *
  1262. * @param string $_flag Message Priority
  1263. * @return void
  1264. */
  1265. function setMD5flag($_flag = false)
  1266. {
  1267. $this->_smtpMD5 = $_flag;
  1268. }
  1269. /**
  1270. * Gets flag which determines whether to calculate message MD5 checksum.
  1271. *
  1272. * @return string Message Priority
  1273. */
  1274. function getMD5flag()
  1275. {
  1276. return $this->_smtpMD5;
  1277. }
  1278. /**
  1279. * Message X-Header Content
  1280. * This is a simple "insert". Whatever is given will be placed
  1281. * "as is" into the Xheader array.
  1282. *
  1283. * @param string $strXdata Message X-Header Content
  1284. * @return void
  1285. */
  1286. function setXheader($strXdata)
  1287. {
  1288. if ( $strXdata )
  1289. $this->_msgXheader[] = $strXdata;
  1290. }
  1291. /**
  1292. * Retrieves the Message X-Header Content
  1293. *
  1294. * @return string $_msgContent Message X-Header Content
  1295. */
  1296. function getXheader()
  1297. {
  1298. return $this->_msgXheader;
  1299. }
  1300. /**
  1301. * Generates Random string for MIME message Boundary
  1302. *
  1303. * @return void
  1304. */
  1305. function _setBoundary()
  1306. {
  1307. $this->_smtpsBoundary = "multipart_x." . time() . ".x_boundary";
  1308. $this->_smtpsRelatedBoundary = 'mul_'.dol_hash(uniqid("dolibarr2"));
  1309. $this->_smtpsAlternativeBoundary = 'mul_'.dol_hash(uniqid("dolibarr3"));
  1310. }
  1311. /**
  1312. * Retrieves the MIME message Boundary
  1313. *
  1314. * @param string $type Type of boundary
  1315. * @return string $_smtpsBoundary MIME message Boundary
  1316. */
  1317. function _getBoundary($type='mixed')
  1318. {
  1319. if ($type == 'mixed') return $this->_smtpsBoundary;
  1320. else if ($type == 'related') return $this->_smtpsRelatedBoundary;
  1321. else if ($type == 'alternative') return $this->_smtpsAlternativeBoundary;
  1322. }
  1323. /**
  1324. * This function has been modified as provided by SirSir to allow multiline responses when
  1325. * using SMTP Extensions
  1326. *
  1327. * @param Handler $socket Socket handler
  1328. * @param string $response Response
  1329. * @return boolean True or false
  1330. */
  1331. function server_parse($socket, $response)
  1332. {
  1333. /**
  1334. * Returns constructed SELECT Object string or boolean upon failure
  1335. * Default value is set at TRUE
  1336. */
  1337. $_retVal = true;
  1338. $server_response = '';
  1339. while ( substr($server_response,3,1) != ' ' )
  1340. {
  1341. if( !( $server_response = fgets($socket, 256) ) )
  1342. {
  1343. $this->_setErr(121, "Couldn't get mail server response codes");
  1344. $_retVal = false;
  1345. }
  1346. }
  1347. if( !( substr($server_response, 0, 3) == $response ) )
  1348. {
  1349. $this->_setErr(120, "Ran into problems sending Mail.\r\nResponse: $server_response");
  1350. $_retVal = false;
  1351. }
  1352. return $_retVal;
  1353. }
  1354. /**
  1355. * Send str
  1356. *
  1357. * @param string $_strSend String to send
  1358. * @param string $_returnCode Return code
  1359. * @param string $CRLF CRLF
  1360. * @return boolean True or false
  1361. */
  1362. function socket_send_str( $_strSend, $_returnCode = null, $CRLF = "\r\n" )
  1363. {
  1364. if ($this->_debug) $this->log.=$_strSend; // DOL_CHANGE LDR for log
  1365. fputs($this->socket, $_strSend . $CRLF);
  1366. if ($this->_debug) $this->log.=' ('.$_returnCode.')' . $CRLF;
  1367. if ( $_returnCode )
  1368. return $this->server_parse($this->socket, $_returnCode);
  1369. }
  1370. // =============================================================
  1371. // ** Error handling methods
  1372. /**
  1373. * Defines errors codes and messages for Class
  1374. *
  1375. * @param int $_errNum Error Code Number
  1376. * @param string $_errMsg Error Message
  1377. * @return void
  1378. */
  1379. function _setErr ( $_errNum, $_errMsg )
  1380. {
  1381. $this->_smtpsErrors[] = array( 'num' => $_errNum,
  1382. 'msg' => $_errMsg );
  1383. }
  1384. /**
  1385. * Returns errors codes and messages for Class
  1386. *
  1387. * @return string $_errMsg Error Message
  1388. */
  1389. function getErrors()
  1390. {
  1391. $_errMsg = array();
  1392. foreach ( $this->_smtpsErrors as $_err => $_info )
  1393. {
  1394. $_errMsg[] = 'Error [' . $_info['num'] .']: '. $_info['msg'];
  1395. }
  1396. return implode("\n", $_errMsg);
  1397. }
  1398. }
  1399. // =============================================================
  1400. // ** CSV Version Control Info
  1401. /**
  1402. * Revision 2011/09/12 07:49:59 eldy
  1403. * Doxygen
  1404. *
  1405. * Revision 2011/09/06 06:53:53 hregis
  1406. * Fix: use dol_hash instead md5 php function
  1407. *
  1408. * Revision 2011/09/03 00:14:27 eldy
  1409. * Doxygen
  1410. *
  1411. * Revision 2011/08/28 14:24:23 eldy
  1412. * Doxygen
  1413. *
  1414. * Revision 2011/07/12 22:19:02 eldy
  1415. * Fix: Attachment fails if content was empty
  1416. *
  1417. * Revision 2011/06/20 23:17:50 hregis
  1418. * Fix: use best structure of mail
  1419. *
  1420. * Revision 2010/04/13 20:58:37 eldy
  1421. * Fix: Can provide ip address on smtps. Better error reporting.
  1422. *
  1423. * Revision 2010/04/13 20:30:25 eldy
  1424. * Fix: Can provide ip address on smtps. Better error reporting.
  1425. *
  1426. * Revision 2010/01/12 13:02:07 hregis
  1427. * Fix: missing attach-files
  1428. *
  1429. * Revision 2009/11/01 14:16:30 eldy
  1430. * Fix: Sending mail with SMTPS was not working.
  1431. *
  1432. * Revision 2009/10/20 13:14:47 hregis
  1433. * Fix: function "split" is deprecated since php 5.3.0
  1434. *
  1435. * Revision 2009/05/13 19:10:07 eldy
  1436. * New: Can use inline images.Everything seems to work with thunderbird and webmail gmail. New to be tested on other mail browsers.
  1437. *
  1438. * Revision 2009/05/13 14:49:30 eldy
  1439. * Fix: Make code so much simpler and solve a lot of problem with new version.
  1440. *
  1441. * Revision 2009/02/09 00:04:35 eldy
  1442. * Added support for SMTPS protocol
  1443. *
  1444. * Revision 2008/04/16 23:11:45 eldy
  1445. * New: Add action "Test server connectivity"
  1446. *
  1447. * Revision 1.18 2007/01/12 22:17:08 ongardie
  1448. * - Added full_http_site_root() to utils-misc.php
  1449. * - Made SMTPs' getError() easier to use
  1450. * - Improved activity modified emails
  1451. *
  1452. * Revision 1.17 2006/04/05 03:15:40 ongardie
  1453. * -Fixed method name typo that resulted in a fatal error.
  1454. *
  1455. * Revision 1.16 2006/03/08 04:05:25 jswalter
  1456. * - '$_smtpsTransEncode' was removed and '$_smtpsTransEncodeType' is now used
  1457. * - '$_smtpsTransEncodeType' is defaulted to ZERO
  1458. * - corrected 'setCharSet()' internal vars
  1459. * - defined '$_mailPath'
  1460. * - added '$_smtpMD5' as a class property
  1461. * - added 'setMD5flag()' to set above property
  1462. * - added 'getMD5flag()' to retrieve above property
  1463. * - 'setAttachment()' will add an MD5 checksum to attachements if above property is set
  1464. * - 'setBodyContent()' will add an MD5 checksum to message parts if above property is set
  1465. * - 'getBodyContent()' will insert the MD5 checksum for messages and attachments if above property is set
  1466. * - removed leading dashes from message boundry
  1467. * - added propery "Close message boundry" tomessage block
  1468. * - corrected some comments in various places
  1469. * - removed some incorrect comments in others
  1470. *
  1471. * Revision 1.15 2006/02/21 02:00:07 vanmer
  1472. * - patch to add support for sending to exim mail server
  1473. * - thanks to Diego Ongaro at ETSZONE (diego@etszone.com)
  1474. *
  1475. * Revision 1.14 2005/08/29 16:22:10 jswalter
  1476. * - change 'multipart/alternative' to 'multipart/mixed', but Windows based mail servers have issues with this.
  1477. * Bug 594
  1478. *
  1479. * Revision 1.13 2005/08/21 01:57:30 vanmer
  1480. * - added initialization for array if no recipients exist
  1481. *
  1482. * Revision 1.12 2005/08/20 12:04:30 braverock
  1483. * - remove potentially binary characters from Message-ID
  1484. * - add getHost to get the hostname of the mailserver
  1485. * - add username to Message-ID header
  1486. *
  1487. * Revision 1.11 2005/08/20 11:49:48 braverock
  1488. * - fix typos in boundary
  1489. * - remove potentially illegal characters from boundary
  1490. *
  1491. * Revision 1.10 2005/08/19 20:39:32 jswalter
  1492. * - added _server_connect()' as a seperate method to handle server connectivity.
  1493. * - added '_server_authenticate()' as a seperate method to handle server authentication.
  1494. * - 'sendMsg()' now uses the new methods to handle server communication.
  1495. * - modified 'server_parse()' and 'socket_send_str()' to give error codes and messages.
  1496. *
  1497. * Revision 1.9 2005/08/19 15:40:18 jswalter
  1498. * - IMPORTANT: 'setAttachement()' is now spelled correctly: 'setAttachment()'
  1499. * - added additional comment to several methods
  1500. * - added '$_smtpsTransEncodeTypes' array to limit encode types
  1501. * - added parameters to 'sendMsg()' for future development around debugging and logging
  1502. * - added error code within 'setConfig()' if the given path is not found
  1503. * - 'setTransportType()' now has parameter validation
  1504. * [this still is not implemented]
  1505. * - 'setPort()' now does parameter validation
  1506. * - 'setTransEncode()' now has parameter validation against '$_smtpsTransEncodeTypes'
  1507. * - modified 'get_email_list()' to handle error handling
  1508. * - 'setSensitivity()' now has parameter validation
  1509. * - 'setPriority()' now has parameter validation
  1510. *
  1511. * Revision 1.8 2005/06/24 21:00:20 jswalter
  1512. * - corrected comments
  1513. * - corrected the defualt value for 'setPriority()'
  1514. * - modified 'setAttachement()' to process multiple attachments correctly
  1515. * - modified 'getBodyContent()' to handle multiple attachments
  1516. * Bug 310
  1517. *
  1518. * Revision 1.7 2005/05/19 21:12:34 braverock
  1519. * - replace chunk_split() with wordwrap() to fix funky wrapping of templates
  1520. *
  1521. * Revision 1.6 2005/04/25 04:55:06 jswalter
  1522. * - cloned from Master Version
  1523. *
  1524. * Revision 1.10 2005/04/25 04:54:10 walter
  1525. * - "fixed" 'getBodyContent()' to handle a "simple" text only message
  1526. *
  1527. * Revision 1.9 2005/04/25 03:52:01 walter
  1528. * - replace closing curly bracket. Removed it in last revision!
  1529. *
  1530. * Revision 1.8 2005/04/25 02:29:49 walter
  1531. * - added '$_transportType' and its getter/setter methods.
  1532. * for future use. NOT yet implemented.
  1533. * - in 'sendMsg()', added HOST validation check
  1534. * - added error check for initial Socket Connection
  1535. * - created new method 'socket_send_str()' to process socket
  1536. * communication in a unified means. Socket calls within
  1537. * 'sendMsg()' have been modified to use this new method.
  1538. * - expanded comments in 'setConfig()'
  1539. * - added "error" check on PHP ini file properties. If these
  1540. * properties not set within the INI file, the default values
  1541. * will be used.
  1542. * - modified 'get_RCPT_list()' to reset itself each time it is called
  1543. * - modified 'setBodyContent()' to store data in a sub-array for better
  1544. * parsing within the 'getBodyContent()' method
  1545. * - modified 'getBodyContent()' to process contents array better.
  1546. * Also modified to handle attachements.
  1547. * - added 'setAttachement(…

Large files files are truncated, but you can click here to view the full file