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