/wp-includes/class-phpmailer.php

  1. <?php 
  2. /** 
  3. * PHPMailer - PHP email creation and transport class. 
  4. * PHP Version 5 
  5. * @package PHPMailer 
  6. * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project 
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk> 
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com> 
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net> 
  10. * @author Brent R. Matzelle (original founder) 
  11. * @copyright 2012 - 2014 Marcus Bointon 
  12. * @copyright 2010 - 2012 Jim Jagielski 
  13. * @copyright 2004 - 2009 Andy Prevost 
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License 
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT 
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  17. * FITNESS FOR A PARTICULAR PURPOSE. 
  18. */ 
  19.  
  20. /** 
  21. * PHPMailer - PHP email creation and transport class. 
  22. * @package PHPMailer 
  23. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk> 
  24. * @author Jim Jagielski (jimjag) <jimjag@gmail.com> 
  25. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net> 
  26. * @author Brent R. Matzelle (original founder) 
  27. */ 
  28. class PHPMailer 
  29. /** 
  30. * The PHPMailer Version number. 
  31. * @var string 
  32. */ 
  33. public $Version = '5.2.22'; 
  34.  
  35. /** 
  36. * Email priority. 
  37. * Options: null (default), 1 = High, 3 = Normal, 5 = low. 
  38. * When null, the header is not set at all. 
  39. * @var integer 
  40. */ 
  41. public $Priority = null; 
  42.  
  43. /** 
  44. * The character set of the message. 
  45. * @var string 
  46. */ 
  47. public $CharSet = 'iso-8859-1'; 
  48.  
  49. /** 
  50. * The MIME Content-type of the message. 
  51. * @var string 
  52. */ 
  53. public $ContentType = 'text/plain'; 
  54.  
  55. /** 
  56. * The message encoding. 
  57. * Options: "8bit", "7bit", "binary", "base64", and "quoted-printable". 
  58. * @var string 
  59. */ 
  60. public $Encoding = '8bit'; 
  61.  
  62. /** 
  63. * Holds the most recent mailer error message. 
  64. * @var string 
  65. */ 
  66. public $ErrorInfo = ''; 
  67.  
  68. /** 
  69. * The From email address for the message. 
  70. * @var string 
  71. */ 
  72. public $From = 'root@localhost'; 
  73.  
  74. /** 
  75. * The From name of the message. 
  76. * @var string 
  77. */ 
  78. public $FromName = 'Root User'; 
  79.  
  80. /** 
  81. * The Sender email (Return-Path) of the message. 
  82. * If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. 
  83. * @var string 
  84. */ 
  85. public $Sender = ''; 
  86.  
  87. /** 
  88. * The Return-Path of the message. 
  89. * If empty, it will be set to either From or Sender. 
  90. * @var string 
  91. * @deprecated Email senders should never set a return-path header; 
  92. * it's the receiver's job (RFC5321 section 4.4), so this no longer does anything. 
  93. * @link https://tools.ietf.org/html/rfc5321#section-4.4 RFC5321 reference 
  94. */ 
  95. public $ReturnPath = ''; 
  96.  
  97. /** 
  98. * The Subject of the message. 
  99. * @var string 
  100. */ 
  101. public $Subject = ''; 
  102.  
  103. /** 
  104. * An HTML or plain text message body. 
  105. * If HTML then call isHTML(true). 
  106. * @var string 
  107. */ 
  108. public $Body = ''; 
  109.  
  110. /** 
  111. * The plain-text message body. 
  112. * This body can be read by mail clients that do not have HTML email 
  113. * capability such as mutt & Eudora. 
  114. * Clients that can read HTML will view the normal Body. 
  115. * @var string 
  116. */ 
  117. public $AltBody = ''; 
  118.  
  119. /** 
  120. * An iCal message part body. 
  121. * Only supported in simple alt or alt_inline message types 
  122. * To generate iCal events, use the bundled extras/EasyPeasyICS.php class or iCalcreator 
  123. * @link http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/ 
  124. * @link http://kigkonsult.se/iCalcreator/ 
  125. * @var string 
  126. */ 
  127. public $Ical = ''; 
  128.  
  129. /** 
  130. * The complete compiled MIME message body. 
  131. * @access protected 
  132. * @var string 
  133. */ 
  134. protected $MIMEBody = ''; 
  135.  
  136. /** 
  137. * The complete compiled MIME message headers. 
  138. * @var string 
  139. * @access protected 
  140. */ 
  141. protected $MIMEHeader = ''; 
  142.  
  143. /** 
  144. * Extra headers that createHeader() doesn't fold in. 
  145. * @var string 
  146. * @access protected 
  147. */ 
  148. protected $mailHeader = ''; 
  149.  
  150. /** 
  151. * Word-wrap the message body to this number of chars. 
  152. * Set to 0 to not wrap. A useful value here is 78, for RFC2822 section 2.1.1 compliance. 
  153. * @var integer 
  154. */ 
  155. public $WordWrap = 0; 
  156.  
  157. /** 
  158. * Which method to use to send mail. 
  159. * Options: "mail", "sendmail", or "smtp". 
  160. * @var string 
  161. */ 
  162. public $Mailer = 'mail'; 
  163.  
  164. /** 
  165. * The path to the sendmail program. 
  166. * @var string 
  167. */ 
  168. public $Sendmail = '/usr/sbin/sendmail'; 
  169.  
  170. /** 
  171. * Whether mail() uses a fully sendmail-compatible MTA. 
  172. * One which supports sendmail's "-oi -f" options. 
  173. * @var boolean 
  174. */ 
  175. public $UseSendmailOptions = true; 
  176.  
  177. /** 
  178. * Path to PHPMailer plugins. 
  179. * Useful if the SMTP class is not in the PHP include path. 
  180. * @var string 
  181. * @deprecated Should not be needed now there is an autoloader. 
  182. */ 
  183. public $PluginDir = ''; 
  184.  
  185. /** 
  186. * The email address that a reading confirmation should be sent to, also known as read receipt. 
  187. * @var string 
  188. */ 
  189. public $ConfirmReadingTo = ''; 
  190.  
  191. /** 
  192. * The hostname to use in the Message-ID header and as default HELO string. 
  193. * If empty, PHPMailer attempts to find one with, in order,  
  194. * $_SERVER['SERVER_NAME'], gethostname(), php_uname('n'), or the value 
  195. * 'localhost.localdomain'. 
  196. * @var string 
  197. */ 
  198. public $Hostname = ''; 
  199.  
  200. /** 
  201. * An ID to be used in the Message-ID header. 
  202. * If empty, a unique id will be generated. 
  203. * You can set your own, but it must be in the format "<id@domain>",  
  204. * as defined in RFC5322 section 3.6.4 or it will be ignored. 
  205. * @see https://tools.ietf.org/html/rfc5322#section-3.6.4 
  206. * @var string 
  207. */ 
  208. public $MessageID = ''; 
  209.  
  210. /** 
  211. * The message Date to be used in the Date header. 
  212. * If empty, the current date will be added. 
  213. * @var string 
  214. */ 
  215. public $MessageDate = ''; 
  216.  
  217. /** 
  218. * SMTP hosts. 
  219. * Either a single hostname or multiple semicolon-delimited hostnames. 
  220. * You can also specify a different port 
  221. * for each host by using this format: [hostname:port] 
  222. * (e.g. "smtp1.example.com:25;smtp2.example.com"). 
  223. * You can also specify encryption type, for example: 
  224. * (e.g. "tls://smtp1.example.com:587;ssl://smtp2.example.com:465"). 
  225. * Hosts will be tried in order. 
  226. * @var string 
  227. */ 
  228. public $Host = 'localhost'; 
  229.  
  230. /** 
  231. * The default SMTP server port. 
  232. * @var integer 
  233. * @TODO Why is this needed when the SMTP class takes care of it? 
  234. */ 
  235. public $Port = 25; 
  236.  
  237. /** 
  238. * The SMTP HELO of the message. 
  239. * Default is $Hostname. If $Hostname is empty, PHPMailer attempts to find 
  240. * one with the same method described above for $Hostname. 
  241. * @var string 
  242. * @see PHPMailer::$Hostname 
  243. */ 
  244. public $Helo = ''; 
  245.  
  246. /** 
  247. * What kind of encryption to use on the SMTP connection. 
  248. * Options: '', 'ssl' or 'tls' 
  249. * @var string 
  250. */ 
  251. public $SMTPSecure = ''; 
  252.  
  253. /** 
  254. * Whether to enable TLS encryption automatically if a server supports it,  
  255. * even if `SMTPSecure` is not set to 'tls'. 
  256. * Be aware that in PHP >= 5.6 this requires that the server's certificates are valid. 
  257. * @var boolean 
  258. */ 
  259. public $SMTPAutoTLS = true; 
  260.  
  261. /** 
  262. * Whether to use SMTP authentication. 
  263. * Uses the Username and Password properties. 
  264. * @var boolean 
  265. * @see PHPMailer::$Username 
  266. * @see PHPMailer::$Password 
  267. */ 
  268. public $SMTPAuth = false; 
  269.  
  270. /** 
  271. * Options array passed to stream_context_create when connecting via SMTP. 
  272. * @var array 
  273. */ 
  274. public $SMTPOptions = array(); 
  275.  
  276. /** 
  277. * SMTP username. 
  278. * @var string 
  279. */ 
  280. public $Username = ''; 
  281.  
  282. /** 
  283. * SMTP password. 
  284. * @var string 
  285. */ 
  286. public $Password = ''; 
  287.  
  288. /** 
  289. * SMTP auth type. 
  290. * Options are CRAM-MD5, LOGIN, PLAIN, attempted in that order if not specified 
  291. * @var string 
  292. */ 
  293. public $AuthType = ''; 
  294.  
  295. /** 
  296. * SMTP realm. 
  297. * Used for NTLM auth 
  298. * @var string 
  299. */ 
  300. public $Realm = ''; 
  301.  
  302. /** 
  303. * SMTP workstation. 
  304. * Used for NTLM auth 
  305. * @var string 
  306. */ 
  307. public $Workstation = ''; 
  308.  
  309. /** 
  310. * The SMTP server timeout in seconds. 
  311. * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2 
  312. * @var integer 
  313. */ 
  314. public $Timeout = 300; 
  315.  
  316. /** 
  317. * SMTP class debug output mode. 
  318. * Debug output level. 
  319. * Options: 
  320. * * `0` No output 
  321. * * `1` Commands 
  322. * * `2` Data and commands 
  323. * * `3` As 2 plus connection status 
  324. * * `4` Low-level data output 
  325. * @var integer 
  326. * @see SMTP::$do_debug 
  327. */ 
  328. public $SMTPDebug = 0; 
  329.  
  330. /** 
  331. * How to handle debug output. 
  332. * Options: 
  333. * * `echo` Output plain-text as-is, appropriate for CLI 
  334. * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output 
  335. * * `error_log` Output to error log as configured in php.ini 
  336. * 
  337. * Alternatively, you can provide a callable expecting two params: a message string and the debug level: 
  338. * <code> 
  339. * $mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; 
  340. * </code> 
  341. * @var string|callable 
  342. * @see SMTP::$Debugoutput 
  343. */ 
  344. public $Debugoutput = 'echo'; 
  345.  
  346. /** 
  347. * Whether to keep SMTP connection open after each message. 
  348. * If this is set to true then to close the connection 
  349. * requires an explicit call to smtpClose(). 
  350. * @var boolean 
  351. */ 
  352. public $SMTPKeepAlive = false; 
  353.  
  354. /** 
  355. * Whether to split multiple to addresses into multiple messages 
  356. * or send them all in one message. 
  357. * Only supported in `mail` and `sendmail` transports, not in SMTP. 
  358. * @var boolean 
  359. */ 
  360. public $SingleTo = false; 
  361.  
  362. /** 
  363. * Storage for addresses when SingleTo is enabled. 
  364. * @var array 
  365. * @TODO This should really not be public 
  366. */ 
  367. public $SingleToArray = array(); 
  368.  
  369. /** 
  370. * Whether to generate VERP addresses on send. 
  371. * Only applicable when sending via SMTP. 
  372. * @link https://en.wikipedia.org/wiki/Variable_envelope_return_path 
  373. * @link http://www.postfix.org/VERP_README.html Postfix VERP info 
  374. * @var boolean 
  375. */ 
  376. public $do_verp = false; 
  377.  
  378. /** 
  379. * Whether to allow sending messages with an empty body. 
  380. * @var boolean 
  381. */ 
  382. public $AllowEmpty = false; 
  383.  
  384. /** 
  385. * The default line ending. 
  386. * @note The default remains "\n". We force CRLF where we know 
  387. * it must be used via self::CRLF. 
  388. * @var string 
  389. */ 
  390. public $LE = "\n"; 
  391.  
  392. /** 
  393. * DKIM selector. 
  394. * @var string 
  395. */ 
  396. public $DKIM_selector = ''; 
  397.  
  398. /** 
  399. * DKIM Identity. 
  400. * Usually the email address used as the source of the email. 
  401. * @var string 
  402. */ 
  403. public $DKIM_identity = ''; 
  404.  
  405. /** 
  406. * DKIM passphrase. 
  407. * Used if your key is encrypted. 
  408. * @var string 
  409. */ 
  410. public $DKIM_passphrase = ''; 
  411.  
  412. /** 
  413. * DKIM signing domain name. 
  414. * @example 'example.com' 
  415. * @var string 
  416. */ 
  417. public $DKIM_domain = ''; 
  418.  
  419. /** 
  420. * DKIM private key file path. 
  421. * @var string 
  422. */ 
  423. public $DKIM_private = ''; 
  424.  
  425. /** 
  426. * DKIM private key string. 
  427. * If set, takes precedence over `$DKIM_private`. 
  428. * @var string 
  429. */ 
  430. public $DKIM_private_string = ''; 
  431.  
  432. /** 
  433. * Callback Action function name. 
  434. * 
  435. * The function that handles the result of the send email action. 
  436. * It is called out by send() for each email sent. 
  437. * 
  438. * Value can be any php callable: http://www.php.net/is_callable 
  439. * 
  440. * Parameters: 
  441. * boolean $result result of the send action 
  442. * string $to email address of the recipient 
  443. * string $cc cc email addresses 
  444. * string $bcc bcc email addresses 
  445. * string $subject the subject 
  446. * string $body the email body 
  447. * string $from email address of sender 
  448. * @var string 
  449. */ 
  450. public $action_function = ''; 
  451.  
  452. /** 
  453. * What to put in the X-Mailer header. 
  454. * Options: An empty string for PHPMailer default, whitespace for none, or a string to use 
  455. * @var string 
  456. */ 
  457. public $XMailer = ''; 
  458.  
  459. /** 
  460. * Which validator to use by default when validating email addresses. 
  461. * May be a callable to inject your own validator, but there are several built-in validators. 
  462. * @see PHPMailer::validateAddress() 
  463. * @var string|callable 
  464. * @static 
  465. */ 
  466. public static $validator = 'auto'; 
  467.  
  468. /** 
  469. * An instance of the SMTP sender class. 
  470. * @var SMTP 
  471. * @access protected 
  472. */ 
  473. protected $smtp = null; 
  474.  
  475. /** 
  476. * The array of 'to' names and addresses. 
  477. * @var array 
  478. * @access protected 
  479. */ 
  480. protected $to = array(); 
  481.  
  482. /** 
  483. * The array of 'cc' names and addresses. 
  484. * @var array 
  485. * @access protected 
  486. */ 
  487. protected $cc = array(); 
  488.  
  489. /** 
  490. * The array of 'bcc' names and addresses. 
  491. * @var array 
  492. * @access protected 
  493. */ 
  494. protected $bcc = array(); 
  495.  
  496. /** 
  497. * The array of reply-to names and addresses. 
  498. * @var array 
  499. * @access protected 
  500. */ 
  501. protected $ReplyTo = array(); 
  502.  
  503. /** 
  504. * An array of all kinds of addresses. 
  505. * Includes all of $to, $cc, $bcc 
  506. * @var array 
  507. * @access protected 
  508. * @see PHPMailer::$to @see PHPMailer::$cc @see PHPMailer::$bcc 
  509. */ 
  510. protected $all_recipients = array(); 
  511.  
  512. /** 
  513. * An array of names and addresses queued for validation. 
  514. * In send(), valid and non duplicate entries are moved to $all_recipients 
  515. * and one of $to, $cc, or $bcc. 
  516. * This array is used only for addresses with IDN. 
  517. * @var array 
  518. * @access protected 
  519. * @see PHPMailer::$to @see PHPMailer::$cc @see PHPMailer::$bcc 
  520. * @see PHPMailer::$all_recipients 
  521. */ 
  522. protected $RecipientsQueue = array(); 
  523.  
  524. /** 
  525. * An array of reply-to names and addresses queued for validation. 
  526. * In send(), valid and non duplicate entries are moved to $ReplyTo. 
  527. * This array is used only for addresses with IDN. 
  528. * @var array 
  529. * @access protected 
  530. * @see PHPMailer::$ReplyTo 
  531. */ 
  532. protected $ReplyToQueue = array(); 
  533.  
  534. /** 
  535. * The array of attachments. 
  536. * @var array 
  537. * @access protected 
  538. */ 
  539. protected $attachment = array(); 
  540.  
  541. /** 
  542. * The array of custom headers. 
  543. * @var array 
  544. * @access protected 
  545. */ 
  546. protected $CustomHeader = array(); 
  547.  
  548. /** 
  549. * The most recent Message-ID (including angular brackets). 
  550. * @var string 
  551. * @access protected 
  552. */ 
  553. protected $lastMessageID = ''; 
  554.  
  555. /** 
  556. * The message's MIME type. 
  557. * @var string 
  558. * @access protected 
  559. */ 
  560. protected $message_type = ''; 
  561.  
  562. /** 
  563. * The array of MIME boundary strings. 
  564. * @var array 
  565. * @access protected 
  566. */ 
  567. protected $boundary = array(); 
  568.  
  569. /** 
  570. * The array of available languages. 
  571. * @var array 
  572. * @access protected 
  573. */ 
  574. protected $language = array(); 
  575.  
  576. /** 
  577. * The number of errors encountered. 
  578. * @var integer 
  579. * @access protected 
  580. */ 
  581. protected $error_count = 0; 
  582.  
  583. /** 
  584. * The S/MIME certificate file path. 
  585. * @var string 
  586. * @access protected 
  587. */ 
  588. protected $sign_cert_file = ''; 
  589.  
  590. /** 
  591. * The S/MIME key file path. 
  592. * @var string 
  593. * @access protected 
  594. */ 
  595. protected $sign_key_file = ''; 
  596.  
  597. /** 
  598. * The optional S/MIME extra certificates ("CA Chain") file path. 
  599. * @var string 
  600. * @access protected 
  601. */ 
  602. protected $sign_extracerts_file = ''; 
  603.  
  604. /** 
  605. * The S/MIME password for the key. 
  606. * Used only if the key is encrypted. 
  607. * @var string 
  608. * @access protected 
  609. */ 
  610. protected $sign_key_pass = ''; 
  611.  
  612. /** 
  613. * Whether to throw exceptions for errors. 
  614. * @var boolean 
  615. * @access protected 
  616. */ 
  617. protected $exceptions = false; 
  618.  
  619. /** 
  620. * Unique ID used for message ID and boundaries. 
  621. * @var string 
  622. * @access protected 
  623. */ 
  624. protected $uniqueid = ''; 
  625.  
  626. /** 
  627. * Error severity: message only, continue processing. 
  628. */ 
  629. const STOP_MESSAGE = 0; 
  630.  
  631. /** 
  632. * Error severity: message, likely ok to continue processing. 
  633. */ 
  634. const STOP_CONTINUE = 1; 
  635.  
  636. /** 
  637. * Error severity: message, plus full stop, critical error reached. 
  638. */ 
  639. const STOP_CRITICAL = 2; 
  640.  
  641. /** 
  642. * SMTP RFC standard line ending. 
  643. */ 
  644. const CRLF = "\r\n"; 
  645.  
  646. /** 
  647. * The maximum line length allowed by RFC 2822 section 2.1.1 
  648. * @var integer 
  649. */ 
  650. const MAX_LINE_LENGTH = 998; 
  651.  
  652. /** 
  653. * Constructor. 
  654. * @param boolean $exceptions Should we throw external exceptions? 
  655. */ 
  656. public function __construct($exceptions = null) 
  657. if ($exceptions !== null) { 
  658. $this->exceptions = (boolean)$exceptions; 
  659.  
  660. /** 
  661. * Destructor. 
  662. */ 
  663. public function __destruct() 
  664. //Close any open SMTP connection nicely 
  665. $this->smtpClose(); 
  666.  
  667. /** 
  668. * Call mail() in a safe_mode-aware fashion. 
  669. * Also, unless sendmail_path points to sendmail (or something that 
  670. * claims to be sendmail), don't pass params (not a perfect fix,  
  671. * but it will do) 
  672. * @param string $to To 
  673. * @param string $subject Subject 
  674. * @param string $body Message Body 
  675. * @param string $header Additional Header(s) 
  676. * @param string $params Params 
  677. * @access private 
  678. * @return boolean 
  679. */ 
  680. private function mailPassthru($to, $subject, $body, $header, $params) 
  681. //Check overloading of mail function to avoid double-encoding 
  682. if (ini_get('mbstring.func_overload') & 1) { 
  683. $subject = $this->secureHeader($subject); 
  684. } else { 
  685. $subject = $this->encodeHeader($this->secureHeader($subject)); 
  686.  
  687. //Can't use additional_parameters in safe_mode, calling mail() with null params breaks 
  688. //@link http://php.net/manual/en/function.mail.php 
  689. if (ini_get('safe_mode') or !$this->UseSendmailOptions or is_null($params)) { 
  690. $result = @mail($to, $subject, $body, $header); 
  691. } else { 
  692. $result = @mail($to, $subject, $body, $header, $params); 
  693. return $result; 
  694. /** 
  695. * Output debugging info via user-defined method. 
  696. * Only generates output if SMTP debug output is enabled (@see SMTP::$do_debug). 
  697. * @see PHPMailer::$Debugoutput 
  698. * @see PHPMailer::$SMTPDebug 
  699. * @param string $str 
  700. */ 
  701. protected function edebug($str) 
  702. if ($this->SMTPDebug <= 0) { 
  703. return; 
  704. //Avoid clash with built-in function names 
  705. if (!in_array($this->Debugoutput, array('error_log', 'html', 'echo')) and is_callable($this->Debugoutput)) { 
  706. call_user_func($this->Debugoutput, $str, $this->SMTPDebug); 
  707. return; 
  708. switch ($this->Debugoutput) { 
  709. case 'error_log': 
  710. //Don't output, just log 
  711. error_log($str); 
  712. break; 
  713. case 'html': 
  714. //Cleans up output a bit for a better looking, HTML-safe output 
  715. echo htmlentities( 
  716. preg_replace('/[\r\n]+/', '', $str),  
  717. ENT_QUOTES,  
  718. 'UTF-8' 
  719. . "<br>\n"; 
  720. break; 
  721. case 'echo': 
  722. default: 
  723. //Normalize line breaks 
  724. $str = preg_replace('/\r\n?/ms', "\n", $str); 
  725. echo gmdate('Y-m-d H:i:s') . "\t" . str_replace( 
  726. "\n",  
  727. "\n \t ",  
  728. trim($str) 
  729. ) . "\n"; 
  730.  
  731. /** 
  732. * Sets message type to HTML or plain. 
  733. * @param boolean $isHtml True for HTML mode. 
  734. * @return void 
  735. */ 
  736. public function isHTML($isHtml = true) 
  737. if ($isHtml) { 
  738. $this->ContentType = 'text/html'; 
  739. } else { 
  740. $this->ContentType = 'text/plain'; 
  741.  
  742. /** 
  743. * Send messages using SMTP. 
  744. * @return void 
  745. */ 
  746. public function isSMTP() 
  747. $this->Mailer = 'smtp'; 
  748.  
  749. /** 
  750. * Send messages using PHP's mail() function. 
  751. * @return void 
  752. */ 
  753. public function isMail() 
  754. $this->Mailer = 'mail'; 
  755.  
  756. /** 
  757. * Send messages using $Sendmail. 
  758. * @return void 
  759. */ 
  760. public function isSendmail() 
  761. $ini_sendmail_path = ini_get('sendmail_path'); 
  762.  
  763. if (!stristr($ini_sendmail_path, 'sendmail')) { 
  764. $this->Sendmail = '/usr/sbin/sendmail'; 
  765. } else { 
  766. $this->Sendmail = $ini_sendmail_path; 
  767. $this->Mailer = 'sendmail'; 
  768.  
  769. /** 
  770. * Send messages using qmail. 
  771. * @return void 
  772. */ 
  773. public function isQmail() 
  774. $ini_sendmail_path = ini_get('sendmail_path'); 
  775.  
  776. if (!stristr($ini_sendmail_path, 'qmail')) { 
  777. $this->Sendmail = '/var/qmail/bin/qmail-inject'; 
  778. } else { 
  779. $this->Sendmail = $ini_sendmail_path; 
  780. $this->Mailer = 'qmail'; 
  781.  
  782. /** 
  783. * Add a "To" address. 
  784. * @param string $address The email address to send to 
  785. * @param string $name 
  786. * @return boolean true on success, false if address already used or invalid in some way 
  787. */ 
  788. public function addAddress($address, $name = '') 
  789. return $this->addOrEnqueueAnAddress('to', $address, $name); 
  790.  
  791. /** 
  792. * Add a "CC" address. 
  793. * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer. 
  794. * @param string $address The email address to send to 
  795. * @param string $name 
  796. * @return boolean true on success, false if address already used or invalid in some way 
  797. */ 
  798. public function addCC($address, $name = '') 
  799. return $this->addOrEnqueueAnAddress('cc', $address, $name); 
  800.  
  801. /** 
  802. * Add a "BCC" address. 
  803. * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer. 
  804. * @param string $address The email address to send to 
  805. * @param string $name 
  806. * @return boolean true on success, false if address already used or invalid in some way 
  807. */ 
  808. public function addBCC($address, $name = '') 
  809. return $this->addOrEnqueueAnAddress('bcc', $address, $name); 
  810.  
  811. /** 
  812. * Add a "Reply-To" address. 
  813. * @param string $address The email address to reply to 
  814. * @param string $name 
  815. * @return boolean true on success, false if address already used or invalid in some way 
  816. */ 
  817. public function addReplyTo($address, $name = '') 
  818. return $this->addOrEnqueueAnAddress('Reply-To', $address, $name); 
  819.  
  820. /** 
  821. * Add an address to one of the recipient arrays or to the ReplyTo array. Because PHPMailer 
  822. * can't validate addresses with an IDN without knowing the PHPMailer::$CharSet (that can still 
  823. * be modified after calling this function), addition of such addresses is delayed until send(). 
  824. * Addresses that have been added already return false, but do not throw exceptions. 
  825. * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' 
  826. * @param string $address The email address to send, resp. to reply to 
  827. * @param string $name 
  828. * @throws phpmailerException 
  829. * @return boolean true on success, false if address already used or invalid in some way 
  830. * @access protected 
  831. */ 
  832. protected function addOrEnqueueAnAddress($kind, $address, $name) 
  833. $address = trim($address); 
  834. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim 
  835. if (($pos = strrpos($address, '@')) === false) { 
  836. // At-sign is misssing. 
  837. $error_message = $this->lang('invalid_address') . " (addAnAddress $kind): $address"; 
  838. $this->setError($error_message); 
  839. $this->edebug($error_message); 
  840. if ($this->exceptions) { 
  841. throw new phpmailerException($error_message); 
  842. return false; 
  843. $params = array($kind, $address, $name); 
  844. // Enqueue addresses with IDN until we know the PHPMailer::$CharSet. 
  845. if ($this->has8bitChars(substr($address, ++$pos)) and $this->idnSupported()) { 
  846. if ($kind != 'Reply-To') { 
  847. if (!array_key_exists($address, $this->RecipientsQueue)) { 
  848. $this->RecipientsQueue[$address] = $params; 
  849. return true; 
  850. } else { 
  851. if (!array_key_exists($address, $this->ReplyToQueue)) { 
  852. $this->ReplyToQueue[$address] = $params; 
  853. return true; 
  854. return false; 
  855. // Immediately add standard addresses without IDN. 
  856. return call_user_func_array(array($this, 'addAnAddress'), $params); 
  857.  
  858. /** 
  859. * Add an address to one of the recipient arrays or to the ReplyTo array. 
  860. * Addresses that have been added already return false, but do not throw exceptions. 
  861. * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' 
  862. * @param string $address The email address to send, resp. to reply to 
  863. * @param string $name 
  864. * @throws phpmailerException 
  865. * @return boolean true on success, false if address already used or invalid in some way 
  866. * @access protected 
  867. */ 
  868. protected function addAnAddress($kind, $address, $name = '') 
  869. if (!in_array($kind, array('to', 'cc', 'bcc', 'Reply-To'))) { 
  870. $error_message = $this->lang('Invalid recipient kind: ') . $kind; 
  871. $this->setError($error_message); 
  872. $this->edebug($error_message); 
  873. if ($this->exceptions) { 
  874. throw new phpmailerException($error_message); 
  875. return false; 
  876. if (!$this->validateAddress($address)) { 
  877. $error_message = $this->lang('invalid_address') . " (addAnAddress $kind): $address"; 
  878. $this->setError($error_message); 
  879. $this->edebug($error_message); 
  880. if ($this->exceptions) { 
  881. throw new phpmailerException($error_message); 
  882. return false; 
  883. if ($kind != 'Reply-To') { 
  884. if (!array_key_exists(strtolower($address), $this->all_recipients)) { 
  885. array_push($this->$kind, array($address, $name)); 
  886. $this->all_recipients[strtolower($address)] = true; 
  887. return true; 
  888. } else { 
  889. if (!array_key_exists(strtolower($address), $this->ReplyTo)) { 
  890. $this->ReplyTo[strtolower($address)] = array($address, $name); 
  891. return true; 
  892. return false; 
  893.  
  894. /** 
  895. * Parse and validate a string containing one or more RFC822-style comma-separated email addresses 
  896. * of the form "display name <address>" into an array of name/address pairs. 
  897. * Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available. 
  898. * Note that quotes in the name part are removed. 
  899. * @param string $addrstr The address list string 
  900. * @param bool $useimap Whether to use the IMAP extension to parse the list 
  901. * @return array 
  902. * @link http://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation 
  903. */ 
  904. public function parseAddresses($addrstr, $useimap = true) 
  905. $addresses = array(); 
  906. if ($useimap and function_exists('imap_rfc822_parse_adrlist')) { 
  907. //Use this built-in parser if it's available 
  908. $list = imap_rfc822_parse_adrlist($addrstr, ''); 
  909. foreach ($list as $address) { 
  910. if ($address->host != '.SYNTAX-ERROR.') { 
  911. if ($this->validateAddress($address->mailbox . '@' . $address->host)) { 
  912. $addresses[] = array( 
  913. 'name' => (property_exists($address, 'personal') ? $address->personal : ''),  
  914. 'address' => $address->mailbox . '@' . $address->host 
  915. ); 
  916. } else { 
  917. //Use this simpler parser 
  918. $list = explode(', ', $addrstr); 
  919. foreach ($list as $address) { 
  920. $address = trim($address); 
  921. //Is there a separate name part? 
  922. if (strpos($address, '<') === false) { 
  923. //No separate name, just use the whole thing 
  924. if ($this->validateAddress($address)) { 
  925. $addresses[] = array( 
  926. 'name' => '',  
  927. 'address' => $address 
  928. ); 
  929. } else { 
  930. list($name, $email) = explode('<', $address); 
  931. $email = trim(str_replace('>', '', $email)); 
  932. if ($this->validateAddress($email)) { 
  933. $addresses[] = array( 
  934. 'name' => trim(str_replace(array('"', "'"), '', $name)),  
  935. 'address' => $email 
  936. ); 
  937. return $addresses; 
  938.  
  939. /** 
  940. * Set the From and FromName properties. 
  941. * @param string $address 
  942. * @param string $name 
  943. * @param boolean $auto Whether to also set the Sender address, defaults to true 
  944. * @throws phpmailerException 
  945. * @return boolean 
  946. */ 
  947. public function setFrom($address, $name = '', $auto = true) 
  948. $address = trim($address); 
  949. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim 
  950. // Don't validate now addresses with IDN. Will be done in send(). 
  951. if (($pos = strrpos($address, '@')) === false or 
  952. (!$this->has8bitChars(substr($address, ++$pos)) or !$this->idnSupported()) and 
  953. !$this->validateAddress($address)) { 
  954. $error_message = $this->lang('invalid_address') . " (setFrom) $address"; 
  955. $this->setError($error_message); 
  956. $this->edebug($error_message); 
  957. if ($this->exceptions) { 
  958. throw new phpmailerException($error_message); 
  959. return false; 
  960. $this->From = $address; 
  961. $this->FromName = $name; 
  962. if ($auto) { 
  963. if (empty($this->Sender)) { 
  964. $this->Sender = $address; 
  965. return true; 
  966.  
  967. /** 
  968. * Return the Message-ID header of the last email. 
  969. * Technically this is the value from the last time the headers were created,  
  970. * but it's also the message ID of the last sent message except in 
  971. * pathological cases. 
  972. * @return string 
  973. */ 
  974. public function getLastMessageID() 
  975. return $this->lastMessageID; 
  976.  
  977. /** 
  978. * Check that a string looks like an email address. 
  979. * @param string $address The email address to check 
  980. * @param string|callable $patternselect A selector for the validation pattern to use : 
  981. * * `auto` Pick best pattern automatically; 
  982. * * `pcre8` Use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14; 
  983. * * `pcre` Use old PCRE implementation; 
  984. * * `php` Use PHP built-in FILTER_VALIDATE_EMAIL; 
  985. * * `html5` Use the pattern given by the HTML5 spec for 'email' type form input elements. 
  986. * * `noregex` Don't use a regex: super fast, really dumb. 
  987. * Alternatively you may pass in a callable to inject your own validator, for example: 
  988. * PHPMailer::validateAddress('user@example.com', function($address) { 
  989. * return (strpos($address, '@') !== false); 
  990. * }); 
  991. * You can also set the PHPMailer::$validator static to a callable, allowing built-in methods to use your validator. 
  992. * @return boolean 
  993. * @static 
  994. * @access public 
  995. */ 
  996. public static function validateAddress($address, $patternselect = null) 
  997. if (is_null($patternselect)) { 
  998. $patternselect = self::$validator; 
  999. if (is_callable($patternselect)) { 
  1000. return call_user_func($patternselect, $address); 
  1001. //Reject line breaks in addresses; it's valid RFC5322, but not RFC5321 
  1002. if (strpos($address, "\n") !== false or strpos($address, "\r") !== false) { 
  1003. return false; 
  1004. if (!$patternselect or $patternselect == 'auto') { 
  1005. //Check this constant first so it works when extension_loaded() is disabled by safe mode 
  1006. //Constant was added in PHP 5.2.4 
  1007. if (defined('PCRE_VERSION')) { 
  1008. //This pattern can get stuck in a recursive loop in PCRE <= 8.0.2 
  1009. if (version_compare(PCRE_VERSION, '8.0.3') >= 0) { 
  1010. $patternselect = 'pcre8'; 
  1011. } else { 
  1012. $patternselect = 'pcre'; 
  1013. } elseif (function_exists('extension_loaded') and extension_loaded('pcre')) { 
  1014. //Fall back to older PCRE 
  1015. $patternselect = 'pcre'; 
  1016. } else { 
  1017. //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension 
  1018. if (version_compare(PHP_VERSION, '5.2.0') >= 0) { 
  1019. $patternselect = 'php'; 
  1020. } else { 
  1021. $patternselect = 'noregex'; 
  1022. switch ($patternselect) { 
  1023. case 'pcre8': 
  1024. /** 
  1025. * Uses the same RFC5322 regex on which FILTER_VALIDATE_EMAIL is based, but allows dotless domains. 
  1026. * @link http://squiloople.com/2009/12/20/email-address-validation/ 
  1027. * @copyright 2009-2010 Michael Rushton 
  1028. * Feel free to use and redistribute this code. But please keep this copyright notice. 
  1029. */ 
  1030. return (boolean)preg_match( 
  1031. '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)) {255, })(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)) {65, }@)' . 
  1032. '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' . 
  1033. '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' . 
  1034. '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' . 
  1035. '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64, })(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' . 
  1036. '(?>(?1)\.(?!(?1)[a-z0-9-]{64, })(?1)(?5)) {0, 126}|\[(?:(?>IPv6:(?>([a-f0-9]{1, 4})(?>:(?6)) {7}' . 
  1037. '|(?!(?:.*[a-f0-9][:\]]) {8, })((?6)(?>:(?6)) {0, 6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)) {5}:' . 
  1038. '|(?!(?:.*[a-f0-9]:) {6, })(?8)?::(?>((?6)(?>:(?6)) {0, 4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' . 
  1039. '|[1-9]?[0-9])(?>\.(?9)) {3}))\])(?1)$/isD',  
  1040. $address 
  1041. ); 
  1042. case 'pcre': 
  1043. //An older regex that doesn't need a recent PCRE 
  1044. return (boolean)preg_match( 
  1045. '/^(?!(?>"?(?>\\\[ -~]|[^"])"?) {255, })(?!(?>"?(?>\\\[ -~]|[^"])"?) {65, }@)(?>' . 
  1046. '[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' . 
  1047. '(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' . 
  1048. '@(?>(?![a-z0-9-]{64, })(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64, })' . 
  1049. '(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)) {0, 126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1, 4})(?>:' . 
  1050. '[a-f0-9]{1, 4}) {7}|(?!(?:.*[a-f0-9][:\]]) {8, })(?>[a-f0-9]{1, 4}(?>:[a-f0-9]{1, 4}) {0, 6})?' . 
  1051. '::(?>[a-f0-9]{1, 4}(?>:[a-f0-9]{1, 4}) {0, 6})?))|(?>(?>IPv6:(?>[a-f0-9]{1, 4}(?>:' . 
  1052. '[a-f0-9]{1, 4}) {5}:|(?!(?:.*[a-f0-9]:) {6, })(?>[a-f0-9]{1, 4}(?>:[a-f0-9]{1, 4}) {0, 4})?' . 
  1053. '::(?>(?:[a-f0-9]{1, 4}(?>:[a-f0-9]{1, 4}) {0, 4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' . 
  1054. '|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])) {3}))\])$/isD',  
  1055. $address 
  1056. ); 
  1057. case 'html5': 
  1058. /** 
  1059. * This is the pattern used in the HTML5 spec for validation of 'email' type form input elements. 
  1060. * @link http://www.whatwg.org/specs/web-apps/current-work/#e-mail-state-(type=email) 
  1061. */ 
  1062. return (boolean)preg_match( 
  1063. '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0, 61}' . 
  1064. '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0, 61}[a-zA-Z0-9])?)*$/sD',  
  1065. $address 
  1066. ); 
  1067. case 'noregex': 
  1068. //No PCRE! Do something _very_ approximate! 
  1069. //Check the address is 3 chars or longer and contains an @ that's not the first or last char 
  1070. return (strlen($address) >= 3 
  1071. and strpos($address, '@') >= 1 
  1072. and strpos($address, '@') != strlen($address) - 1); 
  1073. case 'php': 
  1074. default: 
  1075. return (boolean)filter_var($address, FILTER_VALIDATE_EMAIL); 
  1076.  
  1077. /** 
  1078. * Tells whether IDNs (Internationalized Domain Names) are supported or not. This requires the 
  1079. * "intl" and "mbstring" PHP extensions. 
  1080. * @return bool "true" if required functions for IDN support are present 
  1081. */ 
  1082. public function idnSupported() 
  1083. // @TODO: Write our own "idn_to_ascii" function for PHP <= 5.2. 
  1084. return function_exists('idn_to_ascii') and function_exists('mb_convert_encoding'); 
  1085.  
  1086. /** 
  1087. * Converts IDN in given email address to its ASCII form, also known as punycode, if possible. 
  1088. * Important: Address must be passed in same encoding as currently set in PHPMailer::$CharSet. 
  1089. * This function silently returns unmodified address if: 
  1090. * - No conversion is necessary (i.e. domain name is not an IDN, or is already in ASCII form) 
  1091. * - Conversion to punycode is impossible (e.g. required PHP functions are not available) 
  1092. * or fails for any reason (e.g. domain has characters not allowed in an IDN) 
  1093. * @see PHPMailer::$CharSet 
  1094. * @param string $address The email address to convert 
  1095. * @return string The encoded address in ASCII form 
  1096. */ 
  1097. public function punyencodeAddress($address) 
  1098. // Verify we have required functions, CharSet, and at-sign. 
  1099. if ($this->idnSupported() and 
  1100. !empty($this->CharSet) and 
  1101. ($pos = strrpos($address, '@')) !== false) { 
  1102. $domain = substr($address, ++$pos); 
  1103. // Verify CharSet string is a valid one, and domain properly encoded in this CharSet. 
  1104. if ($this->has8bitChars($domain) and @mb_check_encoding($domain, $this->CharSet)) { 
  1105. $domain = mb_convert_encoding($domain, 'UTF-8', $this->CharSet); 
  1106. if (($punycode = defined('INTL_IDNA_VARIANT_UTS46') ? 
  1107. idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46) : 
  1108. idn_to_ascii($domain)) !== false) { 
  1109. return substr($address, 0, $pos) . $punycode; 
  1110. return $address; 
  1111.  
  1112. /** 
  1113. * Create a message and send it. 
  1114. * Uses the sending method specified by $Mailer. 
  1115. * @throws phpmailerException 
  1116. * @return boolean false on error - See the ErrorInfo property for details of the error. 
  1117. */ 
  1118. public function send() 
  1119. try { 
  1120. if (!$this->preSend()) { 
  1121. return false; 
  1122. return $this->postSend(); 
  1123. } catch (phpmailerException $exc) { 
  1124. $this->mailHeader = ''; 
  1125. $this->setError($exc->getMessage()); 
  1126. if ($this->exceptions) { 
  1127. throw $exc; 
  1128. return false; 
  1129.  
  1130. /** 
  1131. * Prepare a message for sending. 
  1132. * @throws phpmailerException 
  1133. * @return boolean 
  1134. */ 
  1135. public function preSend() 
  1136. try { 
  1137. $this->error_count = 0; // Reset errors 
  1138. $this->mailHeader = ''; 
  1139.  
  1140. // Dequeue recipient and Reply-To addresses with IDN 
  1141. foreach (array_merge($this->RecipientsQueue, $this->ReplyToQueue) as $params) { 
  1142. $params[1] = $this->punyencodeAddress($params[1]); 
  1143. call_user_func_array(array($this, 'addAnAddress'), $params); 
  1144. if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) { 
  1145. throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL); 
  1146.  
  1147. // Validate From, Sender, and ConfirmReadingTo addresses 
  1148. foreach (array('From', 'Sender', 'ConfirmReadingTo') as $address_kind) { 
  1149. $this->$address_kind = trim($this->$address_kind); 
  1150. if (empty($this->$address_kind)) { 
  1151. continue; 
  1152. $this->$address_kind = $this->punyencodeAddress($this->$address_kind); 
  1153. if (!$this->validateAddress($this->$address_kind)) { 
  1154. $error_message = $this->lang('invalid_address') . ' (punyEncode) ' . $this->$address_kind; 
  1155. $this->setError($error_message); 
  1156. $this->edebug($error_message); 
  1157. if ($this->exceptions) { 
  1158. throw new phpmailerException($error_message); 
  1159. return false; 
  1160.  
  1161. // Set whether the message is multipart/alternative 
  1162. if ($this->alternativeExists()) { 
  1163. $this->ContentType = 'multipart/alternative'; 
  1164.  
  1165. $this->setMessageType(); 
  1166. // Refuse to send an empty message unless we are specifically allowing it 
  1167. if (!$this->AllowEmpty and empty($this->Body)) { 
  1168. throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL); 
  1169.  
  1170. // Create body before headers in case body makes changes to headers (e.g. altering transfer encoding) 
  1171. $this->MIMEHeader = ''; 
  1172. $this->MIMEBody = $this->createBody(); 
  1173. // createBody may have added some headers, so retain them 
  1174. $tempheaders = $this->MIMEHeader; 
  1175. $this->MIMEHeader = $this->createHeader(); 
  1176. $this->MIMEHeader .= $tempheaders; 
  1177.  
  1178. // To capture the complete message when using mail(), create 
  1179. // an extra header list which createHeader() doesn't fold in 
  1180. if ($this->Mailer == 'mail') { 
  1181. if (count($this->to) > 0) { 
  1182. $this->mailHeader .= $this->addrAppend('To', $this->to); 
  1183. } else { 
  1184. $this->mailHeader .= $this->headerLine('To', 'undisclosed-recipients:;'); 
  1185. $this->mailHeader .= $this->headerLine( 
  1186. 'Subject',  
  1187. $this->encodeHeader($this->secureHeader(trim($this->Subject))) 
  1188. ); 
  1189.  
  1190. // Sign with DKIM if enabled 
  1191. if (!empty($this->DKIM_domain) 
  1192. && !empty($this->DKIM_selector) 
  1193. && (!empty($this->DKIM_private_string) 
  1194. || (!empty($this->DKIM_private) && file_exists($this->DKIM_private)) 
  1195. ) { 
  1196. $header_dkim = $this->DKIM_Add( 
  1197. $this->MIMEHeader . $this->mailHeader,  
  1198. $this->encodeHeader($this->secureHeader($this->Subject)),  
  1199. $this->MIMEBody 
  1200. ); 
  1201. $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF . 
  1202. str_replace("\r\n", "\n", $header_dkim) . self::CRLF; 
  1203. return true; 
  1204. } catch (phpmailerException $exc) { 
  1205. $this->setError($exc->getMessage()); 
  1206. if ($this->exceptions) { 
  1207. throw $exc; 
  1208. return false; 
  1209.  
  1210. /** 
  1211. * Actually send a message. 
  1212. * Send the email via the selected mechanism 
  1213. * @throws phpmailerException 
  1214. * @return boolean 
  1215. */ 
  1216. public function postSend() 
  1217. try { 
  1218. // Choose the mailer and send through it 
  1219. switch ($this->Mailer) { 
  1220. case 'sendmail': 
  1221. case 'qmail': 
  1222. return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody); 
  1223. case 'smtp': 
  1224. return $this->smtpSend($this->MIMEHeader, $this->MIMEBody); 
  1225. case 'mail': 
  1226. return $this->mailSend($this->MIMEHeader, $this->MIMEBody); 
  1227. default: 
  1228. $sendMethod = $this->Mailer.'Send'; 
  1229. if (method_exists($this, $sendMethod)) { 
  1230. return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody); 
  1231.  
  1232. return $this->mailSend($this->MIMEHeader, $this->MIMEBody); 
  1233. } catch (phpmailerException $exc) { 
  1234. $this->setError($exc->getMessage()); 
  1235. $this->edebug($exc->getMessage()); 
  1236. if ($this->exceptions) { 
  1237. throw $exc; 
  1238. return false; 
  1239.  
  1240. /** 
  1241. * Send mail using the $Sendmail program. 
  1242. * @param string $header The message headers 
  1243. * @param string $body The message body 
  1244. * @see PHPMailer::$Sendmail 
  1245. * @throws phpmailerException 
  1246. * @access protected 
  1247. * @return boolean 
  1248. */ 
  1249. protected function sendmailSend($header, $body) 
  1250. // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. 
  1251. if (!empty($this->Sender) and self::isShellSafe($this->Sender)) { 
  1252. if ($this->Mailer == 'qmail') { 
  1253. $sendmailFmt = '%s -f%s'; 
  1254. } else { 
  1255. $sendmailFmt = '%s -oi -f%s -t'; 
  1256. } else { 
  1257. if ($this->Mailer == 'qmail') { 
  1258. $sendmailFmt = '%s'; 
  1259. } else { 
  1260. $sendmailFmt = '%s -oi -t'; 
  1261.  
  1262. // TODO: If possible, this should be changed to escapeshellarg. Needs thorough testing. 
  1263. $sendmail = sprintf($sendmailFmt, escapeshellcmd($this->Sendmail), $this->Sender); 
  1264.  
  1265. if ($this->SingleTo) { 
  1266. foreach ($this->SingleToArray as $toAddr) { 
  1267. if (!@$mail = popen($sendmail, 'w')) { 
  1268. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 
  1269. fputs($mail, 'To: ' . $toAddr . "\n"); 
  1270. fputs($mail, $header); 
  1271. fputs($mail, $body); 
  1272. $result = pclose($mail); 
  1273. $this->doCallback( 
  1274. ($result == 0),  
  1275. array($toAddr),  
  1276. $this->cc,  
  1277. $this->bcc,  
  1278. $this->Subject,  
  1279. $body,  
  1280. $this->From 
  1281. ); 
  1282. if ($result != 0) { 
  1283. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 
  1284. } else { 
  1285. if (!@$mail = popen($sendmail, 'w')) { 
  1286. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 
  1287. fputs($mail, $header); 
  1288. fputs($mail, $body); 
  1289. $result = pclose($mail); 
  1290. $this->doCallback( 
  1291. ($result == 0),  
  1292. $this->to,  
  1293. $this->cc,  
  1294. $this->bcc,  
  1295. $this->Subject,  
  1296. $body,  
  1297. $this->From 
  1298. ); 
  1299. if ($result != 0) { 
  1300. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 
  1301. return true; 
  1302.  
  1303. /** 
  1304. * Fix CVE-2016-10033 and CVE-2016-10045 by disallowing potentially unsafe shell characters. 
  1305. * 
  1306. * Note that escapeshellarg and escapeshellcmd are inadequate for our purposes, especially on Windows. 
  1307. * @param string $string The string to be validated 
  1308. * @see https://github.com/PHPMailer/PHPMailer/issues/924 CVE-2016-10045 bug report 
  1309. * @access protected 
  1310. * @return boolean 
  1311. */ 
  1312. protected static function isShellSafe($string) 
  1313. // Future-proof 
  1314. if (escapeshellcmd($string) !== $string 
  1315. or !in_array(escapeshellarg($string), array("'$string'", "\"$string\"")) 
  1316. ) { 
  1317. return false; 
  1318.  
  1319. $length = strlen($string); 
  1320.  
  1321. for ($i = 0; $i < $length; $i++) { 
  1322. $c = $string[$i]; 
  1323.  
  1324. // All other characters have a special meaning in at least one common shell, including = and +. 
  1325. // Full stop (.) has a special meaning in cmd.exe, but its impact should be negligible here. 
  1326. // Note that this does permit non-Latin alphanumeric characters based on the current locale. 
  1327. if (!ctype_alnum($c) && strpos('@_-.', $c) === false) { 
  1328. return false; 
  1329.  
  1330. return true; 
  1331.  
  1332. /** 
  1333. * Send mail using the PHP mail() function. 
  1334. * @param string $header The message headers 
  1335. * @param string $body The message body 
  1336. * @link http://www.php.net/manual/en/book.mail.php 
  1337. * @throws phpmailerException 
  1338. * @access protected 
  1339. * @return boolean 
  1340. */ 
  1341. protected function mailSend($header, $body) 
  1342. $toArr = array(); 
  1343. foreach ($this->to as $toaddr) { 
  1344. $toArr[] = $this->addrFormat($toaddr); 
  1345. $to = implode(', ', $toArr); 
  1346.  
  1347. $params = null; 
  1348. //This sets the SMTP envelope sender which gets turned into a return-path header by the receiver 
  1349. if (!empty($this->Sender) and $this->validateAddress($this->Sender)) { 
  1350. // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. 
  1351. if (self::isShellSafe($this->Sender)) { 
  1352. $params = sprintf('-f%s', $this->Sender); 
  1353. if (!empty($this->Sender) and !ini_get('safe_mode') and $this->validateAddress($this->Sender)) { 
  1354. $old_from = ini_get('sendmail_from'); 
  1355. ini_set('sendmail_from', $this->Sender); 
  1356. $result = false; 
  1357. if ($this->SingleTo and count($toArr) > 1) { 
  1358. foreach ($toArr as $toAddr) { 
  1359. $result = $this->mailPassthru($toAddr, $this->Subject, $body, $header, $params); 
  1360. $this->doCallback($result, array($toAddr), $this->cc, $this->bcc, $this->Subject, $body, $this->From); 
  1361. } else { 
  1362. $result = $this->mailPassthru($to, $this->Subject, $body, $header, $params); 
  1363. $this->doCallback($result, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From); 
  1364. if (isset($old_from)) { 
  1365. ini_set('sendmail_from', $old_from); 
  1366. if (!$result) { 
  1367. throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL); 
  1368. return true; 
  1369.  
  1370. /** 
  1371. * Get an instance to use for SMTP operations. 
  1372. * Override this function to load your own SMTP implementation 
  1373. * @return SMTP 
  1374. */ 
  1375. public function getSMTPInstance() 
  1376. if (!is_object($this->smtp)) { 
  1377. require_once( 'class-smtp.php' ); 
  1378. $this->smtp = new SMTP; 
  1379. return $this->smtp; 
  1380.  
  1381. /** 
  1382. * Send mail via SMTP. 
  1383. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. 
  1384. * Uses the PHPMailerSMTP class by default. 
  1385. * @see PHPMailer::getSMTPInstance() to use a different class. 
  1386. * @param string $header The message headers 
  1387. * @param string $body The message body 
  1388. * @throws phpmailerException 
  1389. * @uses SMTP 
  1390. * @access protected 
  1391. * @return boolean 
  1392. */ 
  1393. protected function smtpSend($header, $body) 
  1394. $bad_rcpt = array(); 
  1395. if (!$this->smtpConnect($this->SMTPOptions)) { 
  1396. throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); 
  1397. if (!empty($this->Sender) and $this->validateAddress($this->Sender)) { 
  1398. $smtp_from = $this->Sender; 
  1399. } else { 
  1400. $smtp_from = $this->From; 
  1401. if (!$this->smtp->mail($smtp_from)) { 
  1402. $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(', ', $this->smtp->getError())); 
  1403. throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL); 
  1404.  
  1405. // Attempt to send to all recipients 
  1406. foreach (array($this->to, $this->cc, $this->bcc) as $togroup) { 
  1407. foreach ($togroup as $to) { 
  1408. if (!$this->smtp->recipient($to[0])) { 
  1409. $error = $this->smtp->getError(); 
  1410. $bad_rcpt[] = array('to' => $to[0], 'error' => $error['detail']); 
  1411. $isSent = false; 
  1412. } else { 
  1413. $isSent = true; 
  1414. $this->doCallback($isSent, array($to[0]), array(), array(), $this->Subject, $body, $this->From); 
  1415.  
  1416. // Only send the DATA command if we have viable recipients 
  1417. if ((count($this->all_recipients) > count($bad_rcpt)) and !$this->smtp->data($header . $body)) { 
  1418. throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL); 
  1419. if ($this->SMTPKeepAlive) { 
  1420. $this->smtp->reset(); 
  1421. } else { 
  1422. $this->smtp->quit(); 
  1423. $this->smtp->close(); 
  1424. //Create error message for any bad addresses 
  1425. if (count($bad_rcpt) > 0) { 
  1426. $errstr = ''; 
  1427. foreach ($bad_rcpt as $bad) { 
  1428. $errstr .= $bad['to'] . ': ' . $bad['error']; 
  1429. throw new phpmailerException( 
  1430. $this->lang('recipients_failed') . $errstr,  
  1431. self::STOP_CONTINUE 
  1432. ); 
  1433. return true; 
  1434.  
  1435. /** 
  1436. * Initiate a connection to an SMTP server. 
  1437. * Returns false if the operation failed. 
  1438. * @param array $options An array of options compatible with stream_context_create() 
  1439. * @uses SMTP 
  1440. * @access public 
  1441. * @throws phpmailerException 
  1442. * @return boolean 
  1443. */ 
  1444. public function smtpConnect($options = null) 
  1445. if (is_null($this->smtp)) { 
  1446. $this->smtp = $this->getSMTPInstance(); 
  1447.  
  1448. //If no options are provided, use whatever is set in the instance 
  1449. if (is_null($options)) { 
  1450. $options = $this->SMTPOptions; 
  1451.  
  1452. // Already connected? 
  1453. if ($this->smtp->connected()) { 
  1454. return true; 
  1455.  
  1456. $this->smtp->setTimeout($this->Timeout); 
  1457. $this->smtp->setDebugLevel($this->SMTPDebug); 
  1458. $this->smtp->setDebugOutput($this->Debugoutput); 
  1459. $this->smtp->setVerp($this->do_verp); 
  1460. $hosts = explode(';', $this->Host); 
  1461. $lastexception = null; 
  1462.  
  1463. foreach ($hosts as $hostentry) { 
  1464. $hostinfo = array(); 
  1465. if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) { 
  1466. // Not a valid host entry 
  1467. continue; 
  1468. // $hostinfo[2]: optional ssl or tls prefix 
  1469. // $hostinfo[3]: the hostname 
  1470. // $hostinfo[4]: optional port number 
  1471. // The host string prefix can temporarily override the current setting for SMTPSecure 
  1472. // If it's not specified, the default value is used 
  1473. $prefix = ''; 
  1474. $secure = $this->SMTPSecure; 
  1475. $tls = ($this->SMTPSecure == 'tls'); 
  1476. if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) { 
  1477. $prefix = 'ssl://'; 
  1478. $tls = false; // Can't have SSL and TLS at the same time 
  1479. $secure = 'ssl'; 
  1480. } elseif ($hostinfo[2] == 'tls') { 
  1481. $tls = true; 
  1482. // tls doesn't use a prefix 
  1483. $secure = 'tls'; 
  1484. //Do we need the OpenSSL extension? 
  1485. $sslext = defined('OPENSSL_ALGO_SHA1'); 
  1486. if ('tls' === $secure or 'ssl' === $secure) { 
  1487. //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled 
  1488. if (!$sslext) { 
  1489. throw new phpmailerException($this->lang('extension_missing').'openssl', self::STOP_CRITICAL); 
  1490. $host = $hostinfo[3]; 
  1491. $port = $this->Port; 
  1492. $tport = (integer)$hostinfo[4]; 
  1493. if ($tport > 0 and $tport < 65536) { 
  1494. $port = $tport; 
  1495. if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { 
  1496. try { 
  1497. if ($this->Helo) { 
  1498. $hello = $this->Helo; 
  1499. } else { 
  1500. $hello = $this->serverHostname(); 
  1501. $this->smtp->hello($hello); 
  1502. //Automatically enable TLS encryption if: 
  1503. // * it's not disabled 
  1504. // * we have openssl extension 
  1505. // * we are not already using SSL 
  1506. // * the server offers STARTTLS 
  1507. if ($this->SMTPAutoTLS and $sslext and $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) { 
  1508. $tls = true; 
  1509. if ($tls) { 
  1510. if (!$this->smtp->startTLS()) { 
  1511. throw new phpmailerException($this->lang('connect_host')); 
  1512. // We must resend EHLO after TLS negotiation 
  1513. $this->smtp->hello($hello); 
  1514. if ($this->SMTPAuth) { 
  1515. if (!$this->smtp->authenticate( 
  1516. $this->Username,  
  1517. $this->Password,  
  1518. $this->AuthType,  
  1519. $this->Realm,  
  1520. $this->Workstation 
  1521. ) { 
  1522. throw new phpmailerException($this->lang('authenticate')); 
  1523. return true; 
  1524. } catch (phpmailerException $exc) { 
  1525. $lastexception = $exc; 
  1526. $this->edebug($exc->getMessage()); 
  1527. // We must have connected, but then failed TLS or Auth, so close connection nicely 
  1528. $this->smtp->quit(); 
  1529. // If we get here, all connection attempts have failed, so close connection hard 
  1530. $this->smtp->close(); 
  1531. // As we've caught all exceptions, just report whatever the last one was 
  1532. if ($this->exceptions and !is_null($lastexception)) { 
  1533. throw $lastexception; 
  1534. return false; 
  1535.  
  1536. /** 
  1537. * Close the active SMTP session if one exists. 
  1538. * @return void 
  1539. */ 
  1540. public function smtpClose() 
  1541. if (is_a($this->smtp, 'SMTP')) { 
  1542. if ($this->smtp->connected()) { 
  1543. $this->smtp->quit(); 
  1544. $this->smtp->close(); 
  1545.  
  1546. /** 
  1547. * Set the language for error messages. 
  1548. * Returns false if it cannot load the language file. 
  1549. * The default language is English. 
  1550. * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr") 
  1551. * @param string $lang_path Path to the language file directory, with trailing separator (slash) 
  1552. * @return boolean 
  1553. * @access public 
  1554. */ 
  1555. public function setLanguage($langcode = 'en', $lang_path = '') 
  1556. // Backwards compatibility for renamed language codes 
  1557. $renamed_langcodes = array( 
  1558. 'br' => 'pt_br',  
  1559. 'cz' => 'cs',  
  1560. 'dk' => 'da',  
  1561. 'no' => 'nb',  
  1562. 'se' => 'sv',  
  1563. ); 
  1564.  
  1565. if (isset($renamed_langcodes[$langcode])) { 
  1566. $langcode = $renamed_langcodes[$langcode]; 
  1567.  
  1568. // Define full set of translatable strings in English 
  1569. $PHPMAILER_LANG = array( 
  1570. 'authenticate' => 'SMTP Error: Could not authenticate.',  
  1571. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',  
  1572. 'data_not_accepted' => 'SMTP Error: data not accepted.',  
  1573. 'empty_message' => 'Message body empty',  
  1574. 'encoding' => 'Unknown encoding: ',  
  1575. 'execute' => 'Could not execute: ',  
  1576. 'file_access' => 'Could not access file: ',  
  1577. 'file_open' => 'File Error: Could not open file: ',  
  1578. 'from_failed' => 'The following From address failed: ',  
  1579. 'instantiate' => 'Could not instantiate mail function.',  
  1580. 'invalid_address' => 'Invalid address: ',  
  1581. 'mailer_not_supported' => ' mailer is not supported.',  
  1582. 'provide_address' => 'You must provide at least one recipient email address.',  
  1583. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',  
  1584. 'signing' => 'Signing Error: ',  
  1585. 'smtp_connect_failed' => 'SMTP connect() failed.',  
  1586. 'smtp_error' => 'SMTP server error: ',  
  1587. 'variable_set' => 'Cannot set or reset variable: ',  
  1588. 'extension_missing' => 'Extension missing: ' 
  1589. ); 
  1590. if (empty($lang_path)) { 
  1591. // Calculate an absolute path so it can work if CWD is not here 
  1592. $lang_path = dirname(__FILE__). DIRECTORY_SEPARATOR . 'language'. DIRECTORY_SEPARATOR; 
  1593. //Validate $langcode 
  1594. if (!preg_match('/^[a-z]{2}(?:_[a-zA-Z]{2})?$/', $langcode)) { 
  1595. $langcode = 'en'; 
  1596. $foundlang = true; 
  1597. $lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php'; 
  1598. // There is no English translation file 
  1599. if ($langcode != 'en') { 
  1600. // Make sure language file path is readable 
  1601. if (!is_readable($lang_file)) { 
  1602. $foundlang = false; 
  1603. } else { 
  1604. // Overwrite language-specific strings. 
  1605. // This way we'll never have missing translation keys. 
  1606. $foundlang = include $lang_file; 
  1607. $this->language = $PHPMAILER_LANG; 
  1608. return (boolean)$foundlang; // Returns false if language not found 
  1609.  
  1610. /** 
  1611. * Get the array of strings for the current language. 
  1612. * @return array 
  1613. */ 
  1614. public function getTranslations() 
  1615. return $this->language; 
  1616.  
  1617. /** 
  1618. * Create recipient headers. 
  1619. * @access public 
  1620. * @param string $type 
  1621. * @param array $addr An array of recipient,  
  1622. * where each recipient is a 2-element indexed array with element 0 containing an address 
  1623. * and element 1 containing a name, like: 
  1624. * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User')) 
  1625. * @return string 
  1626. */ 
  1627. public function addrAppend($type, $addr) 
  1628. $addresses = array(); 
  1629. foreach ($addr as $address) { 
  1630. $addresses[] = $this->addrFormat($address); 
  1631. return $type . ': ' . implode(', ', $addresses) . $this->LE; 
  1632.  
  1633. /** 
  1634. * Format an address for use in a message header. 
  1635. * @access public 
  1636. * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name 
  1637. * like array('joe@example.com', 'Joe User') 
  1638. * @return string 
  1639. */ 
  1640. public function addrFormat($addr) 
  1641. if (empty($addr[1])) { // No name provided 
  1642. return $this->secureHeader($addr[0]); 
  1643. } else { 
  1644. return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . ' <' . $this->secureHeader( 
  1645. $addr[0] 
  1646. ) . '>'; 
  1647.  
  1648. /** 
  1649. * Word-wrap message. 
  1650. * For use with mailers that do not automatically perform wrapping 
  1651. * and for quoted-printable encoded messages. 
  1652. * Original written by philippe. 
  1653. * @param string $message The message to wrap 
  1654. * @param integer $length The line length to wrap to 
  1655. * @param boolean $qp_mode Whether to run in Quoted-Printable mode 
  1656. * @access public 
  1657. * @return string 
  1658. */ 
  1659. public function wrapText($message, $length, $qp_mode = false) 
  1660. if ($qp_mode) { 
  1661. $soft_break = sprintf(' =%s', $this->LE); 
  1662. } else { 
  1663. $soft_break = $this->LE; 
  1664. // If utf-8 encoding is used, we will need to make sure we don't 
  1665. // split multibyte characters when we wrap 
  1666. $is_utf8 = (strtolower($this->CharSet) == 'utf-8'); 
  1667. $lelen = strlen($this->LE); 
  1668. $crlflen = strlen(self::CRLF); 
  1669.  
  1670. $message = $this->fixEOL($message); 
  1671. //Remove a trailing line break 
  1672. if (substr($message, -$lelen) == $this->LE) { 
  1673. $message = substr($message, 0, -$lelen); 
  1674.  
  1675. //Split message into lines 
  1676. $lines = explode($this->LE, $message); 
  1677. //Message will be rebuilt in here 
  1678. $message = ''; 
  1679. foreach ($lines as $line) { 
  1680. $words = explode(' ', $line); 
  1681. $buf = ''; 
  1682. $firstword = true; 
  1683. foreach ($words as $word) { 
  1684. if ($qp_mode and (strlen($word) > $length)) { 
  1685. $space_left = $length - strlen($buf) - $crlflen; 
  1686. if (!$firstword) { 
  1687. if ($space_left > 20) { 
  1688. $len = $space_left; 
  1689. if ($is_utf8) { 
  1690. $len = $this->utf8CharBoundary($word, $len); 
  1691. } elseif (substr($word, $len - 1, 1) == '=') { 
  1692. $len--; 
  1693. } elseif (substr($word, $len - 2, 1) == '=') { 
  1694. $len -= 2; 
  1695. $part = substr($word, 0, $len); 
  1696. $word = substr($word, $len); 
  1697. $buf .= ' ' . $part; 
  1698. $message .= $buf . sprintf('=%s', self::CRLF); 
  1699. } else { 
  1700. $message .= $buf . $soft_break; 
  1701. $buf = ''; 
  1702. while (strlen($word) > 0) { 
  1703. if ($length <= 0) { 
  1704. break; 
  1705. $len = $length; 
  1706. if ($is_utf8) { 
  1707. $len = $this->utf8CharBoundary($word, $len); 
  1708. } elseif (substr($word, $len - 1, 1) == '=') { 
  1709. $len--; 
  1710. } elseif (substr($word, $len - 2, 1) == '=') { 
  1711. $len -= 2; 
  1712. $part = substr($word, 0, $len); 
  1713. $word = substr($word, $len); 
  1714.  
  1715. if (strlen($word) > 0) { 
  1716. $message .= $part . sprintf('=%s', self::CRLF); 
  1717. } else { 
  1718. $buf = $part; 
  1719. } else { 
  1720. $buf_o = $buf; 
  1721. if (!$firstword) { 
  1722. $buf .= ' '; 
  1723. $buf .= $word; 
  1724.  
  1725. if (strlen($buf) > $length and $buf_o != '') { 
  1726. $message .= $buf_o . $soft_break; 
  1727. $buf = $word; 
  1728. $firstword = false; 
  1729. $message .= $buf . self::CRLF; 
  1730.  
  1731. return $message; 
  1732.  
  1733. /** 
  1734. * Find the last character boundary prior to $maxLength in a utf-8 
  1735. * quoted-printable encoded string. 
  1736. * Original written by Colin Brown. 
  1737. * @access public 
  1738. * @param string $encodedText utf-8 QP text 
  1739. * @param integer $maxLength Find the last character boundary prior to this length 
  1740. * @return integer 
  1741. */ 
  1742. public function utf8CharBoundary($encodedText, $maxLength) 
  1743. $foundSplitPos = false; 
  1744. $lookBack = 3; 
  1745. while (!$foundSplitPos) { 
  1746. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack); 
  1747. $encodedCharPos = strpos($lastChunk, '='); 
  1748. if (false !== $encodedCharPos) { 
  1749. // Found start of encoded character byte within $lookBack block. 
  1750. // Check the encoded byte value (the 2 chars after the '=') 
  1751. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2); 
  1752. $dec = hexdec($hex); 
  1753. if ($dec < 128) { 
  1754. // Single byte character. 
  1755. // If the encoded char was found at pos 0, it will fit 
  1756. // otherwise reduce maxLength to start of the encoded char 
  1757. if ($encodedCharPos > 0) { 
  1758. $maxLength = $maxLength - ($lookBack - $encodedCharPos); 
  1759. $foundSplitPos = true; 
  1760. } elseif ($dec >= 192) { 
  1761. // First byte of a multi byte character 
  1762. // Reduce maxLength to split at start of character 
  1763. $maxLength = $maxLength - ($lookBack - $encodedCharPos); 
  1764. $foundSplitPos = true; 
  1765. } elseif ($dec < 192) { 
  1766. // Middle byte of a multi byte character, look further back 
  1767. $lookBack += 3; 
  1768. } else { 
  1769. // No encoded character found 
  1770. $foundSplitPos = true; 
  1771. return $maxLength; 
  1772.  
  1773. /** 
  1774. * Apply word wrapping to the message body. 
  1775. * Wraps the message body to the number of chars set in the WordWrap property. 
  1776. * You should only do this to plain-text bodies as wrapping HTML tags may break them. 
  1777. * This is called automatically by createBody(), so you don't need to call it yourself. 
  1778. * @access public 
  1779. * @return void 
  1780. */ 
  1781. public function setWordWrap() 
  1782. if ($this->WordWrap < 1) { 
  1783. return; 
  1784.  
  1785. switch ($this->message_type) { 
  1786. case 'alt': 
  1787. case 'alt_inline': 
  1788. case 'alt_attach': 
  1789. case 'alt_inline_attach': 
  1790. $this->AltBody = $this->wrapText($this->AltBody, $this->WordWrap); 
  1791. break; 
  1792. default: 
  1793. $this->Body = $this->wrapText($this->Body, $this->WordWrap); 
  1794. break; 
  1795.  
  1796. /** 
  1797. * Assemble message headers. 
  1798. * @access public 
  1799. * @return string The assembled headers 
  1800. */ 
  1801. public function createHeader() 
  1802. $result = ''; 
  1803.  
  1804. if ($this->MessageDate == '') { 
  1805. $this->MessageDate = self::rfcDate(); 
  1806. $result .= $this->headerLine('Date', $this->MessageDate); 
  1807.  
  1808. // To be created automatically by mail() 
  1809. if ($this->SingleTo) { 
  1810. if ($this->Mailer != 'mail') { 
  1811. foreach ($this->to as $toaddr) { 
  1812. $this->SingleToArray[] = $this->addrFormat($toaddr); 
  1813. } else { 
  1814. if (count($this->to) > 0) { 
  1815. if ($this->Mailer != 'mail') { 
  1816. $result .= $this->addrAppend('To', $this->to); 
  1817. } elseif (count($this->cc) == 0) { 
  1818. $result .= $this->headerLine('To', 'undisclosed-recipients:;'); 
  1819.  
  1820. $result .= $this->addrAppend('From', array(array(trim($this->From), $this->FromName))); 
  1821.  
  1822. // sendmail and mail() extract Cc from the header before sending 
  1823. if (count($this->cc) > 0) { 
  1824. $result .= $this->addrAppend('Cc', $this->cc); 
  1825.  
  1826. // sendmail and mail() extract Bcc from the header before sending 
  1827. if (( 
  1828. $this->Mailer == 'sendmail' or $this->Mailer == 'qmail' or $this->Mailer == 'mail' 
  1829. and count($this->bcc) > 0 
  1830. ) { 
  1831. $result .= $this->addrAppend('Bcc', $this->bcc); 
  1832.  
  1833. if (count($this->ReplyTo) > 0) { 
  1834. $result .= $this->addrAppend('Reply-To', $this->ReplyTo); 
  1835.  
  1836. // mail() sets the subject itself 
  1837. if ($this->Mailer != 'mail') { 
  1838. $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject))); 
  1839.  
  1840. // Only allow a custom message ID if it conforms to RFC 5322 section 3.6.4 
  1841. // https://tools.ietf.org/html/rfc5322#section-3.6.4 
  1842. if ('' != $this->MessageID and preg_match('/^<.*@.*>$/', $this->MessageID)) { 
  1843. $this->lastMessageID = $this->MessageID; 
  1844. } else { 
  1845. $this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->serverHostname()); 
  1846. $result .= $this->headerLine('Message-ID', $this->lastMessageID); 
  1847. if (!is_null($this->Priority)) { 
  1848. $result .= $this->headerLine('X-Priority', $this->Priority); 
  1849. if ($this->XMailer == '') { 
  1850. $result .= $this->headerLine( 
  1851. 'X-Mailer',  
  1852. 'PHPMailer ' . $this->Version . ' (https://github.com/PHPMailer/PHPMailer)' 
  1853. ); 
  1854. } else { 
  1855. $myXmailer = trim($this->XMailer); 
  1856. if ($myXmailer) { 
  1857. $result .= $this->headerLine('X-Mailer', $myXmailer); 
  1858.  
  1859. if ($this->ConfirmReadingTo != '') { 
  1860. $result .= $this->headerLine('Disposition-Notification-To', '<' . $this->ConfirmReadingTo . '>'); 
  1861.  
  1862. // Add custom headers 
  1863. foreach ($this->CustomHeader as $header) { 
  1864. $result .= $this->headerLine( 
  1865. trim($header[0]),  
  1866. $this->encodeHeader(trim($header[1])) 
  1867. ); 
  1868. if (!$this->sign_key_file) { 
  1869. $result .= $this->headerLine('MIME-Version', '1.0'); 
  1870. $result .= $this->getMailMIME(); 
  1871.  
  1872. return $result; 
  1873.  
  1874. /** 
  1875. * Get the message MIME type headers. 
  1876. * @access public 
  1877. * @return string 
  1878. */ 
  1879. public function getMailMIME() 
  1880. $result = ''; 
  1881. $ismultipart = true; 
  1882. switch ($this->message_type) { 
  1883. case 'inline': 
  1884. $result .= $this->headerLine('Content-Type', 'multipart/related;'); 
  1885. $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"'); 
  1886. break; 
  1887. case 'attach': 
  1888. case 'inline_attach': 
  1889. case 'alt_attach': 
  1890. case 'alt_inline_attach': 
  1891. $result .= $this->headerLine('Content-Type', 'multipart/mixed;'); 
  1892. $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"'); 
  1893. break; 
  1894. case 'alt': 
  1895. case 'alt_inline': 
  1896. $result .= $this->headerLine('Content-Type', 'multipart/alternative;'); 
  1897. $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"'); 
  1898. break; 
  1899. default: 
  1900. // Catches case 'plain': and case '': 
  1901. $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet); 
  1902. $ismultipart = false; 
  1903. break; 
  1904. // RFC1341 part 5 says 7bit is assumed if not specified 
  1905. if ($this->Encoding != '7bit') { 
  1906. // RFC 2045 section 6.4 says multipart MIME parts may only use 7bit, 8bit or binary CTE 
  1907. if ($ismultipart) { 
  1908. if ($this->Encoding == '8bit') { 
  1909. $result .= $this->headerLine('Content-Transfer-Encoding', '8bit'); 
  1910. // The only remaining alternatives are quoted-printable and base64, which are both 7bit compatible 
  1911. } else { 
  1912. $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding); 
  1913.  
  1914. if ($this->Mailer != 'mail') { 
  1915. $result .= $this->LE; 
  1916.  
  1917. return $result; 
  1918.  
  1919. /** 
  1920. * Returns the whole MIME message. 
  1921. * Includes complete headers and body. 
  1922. * Only valid post preSend(). 
  1923. * @see PHPMailer::preSend() 
  1924. * @access public 
  1925. * @return string 
  1926. */ 
  1927. public function getSentMIMEMessage() 
  1928. return rtrim($this->MIMEHeader . $this->mailHeader, "\n\r") . self::CRLF . self::CRLF . $this->MIMEBody; 
  1929.  
  1930. /** 
  1931. * Create unique ID 
  1932. * @return string 
  1933. */ 
  1934. protected function generateId() { 
  1935. return md5(uniqid(time())); 
  1936.  
  1937. /** 
  1938. * Assemble the message body. 
  1939. * Returns an empty string on failure. 
  1940. * @access public 
  1941. * @throws phpmailerException 
  1942. * @return string The assembled message body 
  1943. */ 
  1944. public function createBody() 
  1945. $body = ''; 
  1946. //Create unique IDs and preset boundaries 
  1947. $this->uniqueid = $this->generateId(); 
  1948. $this->boundary[1] = 'b1_' . $this->uniqueid; 
  1949. $this->boundary[2] = 'b2_' . $this->uniqueid; 
  1950. $this->boundary[3] = 'b3_' . $this->uniqueid; 
  1951.  
  1952. if ($this->sign_key_file) { 
  1953. $body .= $this->getMailMIME() . $this->LE; 
  1954.  
  1955. $this->setWordWrap(); 
  1956.  
  1957. $bodyEncoding = $this->Encoding; 
  1958. $bodyCharSet = $this->CharSet; 
  1959. //Can we do a 7-bit downgrade? 
  1960. if ($bodyEncoding == '8bit' and !$this->has8bitChars($this->Body)) { 
  1961. $bodyEncoding = '7bit'; 
  1962. //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit 
  1963. $bodyCharSet = 'us-ascii'; 
  1964. //If lines are too long, and we're not already using an encoding that will shorten them,  
  1965. //change to quoted-printable transfer encoding for the body part only 
  1966. if ('base64' != $this->Encoding and self::hasLineLongerThanMax($this->Body)) { 
  1967. $bodyEncoding = 'quoted-printable'; 
  1968.  
  1969. $altBodyEncoding = $this->Encoding; 
  1970. $altBodyCharSet = $this->CharSet; 
  1971. //Can we do a 7-bit downgrade? 
  1972. if ($altBodyEncoding == '8bit' and !$this->has8bitChars($this->AltBody)) { 
  1973. $altBodyEncoding = '7bit'; 
  1974. //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit 
  1975. $altBodyCharSet = 'us-ascii'; 
  1976. //If lines are too long, and we're not already using an encoding that will shorten them,  
  1977. //change to quoted-printable transfer encoding for the alt body part only 
  1978. if ('base64' != $altBodyEncoding and self::hasLineLongerThanMax($this->AltBody)) { 
  1979. $altBodyEncoding = 'quoted-printable'; 
  1980. //Use this as a preamble in all multipart message types 
  1981. $mimepre = "This is a multi-part message in MIME format." . $this->LE . $this->LE; 
  1982. switch ($this->message_type) { 
  1983. case 'inline': 
  1984. $body .= $mimepre; 
  1985. $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); 
  1986. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  1987. $body .= $this->LE . $this->LE; 
  1988. $body .= $this->attachAll('inline', $this->boundary[1]); 
  1989. break; 
  1990. case 'attach': 
  1991. $body .= $mimepre; 
  1992. $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); 
  1993. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  1994. $body .= $this->LE . $this->LE; 
  1995. $body .= $this->attachAll('attachment', $this->boundary[1]); 
  1996. break; 
  1997. case 'inline_attach': 
  1998. $body .= $mimepre; 
  1999. $body .= $this->textLine('--' . $this->boundary[1]); 
  2000. $body .= $this->headerLine('Content-Type', 'multipart/related;'); 
  2001. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); 
  2002. $body .= $this->LE; 
  2003. $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding); 
  2004. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  2005. $body .= $this->LE . $this->LE; 
  2006. $body .= $this->attachAll('inline', $this->boundary[2]); 
  2007. $body .= $this->LE; 
  2008. $body .= $this->attachAll('attachment', $this->boundary[1]); 
  2009. break; 
  2010. case 'alt': 
  2011. $body .= $mimepre; 
  2012. $body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding); 
  2013. $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 
  2014. $body .= $this->LE . $this->LE; 
  2015. $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, 'text/html', $bodyEncoding); 
  2016. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  2017. $body .= $this->LE . $this->LE; 
  2018. if (!empty($this->Ical)) { 
  2019. $body .= $this->getBoundary($this->boundary[1], '', 'text/calendar; method=REQUEST', ''); 
  2020. $body .= $this->encodeString($this->Ical, $this->Encoding); 
  2021. $body .= $this->LE . $this->LE; 
  2022. $body .= $this->endBoundary($this->boundary[1]); 
  2023. break; 
  2024. case 'alt_inline': 
  2025. $body .= $mimepre; 
  2026. $body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding); 
  2027. $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 
  2028. $body .= $this->LE . $this->LE; 
  2029. $body .= $this->textLine('--' . $this->boundary[1]); 
  2030. $body .= $this->headerLine('Content-Type', 'multipart/related;'); 
  2031. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); 
  2032. $body .= $this->LE; 
  2033. $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding); 
  2034. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  2035. $body .= $this->LE . $this->LE; 
  2036. $body .= $this->attachAll('inline', $this->boundary[2]); 
  2037. $body .= $this->LE; 
  2038. $body .= $this->endBoundary($this->boundary[1]); 
  2039. break; 
  2040. case 'alt_attach': 
  2041. $body .= $mimepre; 
  2042. $body .= $this->textLine('--' . $this->boundary[1]); 
  2043. $body .= $this->headerLine('Content-Type', 'multipart/alternative;'); 
  2044. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); 
  2045. $body .= $this->LE; 
  2046. $body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding); 
  2047. $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 
  2048. $body .= $this->LE . $this->LE; 
  2049. $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding); 
  2050. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  2051. $body .= $this->LE . $this->LE; 
  2052. $body .= $this->endBoundary($this->boundary[2]); 
  2053. $body .= $this->LE; 
  2054. $body .= $this->attachAll('attachment', $this->boundary[1]); 
  2055. break; 
  2056. case 'alt_inline_attach': 
  2057. $body .= $mimepre; 
  2058. $body .= $this->textLine('--' . $this->boundary[1]); 
  2059. $body .= $this->headerLine('Content-Type', 'multipart/alternative;'); 
  2060. $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); 
  2061. $body .= $this->LE; 
  2062. $body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding); 
  2063. $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 
  2064. $body .= $this->LE . $this->LE; 
  2065. $body .= $this->textLine('--' . $this->boundary[2]); 
  2066. $body .= $this->headerLine('Content-Type', 'multipart/related;'); 
  2067. $body .= $this->textLine("\tboundary=\"" . $this->boundary[3] . '"'); 
  2068. $body .= $this->LE; 
  2069. $body .= $this->getBoundary($this->boundary[3], $bodyCharSet, 'text/html', $bodyEncoding); 
  2070. $body .= $this->encodeString($this->Body, $bodyEncoding); 
  2071. $body .= $this->LE . $this->LE; 
  2072. $body .= $this->attachAll('inline', $this->boundary[3]); 
  2073. $body .= $this->LE; 
  2074. $body .= $this->endBoundary($this->boundary[2]); 
  2075. $body .= $this->LE; 
  2076. $body .= $this->attachAll('attachment', $this->boundary[1]); 
  2077. break; 
  2078. default: 
  2079. // Catch case 'plain' and case '', applies to simple `text/plain` and `text/html` body content types 
  2080. //Reset the `Encoding` property in case we changed it for line length reasons 
  2081. $this->Encoding = $bodyEncoding; 
  2082. $body .= $this->encodeString($this->Body, $this->Encoding); 
  2083. break; 
  2084.  
  2085. if ($this->isError()) { 
  2086. $body = ''; 
  2087. } elseif ($this->sign_key_file) { 
  2088. try { 
  2089. if (!defined('PKCS7_TEXT')) { 
  2090. throw new phpmailerException($this->lang('extension_missing') . 'openssl'); 
  2091. // @TODO would be nice to use php://temp streams here, but need to wrap for PHP < 5.1 
  2092. $file = tempnam(sys_get_temp_dir(), 'mail'); 
  2093. if (false === file_put_contents($file, $body)) { 
  2094. throw new phpmailerException($this->lang('signing') . ' Could not write temp file'); 
  2095. $signed = tempnam(sys_get_temp_dir(), 'signed'); 
  2096. //Workaround for PHP bug https://bugs.php.net/bug.php?id=69197 
  2097. if (empty($this->sign_extracerts_file)) { 
  2098. $sign = @openssl_pkcs7_sign( 
  2099. $file,  
  2100. $signed,  
  2101. 'file://' . realpath($this->sign_cert_file),  
  2102. array('file://' . realpath($this->sign_key_file), $this->sign_key_pass),  
  2103. null 
  2104. ); 
  2105. } else { 
  2106. $sign = @openssl_pkcs7_sign( 
  2107. $file,  
  2108. $signed,  
  2109. 'file://' . realpath($this->sign_cert_file),  
  2110. array('file://' . realpath($this->sign_key_file), $this->sign_key_pass),  
  2111. null,  
  2112. PKCS7_DETACHED,  
  2113. $this->sign_extracerts_file 
  2114. ); 
  2115. if ($sign) { 
  2116. @unlink($file); 
  2117. $body = file_get_contents($signed); 
  2118. @unlink($signed); 
  2119. //The message returned by openssl contains both headers and body, so need to split them up 
  2120. $parts = explode("\n\n", $body, 2); 
  2121. $this->MIMEHeader .= $parts[0] . $this->LE . $this->LE; 
  2122. $body = $parts[1]; 
  2123. } else { 
  2124. @unlink($file); 
  2125. @unlink($signed); 
  2126. throw new phpmailerException($this->lang('signing') . openssl_error_string()); 
  2127. } catch (phpmailerException $exc) { 
  2128. $body = ''; 
  2129. if ($this->exceptions) { 
  2130. throw $exc; 
  2131. return $body; 
  2132.  
  2133. /** 
  2134. * Return the start of a message boundary. 
  2135. * @access protected 
  2136. * @param string $boundary 
  2137. * @param string $charSet 
  2138. * @param string $contentType 
  2139. * @param string $encoding 
  2140. * @return string 
  2141. */ 
  2142. protected function getBoundary($boundary, $charSet, $contentType, $encoding) 
  2143. $result = ''; 
  2144. if ($charSet == '') { 
  2145. $charSet = $this->CharSet; 
  2146. if ($contentType == '') { 
  2147. $contentType = $this->ContentType; 
  2148. if ($encoding == '') { 
  2149. $encoding = $this->Encoding; 
  2150. $result .= $this->textLine('--' . $boundary); 
  2151. $result .= sprintf('Content-Type: %s; charset=%s', $contentType, $charSet); 
  2152. $result .= $this->LE; 
  2153. // RFC1341 part 5 says 7bit is assumed if not specified 
  2154. if ($encoding != '7bit') { 
  2155. $result .= $this->headerLine('Content-Transfer-Encoding', $encoding); 
  2156. $result .= $this->LE; 
  2157.  
  2158. return $result; 
  2159.  
  2160. /** 
  2161. * Return the end of a message boundary. 
  2162. * @access protected 
  2163. * @param string $boundary 
  2164. * @return string 
  2165. */ 
  2166. protected function endBoundary($boundary) 
  2167. return $this->LE . '--' . $boundary . '--' . $this->LE; 
  2168.  
  2169. /** 
  2170. * Set the message type. 
  2171. * PHPMailer only supports some preset message types, not arbitrary MIME structures. 
  2172. * @access protected 
  2173. * @return void 
  2174. */ 
  2175. protected function setMessageType() 
  2176. $type = array(); 
  2177. if ($this->alternativeExists()) { 
  2178. $type[] = 'alt'; 
  2179. if ($this->inlineImageExists()) { 
  2180. $type[] = 'inline'; 
  2181. if ($this->attachmentExists()) { 
  2182. $type[] = 'attach'; 
  2183. $this->message_type = implode('_', $type); 
  2184. if ($this->message_type == '') { 
  2185. //The 'plain' message_type refers to the message having a single body element, not that it is plain-text 
  2186. $this->message_type = 'plain'; 
  2187.  
  2188. /** 
  2189. * Format a header line. 
  2190. * @access public 
  2191. * @param string $name 
  2192. * @param string $value 
  2193. * @return string 
  2194. */ 
  2195. public function headerLine($name, $value) 
  2196. return $name . ': ' . $value . $this->LE; 
  2197.  
  2198. /** 
  2199. * Return a formatted mail line. 
  2200. * @access public 
  2201. * @param string $value 
  2202. * @return string 
  2203. */ 
  2204. public function textLine($value) 
  2205. return $value . $this->LE; 
  2206.  
  2207. /** 
  2208. * Add an attachment from a path on the filesystem. 
  2209. * Never use a user-supplied path to a file! 
  2210. * Returns false if the file could not be found or read. 
  2211. * @param string $path Path to the attachment. 
  2212. * @param string $name Overrides the attachment name. 
  2213. * @param string $encoding File encoding (see $Encoding). 
  2214. * @param string $type File extension (MIME) type. 
  2215. * @param string $disposition Disposition to use 
  2216. * @throws phpmailerException 
  2217. * @return boolean 
  2218. */ 
  2219. public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment') 
  2220. try { 
  2221. if (!@is_file($path)) { 
  2222. throw new phpmailerException($this->lang('file_access') . $path, self::STOP_CONTINUE); 
  2223.  
  2224. // If a MIME type is not specified, try to work it out from the file name 
  2225. if ($type == '') { 
  2226. $type = self::filenameToType($path); 
  2227.  
  2228. $filename = basename($path); 
  2229. if ($name == '') { 
  2230. $name = $filename; 
  2231.  
  2232. $this->attachment[] = array( 
  2233. 0 => $path,  
  2234. 1 => $filename,  
  2235. 2 => $name,  
  2236. 3 => $encoding,  
  2237. 4 => $type,  
  2238. 5 => false, // isStringAttachment 
  2239. 6 => $disposition,  
  2240. 7 => 0 
  2241. ); 
  2242.  
  2243. } catch (phpmailerException $exc) { 
  2244. $this->setError($exc->getMessage()); 
  2245. $this->edebug($exc->getMessage()); 
  2246. if ($this->exceptions) { 
  2247. throw $exc; 
  2248. return false; 
  2249. return true; 
  2250.  
  2251. /** 
  2252. * Return the array of attachments. 
  2253. * @return array 
  2254. */ 
  2255. public function getAttachments() 
  2256. return $this->attachment; 
  2257.  
  2258. /** 
  2259. * Attach all file, string, and binary attachments to the message. 
  2260. * Returns an empty string on failure. 
  2261. * @access protected 
  2262. * @param string $disposition_type 
  2263. * @param string $boundary 
  2264. * @return string 
  2265. */ 
  2266. protected function attachAll($disposition_type, $boundary) 
  2267. // Return text of body 
  2268. $mime = array(); 
  2269. $cidUniq = array(); 
  2270. $incl = array(); 
  2271.  
  2272. // Add all attachments 
  2273. foreach ($this->attachment as $attachment) { 
  2274. // Check if it is a valid disposition_filter 
  2275. if ($attachment[6] == $disposition_type) { 
  2276. // Check for string attachment 
  2277. $string = ''; 
  2278. $path = ''; 
  2279. $bString = $attachment[5]; 
  2280. if ($bString) { 
  2281. $string = $attachment[0]; 
  2282. } else { 
  2283. $path = $attachment[0]; 
  2284.  
  2285. $inclhash = md5(serialize($attachment)); 
  2286. if (in_array($inclhash, $incl)) { 
  2287. continue; 
  2288. $incl[] = $inclhash; 
  2289. $name = $attachment[2]; 
  2290. $encoding = $attachment[3]; 
  2291. $type = $attachment[4]; 
  2292. $disposition = $attachment[6]; 
  2293. $cid = $attachment[7]; 
  2294. if ($disposition == 'inline' && array_key_exists($cid, $cidUniq)) { 
  2295. continue; 
  2296. $cidUniq[$cid] = true; 
  2297.  
  2298. $mime[] = sprintf('--%s%s', $boundary, $this->LE); 
  2299. //Only include a filename property if we have one 
  2300. if (!empty($name)) { 
  2301. $mime[] = sprintf( 
  2302. 'Content-Type: %s; name="%s"%s',  
  2303. $type,  
  2304. $this->encodeHeader($this->secureHeader($name)),  
  2305. $this->LE 
  2306. ); 
  2307. } else { 
  2308. $mime[] = sprintf( 
  2309. 'Content-Type: %s%s',  
  2310. $type,  
  2311. $this->LE 
  2312. ); 
  2313. // RFC1341 part 5 says 7bit is assumed if not specified 
  2314. if ($encoding != '7bit') { 
  2315. $mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, $this->LE); 
  2316.  
  2317. if ($disposition == 'inline') { 
  2318. $mime[] = sprintf('Content-ID: <%s>%s', $cid, $this->LE); 
  2319.  
  2320. // If a filename contains any of these chars, it should be quoted,  
  2321. // but not otherwise: RFC2183 & RFC2045 5.1 
  2322. // Fixes a warning in IETF's msglint MIME checker 
  2323. // Allow for bypassing the Content-Disposition header totally 
  2324. if (!(empty($disposition))) { 
  2325. $encoded_name = $this->encodeHeader($this->secureHeader($name)); 
  2326. if (preg_match('/[ \(\)<>@, ;:\\"\/\[\]\?=]/', $encoded_name)) { 
  2327. $mime[] = sprintf( 
  2328. 'Content-Disposition: %s; filename="%s"%s',  
  2329. $disposition,  
  2330. $encoded_name,  
  2331. $this->LE . $this->LE 
  2332. ); 
  2333. } else { 
  2334. if (!empty($encoded_name)) { 
  2335. $mime[] = sprintf( 
  2336. 'Content-Disposition: %s; filename=%s%s',  
  2337. $disposition,  
  2338. $encoded_name,  
  2339. $this->LE . $this->LE 
  2340. ); 
  2341. } else { 
  2342. $mime[] = sprintf( 
  2343. 'Content-Disposition: %s%s',  
  2344. $disposition,  
  2345. $this->LE . $this->LE 
  2346. ); 
  2347. } else { 
  2348. $mime[] = $this->LE; 
  2349.  
  2350. // Encode as string attachment 
  2351. if ($bString) { 
  2352. $mime[] = $this->encodeString($string, $encoding); 
  2353. if ($this->isError()) { 
  2354. return ''; 
  2355. $mime[] = $this->LE . $this->LE; 
  2356. } else { 
  2357. $mime[] = $this->encodeFile($path, $encoding); 
  2358. if ($this->isError()) { 
  2359. return ''; 
  2360. $mime[] = $this->LE . $this->LE; 
  2361.  
  2362. $mime[] = sprintf('--%s--%s', $boundary, $this->LE); 
  2363.  
  2364. return implode('', $mime); 
  2365.  
  2366. /** 
  2367. * Encode a file attachment in requested format. 
  2368. * Returns an empty string on failure. 
  2369. * @param string $path The full path to the file 
  2370. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' 
  2371. * @throws phpmailerException 
  2372. * @access protected 
  2373. * @return string 
  2374. */ 
  2375. protected function encodeFile($path, $encoding = 'base64') 
  2376. try { 
  2377. if (!is_readable($path)) { 
  2378. throw new phpmailerException($this->lang('file_open') . $path, self::STOP_CONTINUE); 
  2379. $magic_quotes = get_magic_quotes_runtime(); 
  2380. if ($magic_quotes) { 
  2381. if (version_compare(PHP_VERSION, '5.3.0', '<')) { 
  2382. set_magic_quotes_runtime(false); 
  2383. } else { 
  2384. //Doesn't exist in PHP 5.4, but we don't need to check because 
  2385. //get_magic_quotes_runtime always returns false in 5.4+ 
  2386. //so it will never get here 
  2387. ini_set('magic_quotes_runtime', false); 
  2388. $file_buffer = file_get_contents($path); 
  2389. $file_buffer = $this->encodeString($file_buffer, $encoding); 
  2390. if ($magic_quotes) { 
  2391. if (version_compare(PHP_VERSION, '5.3.0', '<')) { 
  2392. set_magic_quotes_runtime($magic_quotes); 
  2393. } else { 
  2394. ini_set('magic_quotes_runtime', $magic_quotes); 
  2395. return $file_buffer; 
  2396. } catch (Exception $exc) { 
  2397. $this->setError($exc->getMessage()); 
  2398. return ''; 
  2399.  
  2400. /** 
  2401. * Encode a string in requested format. 
  2402. * Returns an empty string on failure. 
  2403. * @param string $str The text to encode 
  2404. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' 
  2405. * @access public 
  2406. * @return string 
  2407. */ 
  2408. public function encodeString($str, $encoding = 'base64') 
  2409. $encoded = ''; 
  2410. switch (strtolower($encoding)) { 
  2411. case 'base64': 
  2412. $encoded = chunk_split(base64_encode($str), 76, $this->LE); 
  2413. break; 
  2414. case '7bit': 
  2415. case '8bit': 
  2416. $encoded = $this->fixEOL($str); 
  2417. // Make sure it ends with a line break 
  2418. if (substr($encoded, -(strlen($this->LE))) != $this->LE) { 
  2419. $encoded .= $this->LE; 
  2420. break; 
  2421. case 'binary': 
  2422. $encoded = $str; 
  2423. break; 
  2424. case 'quoted-printable': 
  2425. $encoded = $this->encodeQP($str); 
  2426. break; 
  2427. default: 
  2428. $this->setError($this->lang('encoding') . $encoding); 
  2429. break; 
  2430. return $encoded; 
  2431.  
  2432. /** 
  2433. * Encode a header string optimally. 
  2434. * Picks shortest of Q, B, quoted-printable or none. 
  2435. * @access public 
  2436. * @param string $str 
  2437. * @param string $position 
  2438. * @return string 
  2439. */ 
  2440. public function encodeHeader($str, $position = 'text') 
  2441. $matchcount = 0; 
  2442. switch (strtolower($position)) { 
  2443. case 'phrase': 
  2444. if (!preg_match('/[\200-\377]/', $str)) { 
  2445. // Can't use addslashes as we don't know the value of magic_quotes_sybase 
  2446. $encoded = addcslashes($str, "\0..\37\177\\\""); 
  2447. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) { 
  2448. return ($encoded); 
  2449. } else { 
  2450. return ("\"$encoded\""); 
  2451. $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); 
  2452. break; 
  2453. /** @noinspection PhpMissingBreakStatementInspection */ 
  2454. case 'comment': 
  2455. $matchcount = preg_match_all('/[()"]/', $str, $matches); 
  2456. // Intentional fall-through 
  2457. case 'text': 
  2458. default: 
  2459. $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); 
  2460. break; 
  2461.  
  2462. //There are no chars that need encoding 
  2463. if ($matchcount == 0) { 
  2464. return ($str); 
  2465.  
  2466. $maxlen = 75 - 7 - strlen($this->CharSet); 
  2467. // Try to select the encoding which should produce the shortest output 
  2468. if ($matchcount > strlen($str) / 3) { 
  2469. // More than a third of the content will need encoding, so B encoding will be most efficient 
  2470. $encoding = 'B'; 
  2471. if (function_exists('mb_strlen') && $this->hasMultiBytes($str)) { 
  2472. // Use a custom function which correctly encodes and wraps long 
  2473. // multibyte strings without breaking lines within a character 
  2474. $encoded = $this->base64EncodeWrapMB($str, "\n"); 
  2475. } else { 
  2476. $encoded = base64_encode($str); 
  2477. $maxlen -= $maxlen % 4; 
  2478. $encoded = trim(chunk_split($encoded, $maxlen, "\n")); 
  2479. } else { 
  2480. $encoding = 'Q'; 
  2481. $encoded = $this->encodeQ($str, $position); 
  2482. $encoded = $this->wrapText($encoded, $maxlen, true); 
  2483. $encoded = str_replace('=' . self::CRLF, "\n", trim($encoded)); 
  2484.  
  2485. $encoded = preg_replace('/^(.*)$/m', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded); 
  2486. $encoded = trim(str_replace("\n", $this->LE, $encoded)); 
  2487.  
  2488. return $encoded; 
  2489.  
  2490. /** 
  2491. * Check if a string contains multi-byte characters. 
  2492. * @access public 
  2493. * @param string $str multi-byte text to wrap encode 
  2494. * @return boolean 
  2495. */ 
  2496. public function hasMultiBytes($str) 
  2497. if (function_exists('mb_strlen')) { 
  2498. return (strlen($str) > mb_strlen($str, $this->CharSet)); 
  2499. } else { // Assume no multibytes (we can't handle without mbstring functions anyway) 
  2500. return false; 
  2501.  
  2502. /** 
  2503. * Does a string contain any 8-bit chars (in any charset)? 
  2504. * @param string $text 
  2505. * @return boolean 
  2506. */ 
  2507. public function has8bitChars($text) 
  2508. return (boolean)preg_match('/[\x80-\xFF]/', $text); 
  2509.  
  2510. /** 
  2511. * Encode and wrap long multibyte strings for mail headers 
  2512. * without breaking lines within a character. 
  2513. * Adapted from a function by paravoid 
  2514. * @link http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283 
  2515. * @access public 
  2516. * @param string $str multi-byte text to wrap encode 
  2517. * @param string $linebreak string to use as linefeed/end-of-line 
  2518. * @return string 
  2519. */ 
  2520. public function base64EncodeWrapMB($str, $linebreak = null) 
  2521. $start = '=?' . $this->CharSet . '?B?'; 
  2522. $end = '?='; 
  2523. $encoded = ''; 
  2524. if ($linebreak === null) { 
  2525. $linebreak = $this->LE; 
  2526.  
  2527. $mb_length = mb_strlen($str, $this->CharSet); 
  2528. // Each line must have length <= 75, including $start and $end 
  2529. $length = 75 - strlen($start) - strlen($end); 
  2530. // Average multi-byte ratio 
  2531. $ratio = $mb_length / strlen($str); 
  2532. // Base64 has a 4:3 ratio 
  2533. $avgLength = floor($length * $ratio * .75); 
  2534.  
  2535. for ($i = 0; $i < $mb_length; $i += $offset) { 
  2536. $lookBack = 0; 
  2537. do { 
  2538. $offset = $avgLength - $lookBack; 
  2539. $chunk = mb_substr($str, $i, $offset, $this->CharSet); 
  2540. $chunk = base64_encode($chunk); 
  2541. $lookBack++; 
  2542. } while (strlen($chunk) > $length); 
  2543. $encoded .= $chunk . $linebreak; 
  2544.  
  2545. // Chomp the last linefeed 
  2546. $encoded = substr($encoded, 0, -strlen($linebreak)); 
  2547. return $encoded; 
  2548.  
  2549. /** 
  2550. * Encode a string in quoted-printable format. 
  2551. * According to RFC2045 section 6.7. 
  2552. * @access public 
  2553. * @param string $string The text to encode 
  2554. * @param integer $line_max Number of chars allowed on a line before wrapping 
  2555. * @return string 
  2556. * @link http://www.php.net/manual/en/function.quoted-printable-decode.php#89417 Adapted from this comment 
  2557. */ 
  2558. public function encodeQP($string, $line_max = 76) 
  2559. // Use native function if it's available (>= PHP5.3) 
  2560. if (function_exists('quoted_printable_encode')) { 
  2561. return quoted_printable_encode($string); 
  2562. // Fall back to a pure PHP implementation 
  2563. $string = str_replace( 
  2564. array('%20', '%0D%0A.', '%0D%0A', '%'),  
  2565. array(' ', "\r\n=2E", "\r\n", '='),  
  2566. rawurlencode($string) 
  2567. ); 
  2568. return preg_replace('/[^\r\n]{' . ($line_max - 3) . '}[^=\r\n]{2}/', "$0=\r\n", $string); 
  2569.  
  2570. /** 
  2571. * Backward compatibility wrapper for an old QP encoding function that was removed. 
  2572. * @see PHPMailer::encodeQP() 
  2573. * @access public 
  2574. * @param string $string 
  2575. * @param integer $line_max 
  2576. * @param boolean $space_conv 
  2577. * @return string 
  2578. * @deprecated Use encodeQP instead. 
  2579. */ 
  2580. public function encodeQPphp( 
  2581. $string,  
  2582. $line_max = 76,  
  2583. /** @noinspection PhpUnusedParameterInspection */ $space_conv = false 
  2584. ) { 
  2585. return $this->encodeQP($string, $line_max); 
  2586.  
  2587. /** 
  2588. * Encode a string using Q encoding. 
  2589. * @link http://tools.ietf.org/html/rfc2047 
  2590. * @param string $str the text to encode 
  2591. * @param string $position Where the text is going to be used, see the RFC for what that means 
  2592. * @access public 
  2593. * @return string 
  2594. */ 
  2595. public function encodeQ($str, $position = 'text') 
  2596. // There should not be any EOL in the string 
  2597. $pattern = ''; 
  2598. $encoded = str_replace(array("\r", "\n"), '', $str); 
  2599. switch (strtolower($position)) { 
  2600. case 'phrase': 
  2601. // RFC 2047 section 5.3 
  2602. $pattern = '^A-Za-z0-9!*+\/ -'; 
  2603. break; 
  2604. /** @noinspection PhpMissingBreakStatementInspection */ 
  2605. case 'comment': 
  2606. // RFC 2047 section 5.2 
  2607. $pattern = '\(\)"'; 
  2608. // intentional fall-through 
  2609. // for this reason we build the $pattern without including delimiters and [] 
  2610. case 'text': 
  2611. default: 
  2612. // RFC 2047 section 5.1 
  2613. // Replace every high ascii, control, =, ? and _ characters 
  2614. $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern; 
  2615. break; 
  2616. $matches = array(); 
  2617. if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) { 
  2618. // If the string contains an '=', make sure it's the first thing we replace 
  2619. // so as to avoid double-encoding 
  2620. $eqkey = array_search('=', $matches[0]); 
  2621. if (false !== $eqkey) { 
  2622. unset($matches[0][$eqkey]); 
  2623. array_unshift($matches[0], '='); 
  2624. foreach (array_unique($matches[0]) as $char) { 
  2625. $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded); 
  2626. // Replace every spaces to _ (more readable than =20) 
  2627. return str_replace(' ', '_', $encoded); 
  2628.  
  2629. /** 
  2630. * Add a string or binary attachment (non-filesystem). 
  2631. * This method can be used to attach ascii or binary data,  
  2632. * such as a BLOB record from a database. 
  2633. * @param string $string String attachment data. 
  2634. * @param string $filename Name of the attachment. 
  2635. * @param string $encoding File encoding (see $Encoding). 
  2636. * @param string $type File extension (MIME) type. 
  2637. * @param string $disposition Disposition to use 
  2638. * @return void 
  2639. */ 
  2640. public function addStringAttachment( 
  2641. $string,  
  2642. $filename,  
  2643. $encoding = 'base64',  
  2644. $type = '',  
  2645. $disposition = 'attachment' 
  2646. ) { 
  2647. // If a MIME type is not specified, try to work it out from the file name 
  2648. if ($type == '') { 
  2649. $type = self::filenameToType($filename); 
  2650. // Append to $attachment array 
  2651. $this->attachment[] = array( 
  2652. 0 => $string,  
  2653. 1 => $filename,  
  2654. 2 => basename($filename),  
  2655. 3 => $encoding,  
  2656. 4 => $type,  
  2657. 5 => true, // isStringAttachment 
  2658. 6 => $disposition,  
  2659. 7 => 0 
  2660. ); 
  2661.  
  2662. /** 
  2663. * Add an embedded (inline) attachment from a file. 
  2664. * This can include images, sounds, and just about any other document type. 
  2665. * These differ from 'regular' attachments in that they are intended to be 
  2666. * displayed inline with the message, not just attached for download. 
  2667. * This is used in HTML messages that embed the images 
  2668. * the HTML refers to using the $cid value. 
  2669. * Never use a user-supplied path to a file! 
  2670. * @param string $path Path to the attachment. 
  2671. * @param string $cid Content ID of the attachment; Use this to reference 
  2672. * the content when using an embedded image in HTML. 
  2673. * @param string $name Overrides the attachment name. 
  2674. * @param string $encoding File encoding (see $Encoding). 
  2675. * @param string $type File MIME type. 
  2676. * @param string $disposition Disposition to use 
  2677. * @return boolean True on successfully adding an attachment 
  2678. */ 
  2679. public function addEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = '', $disposition = 'inline') 
  2680. if (!@is_file($path)) { 
  2681. $this->setError($this->lang('file_access') . $path); 
  2682. return false; 
  2683.  
  2684. // If a MIME type is not specified, try to work it out from the file name 
  2685. if ($type == '') { 
  2686. $type = self::filenameToType($path); 
  2687.  
  2688. $filename = basename($path); 
  2689. if ($name == '') { 
  2690. $name = $filename; 
  2691.  
  2692. // Append to $attachment array 
  2693. $this->attachment[] = array( 
  2694. 0 => $path,  
  2695. 1 => $filename,  
  2696. 2 => $name,  
  2697. 3 => $encoding,  
  2698. 4 => $type,  
  2699. 5 => false, // isStringAttachment 
  2700. 6 => $disposition,  
  2701. 7 => $cid 
  2702. ); 
  2703. return true; 
  2704.  
  2705. /** 
  2706. * Add an embedded stringified attachment. 
  2707. * This can include images, sounds, and just about any other document type. 
  2708. * Be sure to set the $type to an image type for images: 
  2709. * JPEG images use 'image/jpeg', GIF uses 'image/gif', PNG uses 'image/png'. 
  2710. * @param string $string The attachment binary data. 
  2711. * @param string $cid Content ID of the attachment; Use this to reference 
  2712. * the content when using an embedded image in HTML. 
  2713. * @param string $name 
  2714. * @param string $encoding File encoding (see $Encoding). 
  2715. * @param string $type MIME type. 
  2716. * @param string $disposition Disposition to use 
  2717. * @return boolean True on successfully adding an attachment 
  2718. */ 
  2719. public function addStringEmbeddedImage( 
  2720. $string,  
  2721. $cid,  
  2722. $name = '',  
  2723. $encoding = 'base64',  
  2724. $type = '',  
  2725. $disposition = 'inline' 
  2726. ) { 
  2727. // If a MIME type is not specified, try to work it out from the name 
  2728. if ($type == '' and !empty($name)) { 
  2729. $type = self::filenameToType($name); 
  2730.  
  2731. // Append to $attachment array 
  2732. $this->attachment[] = array( 
  2733. 0 => $string,  
  2734. 1 => $name,  
  2735. 2 => $name,  
  2736. 3 => $encoding,  
  2737. 4 => $type,  
  2738. 5 => true, // isStringAttachment 
  2739. 6 => $disposition,  
  2740. 7 => $cid 
  2741. ); 
  2742. return true; 
  2743.  
  2744. /** 
  2745. * Check if an inline attachment is present. 
  2746. * @access public 
  2747. * @return boolean 
  2748. */ 
  2749. public function inlineImageExists() 
  2750. foreach ($this->attachment as $attachment) { 
  2751. if ($attachment[6] == 'inline') { 
  2752. return true; 
  2753. return false; 
  2754.  
  2755. /** 
  2756. * Check if an attachment (non-inline) is present. 
  2757. * @return boolean 
  2758. */ 
  2759. public function attachmentExists() 
  2760. foreach ($this->attachment as $attachment) { 
  2761. if ($attachment[6] == 'attachment') { 
  2762. return true; 
  2763. return false; 
  2764.  
  2765. /** 
  2766. * Check if this message has an alternative body set. 
  2767. * @return boolean 
  2768. */ 
  2769. public function alternativeExists() 
  2770. return !empty($this->AltBody); 
  2771.  
  2772. /** 
  2773. * Clear queued addresses of given kind. 
  2774. * @access protected 
  2775. * @param string $kind 'to', 'cc', or 'bcc' 
  2776. * @return void 
  2777. */ 
  2778. public function clearQueuedAddresses($kind) 
  2779. $RecipientsQueue = $this->RecipientsQueue; 
  2780. foreach ($RecipientsQueue as $address => $params) { 
  2781. if ($params[0] == $kind) { 
  2782. unset($this->RecipientsQueue[$address]); 
  2783.  
  2784. /** 
  2785. * Clear all To recipients. 
  2786. * @return void 
  2787. */ 
  2788. public function clearAddresses() 
  2789. foreach ($this->to as $to) { 
  2790. unset($this->all_recipients[strtolower($to[0])]); 
  2791. $this->to = array(); 
  2792. $this->clearQueuedAddresses('to'); 
  2793.  
  2794. /** 
  2795. * Clear all CC recipients. 
  2796. * @return void 
  2797. */ 
  2798. public function clearCCs() 
  2799. foreach ($this->cc as $cc) { 
  2800. unset($this->all_recipients[strtolower($cc[0])]); 
  2801. $this->cc = array(); 
  2802. $this->clearQueuedAddresses('cc'); 
  2803.  
  2804. /** 
  2805. * Clear all BCC recipients. 
  2806. * @return void 
  2807. */ 
  2808. public function clearBCCs() 
  2809. foreach ($this->bcc as $bcc) { 
  2810. unset($this->all_recipients[strtolower($bcc[0])]); 
  2811. $this->bcc = array(); 
  2812. $this->clearQueuedAddresses('bcc'); 
  2813.  
  2814. /** 
  2815. * Clear all ReplyTo recipients. 
  2816. * @return void 
  2817. */ 
  2818. public function clearReplyTos() 
  2819. $this->ReplyTo = array(); 
  2820. $this->ReplyToQueue = array(); 
  2821.  
  2822. /** 
  2823. * Clear all recipient types. 
  2824. * @return void 
  2825. */ 
  2826. public function clearAllRecipients() 
  2827. $this->to = array(); 
  2828. $this->cc = array(); 
  2829. $this->bcc = array(); 
  2830. $this->all_recipients = array(); 
  2831. $this->RecipientsQueue = array(); 
  2832.  
  2833. /** 
  2834. * Clear all filesystem, string, and binary attachments. 
  2835. * @return void 
  2836. */ 
  2837. public function clearAttachments() 
  2838. $this->attachment = array(); 
  2839.  
  2840. /** 
  2841. * Clear all custom headers. 
  2842. * @return void 
  2843. */ 
  2844. public function clearCustomHeaders() 
  2845. $this->CustomHeader = array(); 
  2846.  
  2847. /** 
  2848. * Add an error message to the error container. 
  2849. * @access protected 
  2850. * @param string $msg 
  2851. * @return void 
  2852. */ 
  2853. protected function setError($msg) 
  2854. $this->error_count++; 
  2855. if ($this->Mailer == 'smtp' and !is_null($this->smtp)) { 
  2856. $lasterror = $this->smtp->getError(); 
  2857. if (!empty($lasterror['error'])) { 
  2858. $msg .= $this->lang('smtp_error') . $lasterror['error']; 
  2859. if (!empty($lasterror['detail'])) { 
  2860. $msg .= ' Detail: '. $lasterror['detail']; 
  2861. if (!empty($lasterror['smtp_code'])) { 
  2862. $msg .= ' SMTP code: ' . $lasterror['smtp_code']; 
  2863. if (!empty($lasterror['smtp_code_ex'])) { 
  2864. $msg .= ' Additional SMTP info: ' . $lasterror['smtp_code_ex']; 
  2865. $this->ErrorInfo = $msg; 
  2866.  
  2867. /** 
  2868. * Return an RFC 822 formatted date. 
  2869. * @access public 
  2870. * @return string 
  2871. * @static 
  2872. */ 
  2873. public static function rfcDate() 
  2874. // Set the time zone to whatever the default is to avoid 500 errors 
  2875. // Will default to UTC if it's not set properly in php.ini 
  2876. date_default_timezone_set(@date_default_timezone_get()); 
  2877. return date('D, j M Y H:i:s O'); 
  2878.  
  2879. /** 
  2880. * Get the server hostname. 
  2881. * Returns 'localhost.localdomain' if unknown. 
  2882. * @access protected 
  2883. * @return string 
  2884. */ 
  2885. protected function serverHostname() 
  2886. $result = 'localhost.localdomain'; 
  2887. if (!empty($this->Hostname)) { 
  2888. $result = $this->Hostname; 
  2889. } elseif (isset($_SERVER) and array_key_exists('SERVER_NAME', $_SERVER) and !empty($_SERVER['SERVER_NAME'])) { 
  2890. $result = $_SERVER['SERVER_NAME']; 
  2891. } elseif (function_exists('gethostname') && gethostname() !== false) { 
  2892. $result = gethostname(); 
  2893. } elseif (php_uname('n') !== false) { 
  2894. $result = php_uname('n'); 
  2895. return $result; 
  2896.  
  2897. /** 
  2898. * Get an error message in the current language. 
  2899. * @access protected 
  2900. * @param string $key 
  2901. * @return string 
  2902. */ 
  2903. protected function lang($key) 
  2904. if (count($this->language) < 1) { 
  2905. $this->setLanguage('en'); // set the default language 
  2906.  
  2907. if (array_key_exists($key, $this->language)) { 
  2908. if ($key == 'smtp_connect_failed') { 
  2909. //Include a link to troubleshooting docs on SMTP connection failure 
  2910. //this is by far the biggest cause of support questions 
  2911. //but it's usually not PHPMailer's fault. 
  2912. return $this->language[$key] . ' https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'; 
  2913. return $this->language[$key]; 
  2914. } else { 
  2915. //Return the key as a fallback 
  2916. return $key; 
  2917.  
  2918. /** 
  2919. * Check if an error occurred. 
  2920. * @access public 
  2921. * @return boolean True if an error did occur. 
  2922. */ 
  2923. public function isError() 
  2924. return ($this->error_count > 0); 
  2925.  
  2926. /** 
  2927. * Ensure consistent line endings in a string. 
  2928. * Changes every end of line from CRLF, CR or LF to $this->LE. 
  2929. * @access public 
  2930. * @param string $str String to fixEOL 
  2931. * @return string 
  2932. */ 
  2933. public function fixEOL($str) 
  2934. // Normalise to \n 
  2935. $nstr = str_replace(array("\r\n", "\r"), "\n", $str); 
  2936. // Now convert LE as needed 
  2937. if ($this->LE !== "\n") { 
  2938. $nstr = str_replace("\n", $this->LE, $nstr); 
  2939. return $nstr; 
  2940.  
  2941. /** 
  2942. * Add a custom header. 
  2943. * $name value can be overloaded to contain 
  2944. * both header name and value (name:value) 
  2945. * @access public 
  2946. * @param string $name Custom header name 
  2947. * @param string $value Header value 
  2948. * @return void 
  2949. */ 
  2950. public function addCustomHeader($name, $value = null) 
  2951. if ($value === null) { 
  2952. // Value passed in as name:value 
  2953. $this->CustomHeader[] = explode(':', $name, 2); 
  2954. } else { 
  2955. $this->CustomHeader[] = array($name, $value); 
  2956.  
  2957. /** 
  2958. * Returns all custom headers. 
  2959. * @return array 
  2960. */ 
  2961. public function getCustomHeaders() 
  2962. return $this->CustomHeader; 
  2963.  
  2964. /** 
  2965. * Create a message body from an HTML string. 
  2966. * Automatically inlines images and creates a plain-text version by converting the HTML,  
  2967. * overwriting any existing values in Body and AltBody. 
  2968. * Do not source $message content from user input! 
  2969. * $basedir is prepended when handling relative URLs, e.g. <img src="/images/a.png"> and must not be empty 
  2970. * will look for an image file in $basedir/images/a.png and convert it to inline. 
  2971. * If you don't provide a $basedir, relative paths will be left untouched (and thus probably break in email) 
  2972. * If you don't want to apply these transformations to your HTML, just set Body and AltBody directly. 
  2973. * @access public 
  2974. * @param string $message HTML message string 
  2975. * @param string $basedir Absolute path to a base directory to prepend to relative paths to images 
  2976. * @param boolean|callable $advanced Whether to use the internal HTML to text converter 
  2977. * or your own custom converter @see PHPMailer::html2text() 
  2978. * @return string $message The transformed message Body 
  2979. */ 
  2980. public function msgHTML($message, $basedir = '', $advanced = false) 
  2981. preg_match_all('/(src|background)=["\'](.*)["\']/Ui', $message, $images); 
  2982. if (array_key_exists(2, $images)) { 
  2983. if (strlen($basedir) > 1 && substr($basedir, -1) != '/') { 
  2984. // Ensure $basedir has a trailing / 
  2985. $basedir .= '/'; 
  2986. foreach ($images[2] as $imgindex => $url) { 
  2987. // Convert data URIs into embedded images 
  2988. if (preg_match('#^data:(image[^;, ]*)(;base64)?, #', $url, $match)) { 
  2989. $data = substr($url, strpos($url, ', ')); 
  2990. if ($match[2]) { 
  2991. $data = base64_decode($data); 
  2992. } else { 
  2993. $data = rawurldecode($data); 
  2994. $cid = md5($url) . '@phpmailer.0'; // RFC2392 S 2 
  2995. if ($this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, 'base64', $match[1])) { 
  2996. $message = str_replace( 
  2997. $images[0][$imgindex],  
  2998. $images[1][$imgindex] . '="cid:' . $cid . '"',  
  2999. $message 
  3000. ); 
  3001. continue; 
  3002. if ( 
  3003. // Only process relative URLs if a basedir is provided (i.e. no absolute local paths) 
  3004. !empty($basedir) 
  3005. // Ignore URLs containing parent dir traversal (..) 
  3006. && (strpos($url, '..') === false) 
  3007. // Do not change urls that are already inline images 
  3008. && substr($url, 0, 4) !== 'cid:' 
  3009. // Do not change absolute URLs, including anonymous protocol 
  3010. && !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url) 
  3011. ) { 
  3012. $filename = basename($url); 
  3013. $directory = dirname($url); 
  3014. if ($directory == '.') { 
  3015. $directory = ''; 
  3016. $cid = md5($url) . '@phpmailer.0'; // RFC2392 S 2 
  3017. if (strlen($directory) > 1 && substr($directory, -1) != '/') { 
  3018. $directory .= '/'; 
  3019. if ($this->addEmbeddedImage( 
  3020. $basedir . $directory . $filename,  
  3021. $cid,  
  3022. $filename,  
  3023. 'base64',  
  3024. self::_mime_types((string)self::mb_pathinfo($filename, PATHINFO_EXTENSION)) 
  3025. ) { 
  3026. $message = preg_replace( 
  3027. '/' . $images[1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui',  
  3028. $images[1][$imgindex] . '="cid:' . $cid . '"',  
  3029. $message 
  3030. ); 
  3031. $this->isHTML(true); 
  3032. // Convert all message body line breaks to CRLF, makes quoted-printable encoding work much better 
  3033. $this->Body = $this->normalizeBreaks($message); 
  3034. $this->AltBody = $this->normalizeBreaks($this->html2text($message, $advanced)); 
  3035. if (!$this->alternativeExists()) { 
  3036. $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . 
  3037. self::CRLF . self::CRLF; 
  3038. return $this->Body; 
  3039.  
  3040. /** 
  3041. * Convert an HTML string into plain text. 
  3042. * This is used by msgHTML(). 
  3043. * Note - older versions of this function used a bundled advanced converter 
  3044. * which was been removed for license reasons in #232. 
  3045. * Example usage: 
  3046. * <code> 
  3047. * // Use default conversion 
  3048. * $plain = $mail->html2text($html); 
  3049. * // Use your own custom converter 
  3050. * $plain = $mail->html2text($html, function($html) { 
  3051. * $converter = new MyHtml2text($html); 
  3052. * return $converter->get_text(); 
  3053. * }); 
  3054. * </code> 
  3055. * @param string $html The HTML text to convert 
  3056. * @param boolean|callable $advanced Any boolean value to use the internal converter,  
  3057. * or provide your own callable for custom conversion. 
  3058. * @return string 
  3059. */ 
  3060. public function html2text($html, $advanced = false) 
  3061. if (is_callable($advanced)) { 
  3062. return call_user_func($advanced, $html); 
  3063. return html_entity_decode( 
  3064. trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))),  
  3065. ENT_QUOTES,  
  3066. $this->CharSet 
  3067. ); 
  3068.  
  3069. /** 
  3070. * Get the MIME type for a file extension. 
  3071. * @param string $ext File extension 
  3072. * @access public 
  3073. * @return string MIME type of file. 
  3074. * @static 
  3075. */ 
  3076. public static function _mime_types($ext = '') 
  3077. $mimes = array( 
  3078. 'xl' => 'application/excel',  
  3079. 'js' => 'application/javascript',  
  3080. 'hqx' => 'application/mac-binhex40',  
  3081. 'cpt' => 'application/mac-compactpro',  
  3082. 'bin' => 'application/macbinary',  
  3083. 'doc' => 'application/msword',  
  3084. 'word' => 'application/msword',  
  3085. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',  
  3086. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',  
  3087. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',  
  3088. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',  
  3089. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',  
  3090. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',  
  3091. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',  
  3092. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',  
  3093. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',  
  3094. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',  
  3095. 'class' => 'application/octet-stream',  
  3096. 'dll' => 'application/octet-stream',  
  3097. 'dms' => 'application/octet-stream',  
  3098. 'exe' => 'application/octet-stream',  
  3099. 'lha' => 'application/octet-stream',  
  3100. 'lzh' => 'application/octet-stream',  
  3101. 'psd' => 'application/octet-stream',  
  3102. 'sea' => 'application/octet-stream',  
  3103. 'so' => 'application/octet-stream',  
  3104. 'oda' => 'application/oda',  
  3105. 'pdf' => 'application/pdf',  
  3106. 'ai' => 'application/postscript',  
  3107. 'eps' => 'application/postscript',  
  3108. 'ps' => 'application/postscript',  
  3109. 'smi' => 'application/smil',  
  3110. 'smil' => 'application/smil',  
  3111. 'mif' => 'application/vnd.mif',  
  3112. 'xls' => 'application/vnd.ms-excel',  
  3113. 'ppt' => 'application/vnd.ms-powerpoint',  
  3114. 'wbxml' => 'application/vnd.wap.wbxml',  
  3115. 'wmlc' => 'application/vnd.wap.wmlc',  
  3116. 'dcr' => 'application/x-director',  
  3117. 'dir' => 'application/x-director',  
  3118. 'dxr' => 'application/x-director',  
  3119. 'dvi' => 'application/x-dvi',  
  3120. 'gtar' => 'application/x-gtar',  
  3121. 'php3' => 'application/x-httpd-php',  
  3122. 'php4' => 'application/x-httpd-php',  
  3123. 'php' => 'application/x-httpd-php',  
  3124. 'phtml' => 'application/x-httpd-php',  
  3125. 'phps' => 'application/x-httpd-php-source',  
  3126. 'swf' => 'application/x-shockwave-flash',  
  3127. 'sit' => 'application/x-stuffit',  
  3128. 'tar' => 'application/x-tar',  
  3129. 'tgz' => 'application/x-tar',  
  3130. 'xht' => 'application/xhtml+xml',  
  3131. 'xhtml' => 'application/xhtml+xml',  
  3132. 'zip' => 'application/zip',  
  3133. 'mid' => 'audio/midi',  
  3134. 'midi' => 'audio/midi',  
  3135. 'mp2' => 'audio/mpeg',  
  3136. 'mp3' => 'audio/mpeg',  
  3137. 'mpga' => 'audio/mpeg',  
  3138. 'aif' => 'audio/x-aiff',  
  3139. 'aifc' => 'audio/x-aiff',  
  3140. 'aiff' => 'audio/x-aiff',  
  3141. 'ram' => 'audio/x-pn-realaudio',  
  3142. 'rm' => 'audio/x-pn-realaudio',  
  3143. 'rpm' => 'audio/x-pn-realaudio-plugin',  
  3144. 'ra' => 'audio/x-realaudio',  
  3145. 'wav' => 'audio/x-wav',  
  3146. 'bmp' => 'image/bmp',  
  3147. 'gif' => 'image/gif',  
  3148. 'jpeg' => 'image/jpeg',  
  3149. 'jpe' => 'image/jpeg',  
  3150. 'jpg' => 'image/jpeg',  
  3151. 'png' => 'image/png',  
  3152. 'tiff' => 'image/tiff',  
  3153. 'tif' => 'image/tiff',  
  3154. 'eml' => 'message/rfc822',  
  3155. 'css' => 'text/css',  
  3156. 'html' => 'text/html',  
  3157. 'htm' => 'text/html',  
  3158. 'shtml' => 'text/html',  
  3159. 'log' => 'text/plain',  
  3160. 'text' => 'text/plain',  
  3161. 'txt' => 'text/plain',  
  3162. 'rtx' => 'text/richtext',  
  3163. 'rtf' => 'text/rtf',  
  3164. 'vcf' => 'text/vcard',  
  3165. 'vcard' => 'text/vcard',  
  3166. 'xml' => 'text/xml',  
  3167. 'xsl' => 'text/xml',  
  3168. 'mpeg' => 'video/mpeg',  
  3169. 'mpe' => 'video/mpeg',  
  3170. 'mpg' => 'video/mpeg',  
  3171. 'mov' => 'video/quicktime',  
  3172. 'qt' => 'video/quicktime',  
  3173. 'rv' => 'video/vnd.rn-realvideo',  
  3174. 'avi' => 'video/x-msvideo',  
  3175. 'movie' => 'video/x-sgi-movie' 
  3176. ); 
  3177. if (array_key_exists(strtolower($ext), $mimes)) { 
  3178. return $mimes[strtolower($ext)]; 
  3179. return 'application/octet-stream'; 
  3180.  
  3181. /** 
  3182. * Map a file name to a MIME type. 
  3183. * Defaults to 'application/octet-stream', i.e.. arbitrary binary data. 
  3184. * @param string $filename A file name or full path, does not need to exist as a file 
  3185. * @return string 
  3186. * @static 
  3187. */ 
  3188. public static function filenameToType($filename) 
  3189. // In case the path is a URL, strip any query string before getting extension 
  3190. $qpos = strpos($filename, '?'); 
  3191. if (false !== $qpos) { 
  3192. $filename = substr($filename, 0, $qpos); 
  3193. $pathinfo = self::mb_pathinfo($filename); 
  3194. return self::_mime_types($pathinfo['extension']); 
  3195.  
  3196. /** 
  3197. * Multi-byte-safe pathinfo replacement. 
  3198. * Drop-in replacement for pathinfo(), but multibyte-safe, cross-platform-safe, old-version-safe. 
  3199. * Works similarly to the one in PHP >= 5.2.0 
  3200. * @link http://www.php.net/manual/en/function.pathinfo.php#107461 
  3201. * @param string $path A filename or path, does not need to exist as a file 
  3202. * @param integer|string $options Either a PATHINFO_* constant,  
  3203. * or a string name to return only the specified piece, allows 'filename' to work on PHP < 5.2 
  3204. * @return string|array 
  3205. * @static 
  3206. */ 
  3207. public static function mb_pathinfo($path, $options = null) 
  3208. $ret = array('dirname' => '', 'basename' => '', 'extension' => '', 'filename' => ''); 
  3209. $pathinfo = array(); 
  3210. if (preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $pathinfo)) { 
  3211. if (array_key_exists(1, $pathinfo)) { 
  3212. $ret['dirname'] = $pathinfo[1]; 
  3213. if (array_key_exists(2, $pathinfo)) { 
  3214. $ret['basename'] = $pathinfo[2]; 
  3215. if (array_key_exists(5, $pathinfo)) { 
  3216. $ret['extension'] = $pathinfo[5]; 
  3217. if (array_key_exists(3, $pathinfo)) { 
  3218. $ret['filename'] = $pathinfo[3]; 
  3219. switch ($options) { 
  3220. case PATHINFO_DIRNAME: 
  3221. case 'dirname': 
  3222. return $ret['dirname']; 
  3223. case PATHINFO_BASENAME: 
  3224. case 'basename': 
  3225. return $ret['basename']; 
  3226. case PATHINFO_EXTENSION: 
  3227. case 'extension': 
  3228. return $ret['extension']; 
  3229. case PATHINFO_FILENAME: 
  3230. case 'filename': 
  3231. return $ret['filename']; 
  3232. default: 
  3233. return $ret; 
  3234.  
  3235. /** 
  3236. * Set or reset instance properties. 
  3237. * You should avoid this function - it's more verbose, less efficient, more error-prone and 
  3238. * harder to debug than setting properties directly. 
  3239. * Usage Example: 
  3240. * `$mail->set('SMTPSecure', 'tls');` 
  3241. * is the same as: 
  3242. * `$mail->SMTPSecure = 'tls';` 
  3243. * @access public 
  3244. * @param string $name The property name to set 
  3245. * @param mixed $value The value to set the property to 
  3246. * @return boolean 
  3247. * @TODO Should this not be using the __set() magic function? 
  3248. */ 
  3249. public function set($name, $value = '') 
  3250. if (property_exists($this, $name)) { 
  3251. $this->$name = $value; 
  3252. return true; 
  3253. } else { 
  3254. $this->setError($this->lang('variable_set') . $name); 
  3255. return false; 
  3256.  
  3257. /** 
  3258. * Strip newlines to prevent header injection. 
  3259. * @access public 
  3260. * @param string $str 
  3261. * @return string 
  3262. */ 
  3263. public function secureHeader($str) 
  3264. return trim(str_replace(array("\r", "\n"), '', $str)); 
  3265.  
  3266. /** 
  3267. * Normalize line breaks in a string. 
  3268. * Converts UNIX LF, Mac CR and Windows CRLF line breaks into a single line break format. 
  3269. * Defaults to CRLF (for message bodies) and preserves consecutive breaks. 
  3270. * @param string $text 
  3271. * @param string $breaktype What kind of line break to use, defaults to CRLF 
  3272. * @return string 
  3273. * @access public 
  3274. * @static 
  3275. */ 
  3276. public static function normalizeBreaks($text, $breaktype = "\r\n") 
  3277. return preg_replace('/(\r\n|\r|\n)/ms', $breaktype, $text); 
  3278.  
  3279. /** 
  3280. * Set the public and private key files and password for S/MIME signing. 
  3281. * @access public 
  3282. * @param string $cert_filename 
  3283. * @param string $key_filename 
  3284. * @param string $key_pass Password for private key 
  3285. * @param string $extracerts_filename Optional path to chain certificate 
  3286. */ 
  3287. public function sign($cert_filename, $key_filename, $key_pass, $extracerts_filename = '') 
  3288. $this->sign_cert_file = $cert_filename; 
  3289. $this->sign_key_file = $key_filename; 
  3290. $this->sign_key_pass = $key_pass; 
  3291. $this->sign_extracerts_file = $extracerts_filename; 
  3292.  
  3293. /** 
  3294. * Quoted-Printable-encode a DKIM header. 
  3295. * @access public 
  3296. * @param string $txt 
  3297. * @return string 
  3298. */ 
  3299. public function DKIM_QP($txt) 
  3300. $line = ''; 
  3301. for ($i = 0; $i < strlen($txt); $i++) { 
  3302. $ord = ord($txt[$i]); 
  3303. if (((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E))) { 
  3304. $line .= $txt[$i]; 
  3305. } else { 
  3306. $line .= '=' . sprintf('%02X', $ord); 
  3307. return $line; 
  3308.  
  3309. /** 
  3310. * Generate a DKIM signature. 
  3311. * @access public 
  3312. * @param string $signHeader 
  3313. * @throws phpmailerException 
  3314. * @return string The DKIM signature value 
  3315. */ 
  3316. public function DKIM_Sign($signHeader) 
  3317. if (!defined('PKCS7_TEXT')) { 
  3318. if ($this->exceptions) { 
  3319. throw new phpmailerException($this->lang('extension_missing') . 'openssl'); 
  3320. return ''; 
  3321. $privKeyStr = !empty($this->DKIM_private_string) ? $this->DKIM_private_string : file_get_contents($this->DKIM_private); 
  3322. if ('' != $this->DKIM_passphrase) { 
  3323. $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase); 
  3324. } else { 
  3325. $privKey = openssl_pkey_get_private($privKeyStr); 
  3326. //Workaround for missing digest algorithms in old PHP & OpenSSL versions 
  3327. //@link http://stackoverflow.com/a/11117338/333340 
  3328. if (version_compare(PHP_VERSION, '5.3.0') >= 0 and 
  3329. in_array('sha256WithRSAEncryption', openssl_get_md_methods(true))) { 
  3330. if (openssl_sign($signHeader, $signature, $privKey, 'sha256WithRSAEncryption')) { 
  3331. openssl_pkey_free($privKey); 
  3332. return base64_encode($signature); 
  3333. } else { 
  3334. $pinfo = openssl_pkey_get_details($privKey); 
  3335. $hash = hash('sha256', $signHeader); 
  3336. //'Magic' constant for SHA256 from RFC3447 
  3337. //@link https://tools.ietf.org/html/rfc3447#page-43 
  3338. $t = '3031300d060960864801650304020105000420' . $hash; 
  3339. $pslen = $pinfo['bits'] / 8 - (strlen($t) / 2 + 3); 
  3340. $eb = pack('H*', '0001' . str_repeat('FF', $pslen) . '00' . $t); 
  3341.  
  3342. if (openssl_private_encrypt($eb, $signature, $privKey, OPENSSL_NO_PADDING)) { 
  3343. openssl_pkey_free($privKey); 
  3344. return base64_encode($signature); 
  3345. openssl_pkey_free($privKey); 
  3346. return ''; 
  3347.  
  3348. /** 
  3349. * Generate a DKIM canonicalization header. 
  3350. * @access public 
  3351. * @param string $signHeader Header 
  3352. * @return string 
  3353. */ 
  3354. public function DKIM_HeaderC($signHeader) 
  3355. $signHeader = preg_replace('/\r\n\s+/', ' ', $signHeader); 
  3356. $lines = explode("\r\n", $signHeader); 
  3357. foreach ($lines as $key => $line) { 
  3358. list($heading, $value) = explode(':', $line, 2); 
  3359. $heading = strtolower($heading); 
  3360. $value = preg_replace('/\s{2, }/', ' ', $value); // Compress useless spaces 
  3361. $lines[$key] = $heading . ':' . trim($value); // Don't forget to remove WSP around the value 
  3362. $signHeader = implode("\r\n", $lines); 
  3363. return $signHeader; 
  3364.  
  3365. /** 
  3366. * Generate a DKIM canonicalization body. 
  3367. * @access public 
  3368. * @param string $body Message Body 
  3369. * @return string 
  3370. */ 
  3371. public function DKIM_BodyC($body) 
  3372. if ($body == '') { 
  3373. return "\r\n"; 
  3374. // stabilize line endings 
  3375. $body = str_replace("\r\n", "\n", $body); 
  3376. $body = str_replace("\n", "\r\n", $body); 
  3377. // END stabilize line endings 
  3378. while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") { 
  3379. $body = substr($body, 0, strlen($body) - 2); 
  3380. return $body; 
  3381.  
  3382. /** 
  3383. * Create the DKIM header and body in a new message header. 
  3384. * @access public 
  3385. * @param string $headers_line Header lines 
  3386. * @param string $subject Subject 
  3387. * @param string $body Body 
  3388. * @return string 
  3389. */ 
  3390. public function DKIM_Add($headers_line, $subject, $body) 
  3391. $DKIMsignatureType = 'rsa-sha256'; // Signature & hash algorithms 
  3392. $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body 
  3393. $DKIMquery = 'dns/txt'; // Query method 
  3394. $DKIMtime = time(); // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone) 
  3395. $subject_header = "Subject: $subject"; 
  3396. $headers = explode($this->LE, $headers_line); 
  3397. $from_header = ''; 
  3398. $to_header = ''; 
  3399. $date_header = ''; 
  3400. $current = ''; 
  3401. foreach ($headers as $header) { 
  3402. if (strpos($header, 'From:') === 0) { 
  3403. $from_header = $header; 
  3404. $current = 'from_header'; 
  3405. } elseif (strpos($header, 'To:') === 0) { 
  3406. $to_header = $header; 
  3407. $current = 'to_header'; 
  3408. } elseif (strpos($header, 'Date:') === 0) { 
  3409. $date_header = $header; 
  3410. $current = 'date_header'; 
  3411. } else { 
  3412. if (!empty($$current) && strpos($header, ' =?') === 0) { 
  3413. $$current .= $header; 
  3414. } else { 
  3415. $current = ''; 
  3416. $from = str_replace('|', '=7C', $this->DKIM_QP($from_header)); 
  3417. $to = str_replace('|', '=7C', $this->DKIM_QP($to_header)); 
  3418. $date = str_replace('|', '=7C', $this->DKIM_QP($date_header)); 
  3419. $subject = str_replace( 
  3420. '|',  
  3421. '=7C',  
  3422. $this->DKIM_QP($subject_header) 
  3423. ); // Copied header fields (dkim-quoted-printable) 
  3424. $body = $this->DKIM_BodyC($body); 
  3425. $DKIMlen = strlen($body); // Length of body 
  3426. $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body))); // Base64 of packed binary SHA-256 hash of body 
  3427. if ('' == $this->DKIM_identity) { 
  3428. $ident = ''; 
  3429. } else { 
  3430. $ident = ' i=' . $this->DKIM_identity . ';'; 
  3431. $dkimhdrs = 'DKIM-Signature: v=1; a=' . 
  3432. $DKIMsignatureType . '; q=' . 
  3433. $DKIMquery . '; l=' . 
  3434. $DKIMlen . '; s=' . 
  3435. $this->DKIM_selector . 
  3436. ";\r\n" . 
  3437. "\tt=" . $DKIMtime . '; c=' . $DKIMcanonicalization . ";\r\n" . 
  3438. "\th=From:To:Date:Subject;\r\n" . 
  3439. "\td=" . $this->DKIM_domain . ';' . $ident . "\r\n" . 
  3440. "\tz=$from\r\n" . 
  3441. "\t|$to\r\n" . 
  3442. "\t|$date\r\n" . 
  3443. "\t|$subject;\r\n" . 
  3444. "\tbh=" . $DKIMb64 . ";\r\n" . 
  3445. "\tb="; 
  3446. $toSign = $this->DKIM_HeaderC( 
  3447. $from_header . "\r\n" . 
  3448. $to_header . "\r\n" . 
  3449. $date_header . "\r\n" . 
  3450. $subject_header . "\r\n" . 
  3451. $dkimhdrs 
  3452. ); 
  3453. $signed = $this->DKIM_Sign($toSign); 
  3454. return $dkimhdrs . $signed . "\r\n"; 
  3455.  
  3456. /** 
  3457. * Detect if a string contains a line longer than the maximum line length allowed. 
  3458. * @param string $str 
  3459. * @return boolean 
  3460. * @static 
  3461. */ 
  3462. public static function hasLineLongerThanMax($str) 
  3463. //+2 to include CRLF line break for a 1000 total 
  3464. return (boolean)preg_match('/^(.{'.(self::MAX_LINE_LENGTH + 2).', })/m', $str); 
  3465.  
  3466. /** 
  3467. * Allows for public read access to 'to' property. 
  3468. * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. 
  3469. * @access public 
  3470. * @return array 
  3471. */ 
  3472. public function getToAddresses() 
  3473. return $this->to; 
  3474.  
  3475. /** 
  3476. * Allows for public read access to 'cc' property. 
  3477. * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. 
  3478. * @access public 
  3479. * @return array 
  3480. */ 
  3481. public function getCcAddresses() 
  3482. return $this->cc; 
  3483.  
  3484. /** 
  3485. * Allows for public read access to 'bcc' property. 
  3486. * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. 
  3487. * @access public 
  3488. * @return array 
  3489. */ 
  3490. public function getBccAddresses() 
  3491. return $this->bcc; 
  3492.  
  3493. /** 
  3494. * Allows for public read access to 'ReplyTo' property. 
  3495. * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. 
  3496. * @access public 
  3497. * @return array 
  3498. */ 
  3499. public function getReplyToAddresses() 
  3500. return $this->ReplyTo; 
  3501.  
  3502. /** 
  3503. * Allows for public read access to 'all_recipients' property. 
  3504. * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. 
  3505. * @access public 
  3506. * @return array 
  3507. */ 
  3508. public function getAllRecipientAddresses() 
  3509. return $this->all_recipients; 
  3510.  
  3511. /** 
  3512. * Perform a callback. 
  3513. * @param boolean $isSent 
  3514. * @param array $to 
  3515. * @param array $cc 
  3516. * @param array $bcc 
  3517. * @param string $subject 
  3518. * @param string $body 
  3519. * @param string $from 
  3520. */ 
  3521. protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from) 
  3522. if (!empty($this->action_function) && is_callable($this->action_function)) { 
  3523. $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from); 
  3524. call_user_func_array($this->action_function, $params); 
  3525.  
  3526. /** 
  3527. * PHPMailer exception handler 
  3528. * @package PHPMailer 
  3529. */ 
  3530. class phpmailerException extends Exception 
  3531. /** 
  3532. * Prettify error message output 
  3533. * @return string 
  3534. */ 
  3535. public function errorMessage() 
  3536. $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n"; 
  3537. return $errorMsg; 
.