PHPMailer

PHPMailer - PHP email creation and transport class.

Defined (1)

The class is defined in the following location(s).

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