PHPMailer.class.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  1. <?php
  2. namespace KIF\Mail;
  3. use KIF\Mail\SMTP;
  4. use Exception;
  5. /*~ class.phpmailer.php
  6. .---------------------------------------------------------------------------.
  7. | Software: PHPMailer - PHP email class |
  8. | Version: 5.2 |
  9. | Site: https://code.google.com/a/apache-extras.org/p/phpmailer/ |
  10. | ------------------------------------------------------------------------- |
  11. | Admin: Jim Jagielski (project admininistrator) |
  12. | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  13. | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net |
  14. | : Jim Jagielski (jimjag) jimjag@gmail.com |
  15. | Founder: Brent R. Matzelle (original founder) |
  16. | Copyright (c) 2010-2011, Jim Jagielski. All Rights Reserved. |
  17. | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
  18. | Copyright (c) 2001-2003, Brent R. Matzelle |
  19. | ------------------------------------------------------------------------- |
  20. | License: Distributed under the Lesser General Public License (LGPL) |
  21. | http://www.gnu.org/copyleft/lesser.html |
  22. | This program is distributed in the hope that it will be useful - WITHOUT |
  23. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  24. | FITNESS FOR A PARTICULAR PURPOSE. |
  25. '---------------------------------------------------------------------------'
  26. */
  27. /**
  28. * PHPMailer - PHP email transport class
  29. * NOTE: Requires PHP version 5 or later
  30. * @package PHPMailer
  31. * @author Andy Prevost
  32. * @author Marcus Bointon
  33. * @author Jim Jagielski
  34. * @copyright 2010 - 2011 Jim Jagielski
  35. * @copyright 2004 - 2009 Andy Prevost
  36. * @version $Id: class.phpmailer.php 450 2010-06-23 16:46:33Z coolbru $
  37. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  38. */
  39. if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!\n");
  40. class PHPMailer {
  41. /////////////////////////////////////////////////
  42. // PROPERTIES, PUBLIC
  43. /////////////////////////////////////////////////
  44. /**
  45. * Email priority (1 = High, 3 = Normal, 5 = low).
  46. * @var int
  47. */
  48. public $Priority = 3;
  49. /**
  50. * Sets the CharSet of the message.
  51. * @var string
  52. */
  53. public $CharSet = 'iso-8859-1';
  54. /**
  55. * Sets the Content-type of the message.
  56. * @var string
  57. */
  58. public $ContentType = 'text/plain';
  59. /**
  60. * Sets the Encoding of the message. Options for this are
  61. * "8bit", "7bit", "binary", "base64", and "quoted-printable".
  62. * @var string
  63. */
  64. public $Encoding = '8bit';
  65. /**
  66. * Holds the most recent mailer error message.
  67. * @var string
  68. */
  69. public $ErrorInfo = '';
  70. /**
  71. * Sets the From email address for the message.
  72. * @var string
  73. */
  74. public $From = 'root@localhost';
  75. /**
  76. * Sets the From name of the message.
  77. * @var string
  78. */
  79. public $FromName = 'Root User';
  80. /**
  81. * Sets the Sender email (Return-Path) of the message. If not empty,
  82. * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  83. * @var string
  84. */
  85. public $Sender = '';
  86. /**
  87. * Sets the Subject of the message.
  88. * @var string
  89. */
  90. public $Subject = '';
  91. /**
  92. * Sets the Body of the message. This can be either an HTML or text body.
  93. * If HTML then run IsHTML(true).
  94. * @var string
  95. */
  96. public $Body = '';
  97. /**
  98. * Sets the text-only body of the message. This automatically sets the
  99. * email to multipart/alternative. This body can be read by mail
  100. * clients that do not have HTML email capability such as mutt. Clients
  101. * that can read HTML will view the normal Body.
  102. * @var string
  103. */
  104. public $AltBody = '';
  105. /**
  106. * Stores the complete compiled MIME message body.
  107. * @var string
  108. * @access protected
  109. */
  110. protected $MIMEBody = '';
  111. /**
  112. * Stores the complete compiled MIME message headers.
  113. * @var string
  114. * @access protected
  115. */
  116. protected $MIMEHeader = '';
  117. /**
  118. * Sets word wrapping on the body of the message to a given number of
  119. * characters.
  120. * @var int
  121. */
  122. public $WordWrap = 0;
  123. /**
  124. * Method to send mail: ("mail", "sendmail", or "smtp").
  125. * @var string
  126. */
  127. public $Mailer = 'mail';
  128. /**
  129. * Sets the path of the sendmail program.
  130. * @var string
  131. */
  132. public $Sendmail = '/usr/sbin/sendmail';
  133. /**
  134. * Path to PHPMailer plugins. Useful if the SMTP class
  135. * is in a different directory than the PHP include path.
  136. * @var string
  137. */
  138. public $PluginDir = '';
  139. /**
  140. * Sets the email address that a reading confirmation will be sent.
  141. * @var string
  142. */
  143. public $ConfirmReadingTo = '';
  144. /**
  145. * Sets the hostname to use in Message-Id and Received headers
  146. * and as default HELO string. If empty, the value returned
  147. * by SERVER_NAME is used or 'localhost.localdomain'.
  148. * @var string
  149. */
  150. public $Hostname = '';
  151. /**
  152. * Sets the message ID to be used in the Message-Id header.
  153. * If empty, a unique id will be generated.
  154. * @var string
  155. */
  156. public $MessageID = '';
  157. /////////////////////////////////////////////////
  158. // PROPERTIES FOR SMTP
  159. /////////////////////////////////////////////////
  160. /**
  161. * Sets the SMTP hosts. All hosts must be separated by a
  162. * semicolon. You can also specify a different port
  163. * for each host by using this format: [hostname:port]
  164. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  165. * Hosts will be tried in order.
  166. * @var string
  167. */
  168. public $Host = 'localhost';
  169. /**
  170. * Sets the default SMTP server port.
  171. * @var int
  172. */
  173. public $Port = 25;
  174. /**
  175. * Sets the SMTP HELO of the message (Default is $Hostname).
  176. * @var string
  177. */
  178. public $Helo = '';
  179. /**
  180. * Sets connection prefix.
  181. * Options are "", "ssl" or "tls"
  182. * @var string
  183. */
  184. public $SMTPSecure = '';
  185. /**
  186. * Sets SMTP authentication. Utilizes the Username and Password variables.
  187. * @var bool
  188. */
  189. public $SMTPAuth = false;
  190. /**
  191. * Sets SMTP username.
  192. * @var string
  193. */
  194. public $Username = '';
  195. /**
  196. * Sets SMTP password.
  197. * @var string
  198. */
  199. public $Password = '';
  200. /**
  201. * Sets the SMTP server timeout in seconds.
  202. * This function will not work with the win32 version.
  203. * @var int
  204. */
  205. public $Timeout = 10;
  206. /**
  207. * Sets SMTP class debugging on or off.
  208. * @var bool
  209. */
  210. public $SMTPDebug = false;
  211. /**
  212. * Prevents the SMTP connection from being closed after each mail
  213. * sending. If this is set to true then to close the connection
  214. * requires an explicit call to SmtpClose().
  215. * @var bool
  216. */
  217. public $SMTPKeepAlive = false;
  218. /**
  219. * Provides the ability to have the TO field process individual
  220. * emails, instead of sending to entire TO addresses
  221. * @var bool
  222. */
  223. public $SingleTo = false;
  224. /**
  225. * If SingleTo is true, this provides the array to hold the email addresses
  226. * @var bool
  227. */
  228. public $SingleToArray = array();
  229. /**
  230. * Provides the ability to change the line ending
  231. * @var string
  232. */
  233. public $LE = "\n";
  234. /**
  235. * Used with DKIM DNS Resource Record
  236. * @var string
  237. */
  238. public $DKIM_selector = 'phpmailer';
  239. /**
  240. * Used with DKIM DNS Resource Record
  241. * optional, in format of email address 'you@yourdomain.com'
  242. * @var string
  243. */
  244. public $DKIM_identity = '';
  245. /**
  246. * Used with DKIM DNS Resource Record
  247. * @var string
  248. */
  249. public $DKIM_passphrase = '';
  250. /**
  251. * Used with DKIM DNS Resource Record
  252. * optional, in format of email address 'you@yourdomain.com'
  253. * @var string
  254. */
  255. public $DKIM_domain = '';
  256. /**
  257. * Used with DKIM DNS Resource Record
  258. * optional, in format of email address 'you@yourdomain.com'
  259. * @var string
  260. */
  261. public $DKIM_private = '';
  262. /**
  263. * Callback Action function name
  264. * the function that handles the result of the send email action. Parameters:
  265. * bool $result result of the send action
  266. * string $to email address of the recipient
  267. * string $cc cc email addresses
  268. * string $bcc bcc email addresses
  269. * string $subject the subject
  270. * string $body the email body
  271. * @var string
  272. */
  273. public $action_function = ''; //'callbackAction';
  274. /**
  275. * Sets the PHPMailer Version number
  276. * @var string
  277. */
  278. public $Version = '5.2';
  279. /**
  280. * What to use in the X-Mailer header
  281. * @var string
  282. */
  283. public $XMailer = '';
  284. /////////////////////////////////////////////////
  285. // PROPERTIES, PRIVATE AND PROTECTED
  286. /////////////////////////////////////////////////
  287. protected $smtp = NULL;
  288. protected $to = array();
  289. protected $cc = array();
  290. protected $bcc = array();
  291. protected $ReplyTo = array();
  292. protected $all_recipients = array();
  293. protected $attachment = array();
  294. protected $CustomHeader = array();
  295. protected $message_type = '';
  296. protected $boundary = array();
  297. protected $language = array();
  298. protected $error_count = 0;
  299. protected $sign_cert_file = '';
  300. protected $sign_key_file = '';
  301. protected $sign_key_pass = '';
  302. protected $exceptions = false;
  303. /////////////////////////////////////////////////
  304. // CONSTANTS
  305. /////////////////////////////////////////////////
  306. const STOP_MESSAGE = 0; // message only, continue processing
  307. const STOP_CONTINUE = 1; // message?, likely ok to continue processing
  308. const STOP_CRITICAL = 2; // message, plus full stop, critical error reached
  309. /////////////////////////////////////////////////
  310. // METHODS, VARIABLES
  311. /////////////////////////////////////////////////
  312. /**
  313. * Constructor
  314. * @param boolean $exceptions Should we throw external exceptions?
  315. */
  316. public function __construct($exceptions = false) {
  317. $this->exceptions = ($exceptions == true);
  318. }
  319. /**
  320. * Sets message type to HTML.
  321. * @param bool $ishtml
  322. * @return void
  323. */
  324. public function IsHTML($ishtml = true) {
  325. if ($ishtml) {
  326. $this->ContentType = 'text/html';
  327. } else {
  328. $this->ContentType = 'text/plain';
  329. }
  330. }
  331. /**
  332. * Sets Mailer to send message using SMTP.
  333. * @return void
  334. */
  335. public function IsSMTP() {
  336. $this->Mailer = 'smtp';
  337. }
  338. /**
  339. * Sets Mailer to send message using PHP mail() function.
  340. * @return void
  341. */
  342. public function IsMail() {
  343. $this->Mailer = 'mail';
  344. }
  345. /**
  346. * Sets Mailer to send message using the $Sendmail program.
  347. * @return void
  348. */
  349. public function IsSendmail() {
  350. if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
  351. $this->Sendmail = '/var/qmail/bin/sendmail';
  352. }
  353. $this->Mailer = 'sendmail';
  354. }
  355. /**
  356. * Sets Mailer to send message using the qmail MTA.
  357. * @return void
  358. */
  359. public function IsQmail() {
  360. if (stristr(ini_get('sendmail_path'), 'qmail')) {
  361. $this->Sendmail = '/var/qmail/bin/sendmail';
  362. }
  363. $this->Mailer = 'sendmail';
  364. }
  365. /////////////////////////////////////////////////
  366. // METHODS, RECIPIENTS
  367. /////////////////////////////////////////////////
  368. /**
  369. * Adds a "To" address.
  370. * @param string $address
  371. * @param string $name
  372. * @return boolean true on success, false if address already used
  373. */
  374. public function AddAddress($address, $name = '') {
  375. return $this->AddAnAddress('to', $address, $name);
  376. }
  377. /**
  378. * Adds a "Cc" address.
  379. * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
  380. * @param string $address
  381. * @param string $name
  382. * @return boolean true on success, false if address already used
  383. */
  384. public function AddCC($address, $name = '') {
  385. return $this->AddAnAddress('cc', $address, $name);
  386. }
  387. /**
  388. * Adds a "Bcc" address.
  389. * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
  390. * @param string $address
  391. * @param string $name
  392. * @return boolean true on success, false if address already used
  393. */
  394. public function AddBCC($address, $name = '') {
  395. return $this->AddAnAddress('bcc', $address, $name);
  396. }
  397. /**
  398. * Adds a "Reply-to" address.
  399. * @param string $address
  400. * @param string $name
  401. * @return boolean
  402. */
  403. public function AddReplyTo($address, $name = '') {
  404. return $this->AddAnAddress('ReplyTo', $address, $name);
  405. }
  406. /**
  407. * Adds an address to one of the recipient arrays
  408. * Addresses that have been added already return false, but do not throw exceptions
  409. * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
  410. * @param string $address The email address to send to
  411. * @param string $name
  412. * @return boolean true on success, false if address already used or invalid in some way
  413. * @access protected
  414. */
  415. protected function AddAnAddress($kind, $address, $name = '') {
  416. if (!preg_match('/^(to|cc|bcc|ReplyTo)$/', $kind)) {
  417. $this->SetError($this->Lang('Invalid recipient array').': '.$kind);
  418. if ($this->exceptions) {
  419. throw new phpmailerException('Invalid recipient array: ' . $kind);
  420. }
  421. echo $this->Lang('Invalid recipient array').': '.$kind;
  422. return false;
  423. }
  424. $address = trim($address);
  425. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  426. if (!self::ValidateAddress($address)) {
  427. $this->SetError($this->Lang('invalid_address').': '. $address);
  428. if ($this->exceptions) {
  429. throw new phpmailerException($this->Lang('invalid_address').': '.$address);
  430. }
  431. echo $this->Lang('invalid_address').': '.$address;
  432. return false;
  433. }
  434. if ($kind != 'ReplyTo') {
  435. if (!isset($this->all_recipients[strtolower($address)])) {
  436. array_push($this->$kind, array($address, $name));
  437. $this->all_recipients[strtolower($address)] = true;
  438. return true;
  439. }
  440. } else {
  441. if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
  442. $this->ReplyTo[strtolower($address)] = array($address, $name);
  443. return true;
  444. }
  445. }
  446. return false;
  447. }
  448. /**
  449. * Set the From and FromName properties
  450. * @param string $address
  451. * @param string $name
  452. * @return boolean
  453. */
  454. public function SetFrom($address, $name = '', $auto = 1) {
  455. $address = trim($address);
  456. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  457. if (!self::ValidateAddress($address)) {
  458. $this->SetError($this->Lang('invalid_address').': '. $address);
  459. if ($this->exceptions) {
  460. throw new phpmailerException($this->Lang('invalid_address').': '.$address);
  461. }
  462. echo $this->Lang('invalid_address').': '.$address;
  463. return false;
  464. }
  465. $this->From = $address;
  466. $this->FromName = $name;
  467. if ($auto) {
  468. if (empty($this->ReplyTo)) {
  469. $this->AddAnAddress('ReplyTo', $address, $name);
  470. }
  471. if (empty($this->Sender)) {
  472. $this->Sender = $address;
  473. }
  474. }
  475. return true;
  476. }
  477. /**
  478. * Check that a string looks roughly like an email address should
  479. * Static so it can be used without instantiation
  480. * Tries to use PHP built-in validator in the filter extension (from PHP 5.2), falls back to a reasonably competent regex validator
  481. * Conforms approximately to RFC2822
  482. * @link http://www.hexillion.com/samples/#Regex Original pattern found here
  483. * @param string $address The email address to check
  484. * @return boolean
  485. * @static
  486. * @access public
  487. */
  488. public static function ValidateAddress($address) {
  489. if (function_exists('filter_var')) { //Introduced in PHP 5.2
  490. if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
  491. return false;
  492. } else {
  493. return true;
  494. }
  495. } else {
  496. return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
  497. }
  498. }
  499. /////////////////////////////////////////////////
  500. // METHODS, MAIL SENDING
  501. /////////////////////////////////////////////////
  502. /**
  503. * Creates message and assigns Mailer. If the message is
  504. * not sent successfully then it returns false. Use the ErrorInfo
  505. * variable to view description of the error.
  506. * @return bool
  507. */
  508. public function Send() {
  509. try {
  510. if(!$this->PreSend()) return false;
  511. return $this->PostSend();
  512. } catch (phpmailerException $e) {
  513. $this->SetError($e->getMessage());
  514. if ($this->exceptions) {
  515. throw $e;
  516. }
  517. return false;
  518. }
  519. }
  520. protected function PreSend() {
  521. try {
  522. if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  523. throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
  524. }
  525. // Set whether the message is multipart/alternative
  526. if(!empty($this->AltBody)) {
  527. $this->ContentType = 'multipart/alternative';
  528. }
  529. $this->error_count = 0; // reset errors
  530. $this->SetMessageType();
  531. //Refuse to send an empty message
  532. if (empty($this->Body)) {
  533. throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
  534. }
  535. $this->MIMEHeader = $this->CreateHeader();
  536. $this->MIMEBody = $this->CreateBody();
  537. // digitally sign with DKIM if enabled
  538. if ($this->DKIM_domain && $this->DKIM_private) {
  539. $header_dkim = $this->DKIM_Add($this->MIMEHeader, $this->EncodeHeader($this->SecureHeader($this->Subject)), $this->MIMEBody);
  540. $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
  541. }
  542. return true;
  543. } catch (phpmailerException $e) {
  544. $this->SetError($e->getMessage());
  545. if ($this->exceptions) {
  546. throw $e;
  547. }
  548. return false;
  549. }
  550. }
  551. protected function PostSend() {
  552. try {
  553. // Choose the mailer and send through it
  554. switch($this->Mailer) {
  555. case 'sendmail':
  556. return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
  557. case 'smtp':
  558. return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
  559. default:
  560. return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
  561. }
  562. } catch (phpmailerException $e) {
  563. $this->SetError($e->getMessage());
  564. if ($this->exceptions) {
  565. throw $e;
  566. }
  567. echo $e->getMessage()."\n";
  568. return false;
  569. }
  570. }
  571. /**
  572. * Sends mail using the $Sendmail program.
  573. * @param string $header The message headers
  574. * @param string $body The message body
  575. * @access protected
  576. * @return bool
  577. */
  578. protected function SendmailSend($header, $body) {
  579. if ($this->Sender != '') {
  580. $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  581. } else {
  582. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  583. }
  584. if ($this->SingleTo === true) {
  585. foreach ($this->SingleToArray as $key => $val) {
  586. if(!@$mail = popen($sendmail, 'w')) {
  587. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  588. }
  589. fputs($mail, "To: " . $val . "\n");
  590. fputs($mail, $header);
  591. fputs($mail, $body);
  592. $result = pclose($mail);
  593. // implement call back function if it exists
  594. $isSent = ($result == 0) ? 1 : 0;
  595. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  596. if($result != 0) {
  597. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  598. }
  599. }
  600. } else {
  601. if(!@$mail = popen($sendmail, 'w')) {
  602. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  603. }
  604. fputs($mail, $header);
  605. fputs($mail, $body);
  606. $result = pclose($mail);
  607. // implement call back function if it exists
  608. $isSent = ($result == 0) ? 1 : 0;
  609. $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body);
  610. if($result != 0) {
  611. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  612. }
  613. }
  614. return true;
  615. }
  616. /**
  617. * Sends mail using the PHP mail() function.
  618. * @param string $header The message headers
  619. * @param string $body The message body
  620. * @access protected
  621. * @return bool
  622. */
  623. protected function MailSend($header, $body) {
  624. $toArr = array();
  625. foreach($this->to as $t) {
  626. $toArr[] = $this->AddrFormat($t);
  627. }
  628. $to = implode(', ', $toArr);
  629. if (empty($this->Sender)) {
  630. $params = "-oi -f %s";
  631. } else {
  632. $params = sprintf("-oi -f %s", $this->Sender);
  633. }
  634. if ($this->Sender != '' and !ini_get('safe_mode')) {
  635. $old_from = ini_get('sendmail_from');
  636. ini_set('sendmail_from', $this->Sender);
  637. if ($this->SingleTo === true && count($toArr) > 1) {
  638. foreach ($toArr as $key => $val) {
  639. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  640. // implement call back function if it exists
  641. $isSent = ($rt == 1) ? 1 : 0;
  642. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  643. }
  644. } else {
  645. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  646. // implement call back function if it exists
  647. $isSent = ($rt == 1) ? 1 : 0;
  648. $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
  649. }
  650. } else {
  651. if ($this->SingleTo === true && count($toArr) > 1) {
  652. foreach ($toArr as $key => $val) {
  653. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  654. // implement call back function if it exists
  655. $isSent = ($rt == 1) ? 1 : 0;
  656. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  657. }
  658. } else {
  659. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
  660. // implement call back function if it exists
  661. $isSent = ($rt == 1) ? 1 : 0;
  662. $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
  663. }
  664. }
  665. if (isset($old_from)) {
  666. ini_set('sendmail_from', $old_from);
  667. }
  668. if(!$rt) {
  669. throw new phpmailerException($this->Lang('instantiate'), self::STOP_CRITICAL);
  670. }
  671. return true;
  672. }
  673. /**
  674. * Sends mail via SMTP using PhpSMTP
  675. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
  676. * @param string $header The message headers
  677. * @param string $body The message body
  678. * @uses SMTP
  679. * @access protected
  680. * @return bool
  681. */
  682. protected function SmtpSend($header, $body) {
  683. $bad_rcpt = array();
  684. if(!$this->SmtpConnect()) {
  685. throw new phpmailerException($this->Lang('smtp_connect_failed'), self::STOP_CRITICAL);
  686. }
  687. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  688. if(!$this->smtp->Mail($smtp_from)) {
  689. throw new phpmailerException($this->Lang('from_failed') . $smtp_from, self::STOP_CRITICAL);
  690. }
  691. // Attempt to send attach all recipients
  692. foreach($this->to as $to) {
  693. if (!$this->smtp->Recipient($to[0])) {
  694. $bad_rcpt[] = $to[0];
  695. // implement call back function if it exists
  696. $isSent = 0;
  697. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
  698. } else {
  699. // implement call back function if it exists
  700. $isSent = 1;
  701. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
  702. }
  703. }
  704. foreach($this->cc as $cc) {
  705. if (!$this->smtp->Recipient($cc[0])) {
  706. $bad_rcpt[] = $cc[0];
  707. // implement call back function if it exists
  708. $isSent = 0;
  709. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
  710. } else {
  711. // implement call back function if it exists
  712. $isSent = 1;
  713. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
  714. }
  715. }
  716. foreach($this->bcc as $bcc) {
  717. if (!$this->smtp->Recipient($bcc[0])) {
  718. $bad_rcpt[] = $bcc[0];
  719. // implement call back function if it exists
  720. $isSent = 0;
  721. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
  722. } else {
  723. // implement call back function if it exists
  724. $isSent = 1;
  725. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
  726. }
  727. }
  728. if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses
  729. $badaddresses = implode(', ', $bad_rcpt);
  730. throw new phpmailerException($this->Lang('recipients_failed') . $badaddresses);
  731. }
  732. if(!$this->smtp->Data($header . $body)) {
  733. throw new phpmailerException($this->Lang('data_not_accepted'), self::STOP_CRITICAL);
  734. }
  735. if($this->SMTPKeepAlive == true) {
  736. $this->smtp->Reset();
  737. }
  738. return true;
  739. }
  740. /**
  741. * Initiates a connection to an SMTP server.
  742. * Returns false if the operation failed.
  743. * @uses SMTP
  744. * @access public
  745. * @return bool
  746. */
  747. public function SmtpConnect() {
  748. if(is_null($this->smtp)) {
  749. $this->smtp = new SMTP();
  750. }
  751. $this->smtp->do_debug = $this->SMTPDebug;
  752. $hosts = explode(';', $this->Host);
  753. $index = 0;
  754. $connection = $this->smtp->Connected();
  755. // Retry while there is no connection
  756. try {
  757. while($index < count($hosts) && !$connection) {
  758. $hostinfo = array();
  759. if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
  760. $host = $hostinfo[1];
  761. $port = $hostinfo[2];
  762. } else {
  763. $host = $hosts[$index];
  764. $port = $this->Port;
  765. }
  766. $tls = ($this->SMTPSecure == 'tls');
  767. $ssl = ($this->SMTPSecure == 'ssl');
  768. if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
  769. $hello = ($this->Helo != '' ? $this->Helo : $this->ServerHostname());
  770. $this->smtp->Hello($hello);
  771. if ($tls) {
  772. if (!$this->smtp->StartTLS()) {
  773. throw new phpmailerException($this->Lang('tls'));
  774. }
  775. //We must resend HELO after tls negotiation
  776. $this->smtp->Hello($hello);
  777. }
  778. $connection = true;
  779. if ($this->SMTPAuth) {
  780. if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
  781. throw new phpmailerException($this->Lang('authenticate'));
  782. }
  783. }
  784. }
  785. $index++;
  786. if (!$connection) {
  787. throw new phpmailerException($this->Lang('connect_host'));
  788. }
  789. }
  790. } catch (phpmailerException $e) {
  791. $this->smtp->Reset();
  792. throw $e;
  793. }
  794. return true;
  795. }
  796. /**
  797. * Closes the active SMTP session if one exists.
  798. * @return void
  799. */
  800. public function SmtpClose() {
  801. if(!is_null($this->smtp)) {
  802. if($this->smtp->Connected()) {
  803. $this->smtp->Quit();
  804. $this->smtp->Close();
  805. }
  806. }
  807. }
  808. /**
  809. * Sets the language for all class error messages.
  810. * Returns false if it cannot load the language file. The default language is English.
  811. * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
  812. * @param string $lang_path Path to the language file directory
  813. * @access public
  814. */
  815. function SetLanguage($langcode = 'en', $lang_path = 'language/') {
  816. //Define full set of translatable strings
  817. $PHPMAILER_LANG = array(
  818. 'provide_address' => 'You must provide at least one recipient email address.',
  819. 'mailer_not_supported' => ' mailer is not supported.',
  820. 'execute' => 'Could not execute: ',
  821. 'instantiate' => 'Could not instantiate mail function.',
  822. 'authenticate' => 'SMTP Error: Could not authenticate.',
  823. 'from_failed' => 'The following From address failed: ',
  824. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  825. 'data_not_accepted' => 'SMTP Error: Data not accepted.',
  826. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  827. 'file_access' => 'Could not access file: ',
  828. 'file_open' => 'File Error: Could not open file: ',
  829. 'encoding' => 'Unknown encoding: ',
  830. 'signing' => 'Signing Error: ',
  831. 'smtp_error' => 'SMTP server error: ',
  832. 'empty_message' => 'Message body empty',
  833. 'invalid_address' => 'Invalid address',
  834. 'variable_set' => 'Cannot set or reset variable: '
  835. );
  836. //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
  837. $l = true;
  838. if ($langcode != 'en') { //There is no English translation file
  839. $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
  840. }
  841. $this->language = $PHPMAILER_LANG;
  842. return ($l == true); //Returns false if language not found
  843. }
  844. /**
  845. * Return the current array of language strings
  846. * @return array
  847. */
  848. public function GetTranslations() {
  849. return $this->language;
  850. }
  851. /////////////////////////////////////////////////
  852. // METHODS, MESSAGE CREATION
  853. /////////////////////////////////////////////////
  854. /**
  855. * Creates recipient headers.
  856. * @access public
  857. * @return string
  858. */
  859. public function AddrAppend($type, $addr) {
  860. $addr_str = $type . ': ';
  861. $addresses = array();
  862. foreach ($addr as $a) {
  863. $addresses[] = $this->AddrFormat($a);
  864. }
  865. $addr_str .= implode(', ', $addresses);
  866. $addr_str .= $this->LE;
  867. return $addr_str;
  868. }
  869. /**
  870. * Formats an address correctly.
  871. * @access public
  872. * @return string
  873. */
  874. public function AddrFormat($addr) {
  875. if (empty($addr[1])) {
  876. return $this->SecureHeader($addr[0]);
  877. } else {
  878. return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  879. }
  880. }
  881. /**
  882. * Wraps message for use with mailers that do not
  883. * automatically perform wrapping and for quoted-printable.
  884. * Original written by philippe.
  885. * @param string $message The message to wrap
  886. * @param integer $length The line length to wrap to
  887. * @param boolean $qp_mode Whether to run in Quoted-Printable mode
  888. * @access public
  889. * @return string
  890. */
  891. public function WrapText($message, $length, $qp_mode = false) {
  892. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  893. // If utf-8 encoding is used, we will need to make sure we don't
  894. // split multibyte characters when we wrap
  895. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  896. $message = $this->FixEOL($message);
  897. if (substr($message, -1) == $this->LE) {
  898. $message = substr($message, 0, -1);
  899. }
  900. $line = explode($this->LE, $message);
  901. $message = '';
  902. for ($i = 0 ;$i < count($line); $i++) {
  903. $line_part = explode(' ', $line[$i]);
  904. $buf = '';
  905. for ($e = 0; $e<count($line_part); $e++) {
  906. $word = $line_part[$e];
  907. if ($qp_mode and (strlen($word) > $length)) {
  908. $space_left = $length - strlen($buf) - 1;
  909. if ($e != 0) {
  910. if ($space_left > 20) {
  911. $len = $space_left;
  912. if ($is_utf8) {
  913. $len = $this->UTF8CharBoundary($word, $len);
  914. } elseif (substr($word, $len - 1, 1) == "=") {
  915. $len--;
  916. } elseif (substr($word, $len - 2, 1) == "=") {
  917. $len -= 2;
  918. }
  919. $part = substr($word, 0, $len);
  920. $word = substr($word, $len);
  921. $buf .= ' ' . $part;
  922. $message .= $buf . sprintf("=%s", $this->LE);
  923. } else {
  924. $message .= $buf . $soft_break;
  925. }
  926. $buf = '';
  927. }
  928. while (strlen($word) > 0) {
  929. $len = $length;
  930. if ($is_utf8) {
  931. $len = $this->UTF8CharBoundary($word, $len);
  932. } elseif (substr($word, $len - 1, 1) == "=") {
  933. $len--;
  934. } elseif (substr($word, $len - 2, 1) == "=") {
  935. $len -= 2;
  936. }
  937. $part = substr($word, 0, $len);
  938. $word = substr($word, $len);
  939. if (strlen($word) > 0) {
  940. $message .= $part . sprintf("=%s", $this->LE);
  941. } else {
  942. $buf = $part;
  943. }
  944. }
  945. } else {
  946. $buf_o = $buf;
  947. $buf .= ($e == 0) ? $word : (' ' . $word);
  948. if (strlen($buf) > $length and $buf_o != '') {
  949. $message .= $buf_o . $soft_break;
  950. $buf = $word;
  951. }
  952. }
  953. }
  954. $message .= $buf . $this->LE;
  955. }
  956. return $message;
  957. }
  958. /**
  959. * Finds last character boundary prior to maxLength in a utf-8
  960. * quoted (printable) encoded string.
  961. * Original written by Colin Brown.
  962. * @access public
  963. * @param string $encodedText utf-8 QP text
  964. * @param int $maxLength find last character boundary prior to this length
  965. * @return int
  966. */
  967. public function UTF8CharBoundary($encodedText, $maxLength) {
  968. $foundSplitPos = false;
  969. $lookBack = 3;
  970. while (!$foundSplitPos) {
  971. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  972. $encodedCharPos = strpos($lastChunk, "=");
  973. if ($encodedCharPos !== false) {
  974. // Found start of encoded character byte within $lookBack block.
  975. // Check the encoded byte value (the 2 chars after the '=')
  976. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  977. $dec = hexdec($hex);
  978. if ($dec < 128) { // Single byte character.
  979. // If the encoded char was found at pos 0, it will fit
  980. // otherwise reduce maxLength to start of the encoded char
  981. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  982. $maxLength - ($lookBack - $encodedCharPos);
  983. $foundSplitPos = true;
  984. } elseif ($dec >= 192) { // First byte of a multi byte character
  985. // Reduce maxLength to split at start of character
  986. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  987. $foundSplitPos = true;
  988. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  989. $lookBack += 3;
  990. }
  991. } else {
  992. // No encoded character found
  993. $foundSplitPos = true;
  994. }
  995. }
  996. return $maxLength;
  997. }
  998. /**
  999. * Set the body wrapping.
  1000. * @access public
  1001. * @return void
  1002. */
  1003. public function SetWordWrap() {
  1004. if($this->WordWrap < 1) {
  1005. return;
  1006. }
  1007. switch($this->message_type) {
  1008. case 'alt':
  1009. case 'alt_inline':
  1010. case 'alt_attach':
  1011. case 'alt_inline_attach':
  1012. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  1013. break;
  1014. default:
  1015. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  1016. break;
  1017. }
  1018. }
  1019. /**
  1020. * Assembles message header.
  1021. * @access public
  1022. * @return string The assembled header
  1023. */
  1024. public function CreateHeader() {
  1025. $result = '';
  1026. // Set the boundaries
  1027. $uniq_id = md5(uniqid(time()));
  1028. $this->boundary[1] = 'b1_' . $uniq_id;
  1029. $this->boundary[2] = 'b2_' . $uniq_id;
  1030. $this->boundary[3] = 'b3_' . $uniq_id;
  1031. $result .= $this->HeaderLine('Date', self::RFCDate());
  1032. if($this->Sender == '') {
  1033. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  1034. } else {
  1035. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  1036. }
  1037. // To be created automatically by mail()
  1038. if($this->Mailer != 'mail') {
  1039. if ($this->SingleTo === true) {
  1040. foreach($this->to as $t) {
  1041. $this->SingleToArray[] = $this->AddrFormat($t);
  1042. }
  1043. } else {
  1044. if(count($this->to) > 0) {
  1045. $result .= $this->AddrAppend('To', $this->to);
  1046. } elseif (count($this->cc) == 0) {
  1047. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  1048. }
  1049. }
  1050. }
  1051. $from = array();
  1052. $from[0][0] = trim($this->From);
  1053. $from[0][1] = $this->FromName;
  1054. $result .= $this->AddrAppend('From', $from);
  1055. // sendmail and mail() extract Cc from the header before sending
  1056. if(count($this->cc) > 0) {
  1057. $result .= $this->AddrAppend('Cc', $this->cc);
  1058. }
  1059. // sendmail and mail() extract Bcc from the header before sending
  1060. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  1061. $result .= $this->AddrAppend('Bcc', $this->bcc);
  1062. }
  1063. if(count($this->ReplyTo) > 0) {
  1064. $result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
  1065. }
  1066. // mail() sets the subject itself
  1067. if($this->Mailer != 'mail') {
  1068. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  1069. }
  1070. if($this->MessageID != '') {
  1071. $result .= $this->HeaderLine('Message-ID', $this->MessageID);
  1072. } else {
  1073. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  1074. }
  1075. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  1076. if($this->XMailer) {
  1077. $result .= $this->HeaderLine('X-Mailer', $this->XMailer);
  1078. } else {
  1079. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (http://code.google.com/a/apache-extras.org/p/phpmailer/)');
  1080. }
  1081. if($this->ConfirmReadingTo != '') {
  1082. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  1083. }
  1084. // Add custom headers
  1085. for($index = 0; $index < count($this->CustomHeader); $index++) {
  1086. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  1087. }
  1088. if (!$this->sign_key_file) {
  1089. $result .= $this->HeaderLine('MIME-Version', '1.0');
  1090. $result .= $this->GetMailMIME();
  1091. }
  1092. return $result;
  1093. }
  1094. /**
  1095. * Returns the message MIME.
  1096. * @access public
  1097. * @return string
  1098. */
  1099. public function GetMailMIME() {
  1100. $result = '';
  1101. switch($this->message_type) {
  1102. case 'plain':
  1103. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  1104. $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset="'.$this->CharSet.'"');
  1105. break;
  1106. case 'inline':
  1107. $result .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1108. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1109. break;
  1110. case 'attach':
  1111. case 'inline_attach':
  1112. case 'alt_attach':
  1113. case 'alt_inline_attach':
  1114. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  1115. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1116. break;
  1117. case 'alt':
  1118. case 'alt_inline':
  1119. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1120. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1121. break;
  1122. }
  1123. if($this->Mailer != 'mail') {
  1124. $result .= $this->LE.$this->LE;
  1125. }
  1126. return $result;
  1127. }
  1128. /**
  1129. * Assembles the message body. Returns an empty string on failure.
  1130. * @access public
  1131. * @return string The assembled message body
  1132. */
  1133. public function CreateBody() {
  1134. $body = '';
  1135. if ($this->sign_key_file) {
  1136. $body .= $this->GetMailMIME();
  1137. }
  1138. $this->SetWordWrap();
  1139. switch($this->message_type) {
  1140. case 'plain':
  1141. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1142. break;
  1143. case 'inline':
  1144. $body .= $this->GetBoundary($this->boundary[1], '', '', '');
  1145. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1146. $body .= $this->LE.$this->LE;
  1147. $body .= $this->AttachAll("inline", $this->boundary[1]);
  1148. break;
  1149. case 'attach':
  1150. $body .= $this->GetBoundary($this->boundary[1], '', '', '');
  1151. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1152. $body .= $this->LE.$this->LE;
  1153. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1154. break;
  1155. case 'inline_attach':
  1156. $body .= $this->TextLine("--" . $this->boundary[1]);
  1157. $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1158. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1159. $body .= $this->LE;
  1160. $body .= $this->GetBoundary($this->boundary[2], '', '', '');
  1161. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1162. $body .= $this->LE.$this->LE;
  1163. $body .= $this->AttachAll("inline", $this->boundary[2]);
  1164. $body .= $this->LE;
  1165. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1166. break;
  1167. case 'alt':
  1168. $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  1169. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1170. $body .= $this->LE.$this->LE;
  1171. $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  1172. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1173. $body .= $this->LE.$this->LE;
  1174. $body .= $this->EndBoundary($this->boundary[1]);
  1175. break;
  1176. case 'alt_inline':
  1177. $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  1178. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1179. $body .= $this->LE.$this->LE;
  1180. $body .= $this->TextLine("--" . $this->boundary[1]);
  1181. $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1182. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1183. $body .= $this->LE;
  1184. $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
  1185. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1186. $body .= $this->LE.$this->LE;
  1187. $body .= $this->AttachAll("inline", $this->boundary[2]);
  1188. $body .= $this->LE;
  1189. $body .= $this->EndBoundary($this->boundary[1]);
  1190. break;
  1191. case 'alt_attach':
  1192. $body .= $this->TextLine("--" . $this->boundary[1]);
  1193. $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1194. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1195. $body .= $this->LE;
  1196. $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
  1197. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1198. $body .= $this->LE.$this->LE;
  1199. $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
  1200. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1201. $body .= $this->LE.$this->LE;
  1202. $body .= $this->EndBoundary($this->boundary[2]);
  1203. $body .= $this->LE;
  1204. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1205. break;
  1206. case 'alt_inline_attach':
  1207. $body .= $this->TextLine("--" . $this->boundary[1]);
  1208. $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1209. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1210. $body .= $this->LE;
  1211. $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
  1212. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1213. $body .= $this->LE.$this->LE;
  1214. $body .= $this->TextLine("--" . $this->boundary[2]);
  1215. $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1216. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[3] . '"');
  1217. $body .= $this->LE;
  1218. $body .= $this->GetBoundary($this->boundary[3], '', 'text/html', '');
  1219. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1220. $body .= $this->LE.$this->LE;
  1221. $body .= $this->AttachAll("inline", $this->boundary[3]);
  1222. $body .= $this->LE;
  1223. $body .= $this->EndBoundary($this->boundary[2]);
  1224. $body .= $this->LE;
  1225. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1226. break;
  1227. }
  1228. if ($this->IsError()) {
  1229. $body = '';
  1230. } elseif ($this->sign_key_file) {
  1231. try {
  1232. $file = tempnam('', 'mail');
  1233. file_put_contents($file, $body); //TODO check this worked
  1234. $signed = tempnam("", "signed");
  1235. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {
  1236. @unlink($file);
  1237. @unlink($signed);
  1238. $body = file_get_contents($signed);
  1239. } else {
  1240. @unlink($file);
  1241. @unlink($signed);
  1242. throw new phpmailerException($this->Lang("signing").openssl_error_string());
  1243. }
  1244. } catch (phpmailerException $e) {
  1245. $body = '';
  1246. if ($this->exceptions) {
  1247. throw $e;
  1248. }
  1249. }
  1250. }
  1251. return $body;
  1252. }
  1253. /**
  1254. * Returns the start of a message boundary.
  1255. * @access protected
  1256. * @return string
  1257. */
  1258. protected function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  1259. $result = '';
  1260. if($charSet == '') {
  1261. $charSet = $this->CharSet;
  1262. }
  1263. if($contentType == '') {
  1264. $contentType = $this->ContentType;
  1265. }
  1266. if($encoding == '') {
  1267. $encoding = $this->Encoding;
  1268. }
  1269. $result .= $this->TextLine('--' . $boundary);
  1270. $result .= sprintf("Content-Type: %s; charset=\"%s\"", $contentType, $charSet);
  1271. $result .= $this->LE;
  1272. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  1273. $result .= $this->LE;
  1274. return $result;
  1275. }
  1276. /**
  1277. * Returns the end of a message boundary.
  1278. * @access protected
  1279. * @return string
  1280. */
  1281. protected function EndBoundary($boundary) {
  1282. return $this->LE . '--' . $boundary . '--' . $this->LE;
  1283. }
  1284. /**
  1285. * Sets the message type.
  1286. * @access protected
  1287. * @return void
  1288. */
  1289. protected function SetMessageType() {
  1290. $this->message_type = array();
  1291. if($this->AlternativeExists()) $this->message_type[] = "alt";
  1292. if($this->InlineImageExists()) $this->message_type[] = "inline";
  1293. if($this->AttachmentExists()) $this->message_type[] = "attach";
  1294. $this->message_type = implode("_", $this->message_type);
  1295. if($this->message_type == "") $this->message_type = "plain";
  1296. }
  1297. /**
  1298. * Returns a formatted header line.
  1299. * @access public
  1300. * @return string
  1301. */
  1302. public function HeaderLine($name, $value) {
  1303. return $name . ': ' . $value . $this->LE;
  1304. }
  1305. /**
  1306. * Returns a formatted mail line.
  1307. * @access public
  1308. * @return string
  1309. */
  1310. public function TextLine($value) {
  1311. return $value . $this->LE;
  1312. }
  1313. /////////////////////////////////////////////////
  1314. // CLASS METHODS, ATTACHMENTS
  1315. /////////////////////////////////////////////////
  1316. /**
  1317. * Adds an attachment from a path on the filesystem.
  1318. * Returns false if the file could not be found
  1319. * or accessed.
  1320. * @param string $path Path to the attachment.
  1321. * @param string $name Overrides the attachment name.
  1322. * @param string $encoding File encoding (see $Encoding).
  1323. * @param string $type File extension (MIME) type.
  1324. * @return bool
  1325. */
  1326. public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1327. try {
  1328. if ( !@is_file($path) ) {
  1329. throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
  1330. }
  1331. $filename = basename($path);
  1332. if ( $name == '' ) {
  1333. $name = $filename;
  1334. }
  1335. $this->attachment[] = array(
  1336. 0 => $path,
  1337. 1 => $filename,
  1338. 2 => $name,
  1339. 3 => $encoding,
  1340. 4 => $type,
  1341. 5 => false, // isStringAttachment
  1342. 6 => 'attachment',
  1343. 7 => 0
  1344. );
  1345. } catch (phpmailerException $e) {
  1346. $this->SetError($e->getMessage());
  1347. if ($this->exceptions) {
  1348. throw $e;
  1349. }
  1350. echo $e->getMessage()."\n";
  1351. if ( $e->getCode() == self::STOP_CRITICAL ) {
  1352. return false;
  1353. }
  1354. }
  1355. return true;
  1356. }
  1357. /**
  1358. * Return the current array of attachments
  1359. * @return array
  1360. */
  1361. public function GetAttachments() {
  1362. return $this->attachment;
  1363. }
  1364. /**
  1365. * Attaches all fs, string, and binary attachments to the message.
  1366. * Returns an empty string on failure.
  1367. * @access protected
  1368. * @return string
  1369. */
  1370. protected function AttachAll($disposition_type, $boundary) {
  1371. // Return text of body
  1372. $mime = array();
  1373. $cidUniq = array();
  1374. $incl = array();
  1375. // Add all attachments
  1376. foreach ($this->attachment as $attachment) {
  1377. // CHECK IF IT IS A VALID DISPOSITION_FILTER
  1378. if($attachment[6] == $disposition_type) {
  1379. // Check for string attachment
  1380. $bString = $attachment[5];
  1381. if ($bString) {
  1382. $string = $attachment[0];
  1383. } else {
  1384. $path = $attachment[0];
  1385. }
  1386. $inclhash = md5(serialize($attachment));
  1387. if (in_array($inclhash, $incl)) { continue; }
  1388. $incl[] = $inclhash;
  1389. $filename = $attachment[1];
  1390. $name = $attachment[2];
  1391. $encoding = $attachment[3];
  1392. $type = $attachment[4];
  1393. $disposition = $attachment[6];
  1394. $cid = $attachment[7];
  1395. if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
  1396. $cidUniq[$cid] = true;
  1397. $mime[] = sprintf("--%s%s", $boundary, $this->LE);
  1398. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
  1399. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1400. if($disposition == 'inline') {
  1401. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1402. }
  1403. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
  1404. // Encode as string attachment
  1405. if($bString) {
  1406. $mime[] = $this->EncodeString($string, $encoding);
  1407. if($this->IsError()) {
  1408. return '';
  1409. }
  1410. $mime[] = $this->LE.$this->LE;
  1411. } else {
  1412. $mime[] = $this->EncodeFile($path, $encoding);
  1413. if($this->IsError()) {
  1414. return '';
  1415. }
  1416. $mime[] = $this->LE.$this->LE;
  1417. }
  1418. }
  1419. }
  1420. $mime[] = sprintf("--%s--%s", $boundary, $this->LE);
  1421. return implode("", $mime);
  1422. }
  1423. /**
  1424. * Encodes attachment in requested format.
  1425. * Returns an empty string on failure.
  1426. * @param string $path The full path to the file
  1427. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
  1428. * @see EncodeFile()
  1429. * @access protected
  1430. * @return string
  1431. */
  1432. protected function EncodeFile($path, $encoding = 'base64') {
  1433. try {
  1434. if (!is_readable($path)) {
  1435. throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
  1436. }
  1437. if (function_exists('get_magic_quotes')) {
  1438. function get_magic_quotes() {
  1439. return false;
  1440. }
  1441. }
  1442. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  1443. $magic_quotes = get_magic_quotes_runtime();
  1444. set_magic_quotes_runtime(0);
  1445. }
  1446. $file_buffer = file_get_contents($path);
  1447. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1448. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  1449. set_magic_quotes_runtime($magic_quotes);
  1450. }
  1451. return $file_buffer;
  1452. } catch (Exception $e) {
  1453. $this->SetError($e->getMessage());
  1454. return '';
  1455. }
  1456. }
  1457. /**
  1458. * Encodes string to requested format.
  1459. * Returns an empty string on failure.
  1460. * @param string $str The text to encode
  1461. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
  1462. * @access public
  1463. * @return string
  1464. */
  1465. public function EncodeString($str, $encoding = 'base64') {
  1466. $encoded = '';
  1467. switch(strtolower($encoding)) {
  1468. case 'base64':
  1469. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1470. break;
  1471. case '7bit':
  1472. case '8bit':
  1473. $encoded = $this->FixEOL($str);
  1474. //Make sure it ends with a line break
  1475. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1476. $encoded .= $this->LE;
  1477. break;
  1478. case 'binary':
  1479. $encoded = $str;
  1480. break;
  1481. case 'quoted-printable':
  1482. $encoded = $this->EncodeQP($str);
  1483. break;
  1484. default:
  1485. $this->SetError($this->Lang('encoding') . $encoding);
  1486. break;
  1487. }
  1488. return $encoded;
  1489. }
  1490. /**
  1491. * Encode a header string to best (shortest) of Q, B, quoted or none.
  1492. * @access public
  1493. * @return string
  1494. */
  1495. public function EncodeHeader($str, $position = 'text') {
  1496. $x = 0;
  1497. switch (strtolower($position)) {
  1498. case 'phrase':
  1499. if (!preg_match('/[\200-\377]/', $str)) {
  1500. // Can't use addslashes as we don't know what value has magic_quotes_sybase
  1501. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1502. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1503. return ($encoded);
  1504. } else {
  1505. return ("\"$encoded\"");
  1506. }
  1507. }
  1508. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1509. break;
  1510. case 'comment':
  1511. $x = preg_match_all('/[()"]/', $str, $matches);
  1512. // Fall-through
  1513. case 'text':
  1514. default:
  1515. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1516. break;
  1517. }
  1518. if ($x == 0) {
  1519. return ($str);
  1520. }
  1521. $maxlen = 75 - 7 - strlen($this->CharSet);
  1522. // Try to select the encoding which should produce the shortest output
  1523. if (strlen($str)/3 < $x) {
  1524. $encoding = 'B';
  1525. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1526. // Use a custom function which correctly encodes and wraps long
  1527. // multibyte strings without breaking lines within a character
  1528. $encoded = $this->Base64EncodeWrapMB($str);
  1529. } else {
  1530. $encoded = base64_encode($str);
  1531. $maxlen -= $maxlen % 4;
  1532. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1533. }
  1534. } else {
  1535. $encoding = 'Q';
  1536. $encoded = $this->EncodeQ($str, $position);
  1537. $encoded = $this->WrapText($encoded, $maxlen, true);
  1538. $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
  1539. }
  1540. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1541. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1542. return $encoded;
  1543. }
  1544. /**
  1545. * Checks if a string contains multibyte characters.
  1546. * @access public
  1547. * @param string $str multi-byte text to wrap encode
  1548. * @return bool
  1549. */
  1550. public function HasMultiBytes($str) {
  1551. if (function_exists('mb_strlen')) {
  1552. return (strlen($str) > mb_strlen($str, $this->CharSet));
  1553. } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
  1554. return false;
  1555. }
  1556. }
  1557. /**
  1558. * Correctly encodes and wraps long multibyte strings for mail headers
  1559. * without breaking lines within a character.
  1560. * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
  1561. * @access public
  1562. * @param string $str multi-byte text to wrap encode
  1563. * @return string
  1564. */
  1565. public function Base64EncodeWrapMB($str) {
  1566. $start = "=?".$this->CharSet."?B?";
  1567. $end = "?=";
  1568. $encoded = "";
  1569. $mb_length = mb_strlen($str, $this->CharSet);
  1570. // Each line must have length <= 75, including $start and $end
  1571. $length = 75 - strlen($start) - strlen($end);
  1572. // Average multi-byte ratio
  1573. $ratio = $mb_length / strlen($str);
  1574. // Base64 has a 4:3 ratio
  1575. $offset = $avgLength = floor($length * $ratio * .75);
  1576. for ($i = 0; $i < $mb_length; $i += $offset) {
  1577. $lookBack = 0;
  1578. do {
  1579. $offset = $avgLength - $lookBack;
  1580. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1581. $chunk = base64_encode($chunk);
  1582. $lookBack++;
  1583. }
  1584. while (strlen($chunk) > $length);
  1585. $encoded .= $chunk . $this->LE;
  1586. }
  1587. // Chomp the last linefeed
  1588. $encoded = substr($encoded, 0, -strlen($this->LE));
  1589. return $encoded;
  1590. }
  1591. /**
  1592. * Encode string to quoted-printable.
  1593. * Only uses standard PHP, slow, but will always work
  1594. * @access public
  1595. * @param string $string the text to encode
  1596. * @param integer $line_max Number of chars allowed on a line before wrapping
  1597. * @return string
  1598. */
  1599. public function EncodeQPphp( $input = '', $line_max = 76, $space_conv = false) {
  1600. $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
  1601. $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
  1602. $eol = "\r\n";
  1603. $escape = '=';
  1604. $output = '';
  1605. while( list(, $line) = each($lines) ) {
  1606. $linlen = strlen($line);
  1607. $newline = '';
  1608. for($i = 0; $i < $linlen; $i++) {
  1609. $c = substr( $line, $i, 1 );
  1610. $dec = ord( $c );
  1611. if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
  1612. $c = '=2E';
  1613. }
  1614. if ( $dec == 32 ) {
  1615. if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
  1616. $c = '=20';
  1617. } else if ( $space_conv ) {
  1618. $c = '=20';
  1619. }
  1620. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1621. $h2 = floor($dec/16);
  1622. $h1 = floor($dec%16);
  1623. $c = $escape.$hex[$h2].$hex[$h1];
  1624. }
  1625. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1626. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1627. $newline = '';
  1628. // check if newline first character will be point or not
  1629. if ( $dec == 46 ) {
  1630. $c = '=2E';
  1631. }
  1632. }
  1633. $newline .= $c;
  1634. } // end of for
  1635. $output .= $newline.$eol;
  1636. } // end of while
  1637. return $output;
  1638. }
  1639. /**
  1640. * Encode string to RFC2045 (6.7) quoted-printable format
  1641. * Uses a PHP5 stream filter to do the encoding about 64x faster than the old version
  1642. * Also results in same content as you started with after decoding
  1643. * @see EncodeQPphp()
  1644. * @access public
  1645. * @param string $string the text to encode
  1646. * @param integer $line_max Number of chars allowed on a line before wrapping
  1647. * @param boolean $space_conv Dummy param for compatibility with existing EncodeQP function
  1648. * @return string
  1649. * @author Marcus Bointon
  1650. */
  1651. public function EncodeQP($string, $line_max = 76, $space_conv = false) {
  1652. if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
  1653. return quoted_printable_encode($string);
  1654. }
  1655. $filters = stream_get_filters();
  1656. if (!in_array('convert.*', $filters)) { //Got convert stream filter?
  1657. return $this->EncodeQPphp($string, $line_max, $space_conv); //Fall back to old implementation
  1658. }
  1659. $fp = fopen('php://temp/', 'r+');
  1660. $string = preg_replace('/\r\n?/', $this->LE, $string); //Normalise line breaks
  1661. $params = array('line-length' => $line_max, 'line-break-chars' => $this->LE);
  1662. $s = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $params);
  1663. fputs($fp, $string);
  1664. rewind($fp);
  1665. $out = stream_get_contents($fp);
  1666. stream_filter_remove($s);
  1667. $out = preg_replace('/^\./m', '=2E', $out); //Encode . if it is first char on a line, workaround for bug in Exchange
  1668. fclose($fp);
  1669. return $out;
  1670. }
  1671. /**
  1672. * Encode string to q encoding.
  1673. * @link http://tools.ietf.org/html/rfc2047
  1674. * @param string $str the text to encode
  1675. * @param string $position Where the text is going to be used, see the RFC for what that means
  1676. * @access public
  1677. * @return string
  1678. */
  1679. public function EncodeQ($str, $position = 'text') {
  1680. // There should not be any EOL in the string
  1681. $encoded = preg_replace('/[\r\n]*/', '', $str);
  1682. switch (strtolower($position)) {
  1683. case 'phrase':
  1684. $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1685. break;
  1686. case 'comment':
  1687. $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1688. case 'text':
  1689. default:
  1690. // Replace every high ascii, control =, ? and _ characters
  1691. //TODO using /e (equivalent to eval()) is probably not a good idea
  1692. $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  1693. "'='.sprintf('%02X', ord(stripslashes('\\1')))", $encoded);
  1694. break;
  1695. }
  1696. // Replace every spaces to _ (more readable than =20)
  1697. $encoded = str_replace(' ', '_', $encoded);
  1698. return $encoded;
  1699. }
  1700. /**
  1701. * Adds a string or binary attachment (non-filesystem) to the list.
  1702. * This method can be used to attach ascii or binary data,
  1703. * such as a BLOB record from a database.
  1704. * @param string $string String attachment data.
  1705. * @param string $filename Name of the attachment.
  1706. * @param string $encoding File encoding (see $Encoding).
  1707. * @param string $type File extension (MIME) type.
  1708. * @return void
  1709. */
  1710. public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
  1711. // Append to $attachment array
  1712. $this->attachment[] = array(
  1713. 0 => $string,
  1714. 1 => $filename,
  1715. 2 => basename($filename),
  1716. 3 => $encoding,
  1717. 4 => $type,
  1718. 5 => true, // isStringAttachment
  1719. 6 => 'attachment',
  1720. 7 => 0
  1721. );
  1722. }
  1723. /**
  1724. * Adds an embedded attachment. This can include images, sounds, and
  1725. * just about any other document. Make sure to set the $type to an
  1726. * image type. For JPEG images use "image/jpeg" and for GIF images
  1727. * use "image/gif".
  1728. * @param string $path Path to the attachment.
  1729. * @param string $cid Content ID of the attachment. Use this to identify
  1730. * the Id for accessing the image in an HTML form.
  1731. * @param string $name Overrides the attachment name.
  1732. * @param string $encoding File encoding (see $Encoding).
  1733. * @param string $type File extension (MIME) type.
  1734. * @return bool
  1735. */
  1736. public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1737. if ( !@is_file($path) ) {
  1738. $this->SetError($this->Lang('file_access') . $path);
  1739. return false;
  1740. }
  1741. $filename = basename($path);
  1742. if ( $name == '' ) {
  1743. $name = $filename;
  1744. }
  1745. // Append to $attachment array
  1746. $this->attachment[] = array(
  1747. 0 => $path,
  1748. 1 => $filename,
  1749. 2 => $name,
  1750. 3 => $encoding,
  1751. 4 => $type,
  1752. 5 => false, // isStringAttachment
  1753. 6 => 'inline',
  1754. 7 => $cid
  1755. );
  1756. return true;
  1757. }
  1758. public function AddStringEmbeddedImage($string, $cid, $filename = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1759. // Append to $attachment array
  1760. $this->attachment[] = array(
  1761. 0 => $string,
  1762. 1 => $filename,
  1763. 2 => basename($filename),
  1764. 3 => $encoding,
  1765. 4 => $type,
  1766. 5 => true, // isStringAttachment
  1767. 6 => 'inline',
  1768. 7 => $cid
  1769. );
  1770. }
  1771. /**
  1772. * Returns true if an inline attachment is present.
  1773. * @access public
  1774. * @return bool
  1775. */
  1776. public function InlineImageExists() {
  1777. foreach($this->attachment as $attachment) {
  1778. if ($attachment[6] == 'inline') {
  1779. return true;
  1780. }
  1781. }
  1782. return false;
  1783. }
  1784. public function AttachmentExists() {
  1785. foreach($this->attachment as $attachment) {
  1786. if ($attachment[6] == 'attachment') {
  1787. return true;
  1788. }
  1789. }
  1790. return false;
  1791. }
  1792. public function AlternativeExists() {
  1793. return strlen($this->AltBody)>0;
  1794. }
  1795. /////////////////////////////////////////////////
  1796. // CLASS METHODS, MESSAGE RESET
  1797. /////////////////////////////////////////////////
  1798. /**
  1799. * Clears all recipients assigned in the TO array. Returns void.
  1800. * @return void
  1801. */
  1802. public function ClearAddresses() {
  1803. foreach($this->to as $to) {
  1804. unset($this->all_recipients[strtolower($to[0])]);
  1805. }
  1806. $this->to = array();
  1807. }
  1808. /**
  1809. * Clears all recipients assigned in the CC array. Returns void.
  1810. * @return void
  1811. */
  1812. public function ClearCCs() {
  1813. foreach($this->cc as $cc) {
  1814. unset($this->all_recipients[strtolower($cc[0])]);
  1815. }
  1816. $this->cc = array();
  1817. }
  1818. /**
  1819. * Clears all recipients assigned in the BCC array. Returns void.
  1820. * @return void
  1821. */
  1822. public function ClearBCCs() {
  1823. foreach($this->bcc as $bcc) {
  1824. unset($this->all_recipients[strtolower($bcc[0])]);
  1825. }
  1826. $this->bcc = array();
  1827. }
  1828. /**
  1829. * Clears all recipients assigned in the ReplyTo array. Returns void.
  1830. * @return void
  1831. */
  1832. public function ClearReplyTos() {
  1833. $this->ReplyTo = array();
  1834. }
  1835. /**
  1836. * Clears all recipients assigned in the TO, CC and BCC
  1837. * array. Returns void.
  1838. * @return void
  1839. */
  1840. public function ClearAllRecipients() {
  1841. $this->to = array();
  1842. $this->cc = array();
  1843. $this->bcc = array();
  1844. $this->all_recipients = array();
  1845. }
  1846. /**
  1847. * Clears all previously set filesystem, string, and binary
  1848. * attachments. Returns void.
  1849. * @return void
  1850. */
  1851. public function ClearAttachments() {
  1852. $this->attachment = array();
  1853. }
  1854. /**
  1855. * Clears all custom headers. Returns void.
  1856. * @return void
  1857. */
  1858. public function ClearCustomHeaders() {
  1859. $this->CustomHeader = array();
  1860. }
  1861. /////////////////////////////////////////////////
  1862. // CLASS METHODS, MISCELLANEOUS
  1863. /////////////////////////////////////////////////
  1864. /**
  1865. * Adds the error message to the error container.
  1866. * @access protected
  1867. * @return void
  1868. */
  1869. protected function SetError($msg) {
  1870. $this->error_count++;
  1871. if ($this->Mailer == 'smtp' and !is_null($this->smtp)) {
  1872. $lasterror = $this->smtp->getError();
  1873. if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) {
  1874. $msg .= '<p>' . $this->Lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n";
  1875. }
  1876. }
  1877. $this->ErrorInfo = $msg;
  1878. }
  1879. /**
  1880. * Returns the proper RFC 822 formatted date.
  1881. * @access public
  1882. * @return string
  1883. * @static
  1884. */
  1885. public static function RFCDate() {
  1886. $tz = date('Z');
  1887. $tzs = ($tz < 0) ? '-' : '+';
  1888. $tz = abs($tz);
  1889. $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
  1890. $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
  1891. return $result;
  1892. }
  1893. /**
  1894. * Returns the server hostname or 'localhost.localdomain' if unknown.
  1895. * @access protected
  1896. * @return string
  1897. */
  1898. protected function ServerHostname() {
  1899. if (!empty($this->Hostname)) {
  1900. $result = $this->Hostname;
  1901. } elseif (isset($_SERVER['SERVER_NAME'])) {
  1902. $result = $_SERVER['SERVER_NAME'];
  1903. } else {
  1904. $result = 'localhost.localdomain';
  1905. }
  1906. return $result;
  1907. }
  1908. /**
  1909. * Returns a message in the appropriate language.
  1910. * @access protected
  1911. * @return string
  1912. */
  1913. protected function Lang($key) {
  1914. if(count($this->language) < 1) {
  1915. $this->SetLanguage('en'); // set the default language
  1916. }
  1917. if(isset($this->language[$key])) {
  1918. return $this->language[$key];
  1919. } else {
  1920. return 'Language string failed to load: ' . $key;
  1921. }
  1922. }
  1923. /**
  1924. * Returns true if an error occurred.
  1925. * @access public
  1926. * @return bool
  1927. */
  1928. public function IsError() {
  1929. return ($this->error_count > 0);
  1930. }
  1931. /**
  1932. * Changes every end of line from CR or LF to CRLF.
  1933. * @access public
  1934. * @return string
  1935. */
  1936. public function FixEOL($str) {
  1937. $str = str_replace("\r\n", "\n", $str);
  1938. $str = str_replace("\r", "\n", $str);
  1939. $str = str_replace("\n", $this->LE, $str);
  1940. return $str;
  1941. }
  1942. /**
  1943. * Adds a custom header.
  1944. * @access public
  1945. * @return void
  1946. */
  1947. public function AddCustomHeader($custom_header) {
  1948. $this->CustomHeader[] = explode(':', $custom_header, 2);
  1949. }
  1950. /**
  1951. * Evaluates the message and returns modifications for inline images and backgrounds
  1952. * @access public
  1953. * @return $message
  1954. */
  1955. public function MsgHTML($message, $basedir = '') {
  1956. preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
  1957. if(isset($images[2])) {
  1958. foreach($images[2] as $i => $url) {
  1959. // do not change urls for absolute images (thanks to corvuscorax)
  1960. if (!preg_match('#^[A-z]+://#', $url)) {
  1961. $filename = basename($url);
  1962. $directory = dirname($url);
  1963. ($directory == '.') ? $directory='': '';
  1964. $cid = 'cid:' . md5($filename);
  1965. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  1966. $mimeType = self::_mime_types($ext);
  1967. if ( strlen($basedir) > 1 && substr($basedir, -1) != '/') { $basedir .= '/'; }
  1968. if ( strlen($directory) > 1 && substr($directory, -1) != '/') { $directory .= '/'; }
  1969. if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType) ) {
  1970. $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
  1971. }
  1972. }
  1973. }
  1974. }
  1975. $this->IsHTML(true);
  1976. $this->Body = $message;
  1977. $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $message)));
  1978. if (!empty($textMsg) && empty($this->AltBody)) {
  1979. $this->AltBody = html_entity_decode($textMsg);
  1980. }
  1981. if (empty($this->AltBody)) {
  1982. $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
  1983. }
  1984. }
  1985. /**
  1986. * Gets the MIME type of the embedded or inline image
  1987. * @param string File extension
  1988. * @access public
  1989. * @return string MIME type of ext
  1990. * @static
  1991. */
  1992. public static function _mime_types($ext = '') {
  1993. $mimes = array(
  1994. 'hqx' => 'application/mac-binhex40',
  1995. 'cpt' => 'application/mac-compactpro',
  1996. 'doc' => 'application/msword',
  1997. 'bin' => 'application/macbinary',
  1998. 'dms' => 'application/octet-stream',
  1999. 'lha' => 'application/octet-stream',
  2000. 'lzh' => 'application/octet-stream',
  2001. 'exe' => 'application/octet-stream',
  2002. 'class' => 'application/octet-stream',
  2003. 'psd' => 'application/octet-stream',
  2004. 'so' => 'application/octet-stream',
  2005. 'sea' => 'application/octet-stream',
  2006. 'dll' => 'application/octet-stream',
  2007. 'oda' => 'application/oda',
  2008. 'pdf' => 'application/pdf',
  2009. 'ai' => 'application/postscript',
  2010. 'eps' => 'application/postscript',
  2011. 'ps' => 'application/postscript',
  2012. 'smi' => 'application/smil',
  2013. 'smil' => 'application/smil',
  2014. 'mif' => 'application/vnd.mif',
  2015. 'xls' => 'application/vnd.ms-excel',
  2016. 'ppt' => 'application/vnd.ms-powerpoint',
  2017. 'wbxml' => 'application/vnd.wap.wbxml',
  2018. 'wmlc' => 'application/vnd.wap.wmlc',
  2019. 'dcr' => 'application/x-director',
  2020. 'dir' => 'application/x-director',
  2021. 'dxr' => 'application/x-director',
  2022. 'dvi' => 'application/x-dvi',
  2023. 'gtar' => 'application/x-gtar',
  2024. 'php' => 'application/x-httpd-php',
  2025. 'php4' => 'application/x-httpd-php',
  2026. 'php3' => 'application/x-httpd-php',
  2027. 'phtml' => 'application/x-httpd-php',
  2028. 'phps' => 'application/x-httpd-php-source',
  2029. 'js' => 'application/x-javascript',
  2030. 'swf' => 'application/x-shockwave-flash',
  2031. 'sit' => 'application/x-stuffit',
  2032. 'tar' => 'application/x-tar',
  2033. 'tgz' => 'application/x-tar',
  2034. 'xhtml' => 'application/xhtml+xml',
  2035. 'xht' => 'application/xhtml+xml',
  2036. 'zip' => 'application/zip',
  2037. 'mid' => 'audio/midi',
  2038. 'midi' => 'audio/midi',
  2039. 'mpga' => 'audio/mpeg',
  2040. 'mp2' => 'audio/mpeg',
  2041. 'mp3' => 'audio/mpeg',
  2042. 'aif' => 'audio/x-aiff',
  2043. 'aiff' => 'audio/x-aiff',
  2044. 'aifc' => 'audio/x-aiff',
  2045. 'ram' => 'audio/x-pn-realaudio',
  2046. 'rm' => 'audio/x-pn-realaudio',
  2047. 'rpm' => 'audio/x-pn-realaudio-plugin',
  2048. 'ra' => 'audio/x-realaudio',
  2049. 'rv' => 'video/vnd.rn-realvideo',
  2050. 'wav' => 'audio/x-wav',
  2051. 'bmp' => 'image/bmp',
  2052. 'gif' => 'image/gif',
  2053. 'jpeg' => 'image/jpeg',
  2054. 'jpg' => 'image/jpeg',
  2055. 'jpe' => 'image/jpeg',
  2056. 'png' => 'image/png',
  2057. 'tiff' => 'image/tiff',
  2058. 'tif' => 'image/tiff',
  2059. 'css' => 'text/css',
  2060. 'html' => 'text/html',
  2061. 'htm' => 'text/html',
  2062. 'shtml' => 'text/html',
  2063. 'txt' => 'text/plain',
  2064. 'text' => 'text/plain',
  2065. 'log' => 'text/plain',
  2066. 'rtx' => 'text/richtext',
  2067. 'rtf' => 'text/rtf',
  2068. 'xml' => 'text/xml',
  2069. 'xsl' => 'text/xml',
  2070. 'mpeg' => 'video/mpeg',
  2071. 'mpg' => 'video/mpeg',
  2072. 'mpe' => 'video/mpeg',
  2073. 'qt' => 'video/quicktime',
  2074. 'mov' => 'video/quicktime',
  2075. 'avi' => 'video/x-msvideo',
  2076. 'movie' => 'video/x-sgi-movie',
  2077. 'doc' => 'application/msword',
  2078. 'word' => 'application/msword',
  2079. 'xl' => 'application/excel',
  2080. 'eml' => 'message/rfc822'
  2081. );
  2082. return (!isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
  2083. }
  2084. /**
  2085. * Set (or reset) Class Objects (variables)
  2086. *
  2087. * Usage Example:
  2088. * $page->set('X-Priority', '3');
  2089. *
  2090. * @access public
  2091. * @param string $name Parameter Name
  2092. * @param mixed $value Parameter Value
  2093. * NOTE: will not work with arrays, there are no arrays to set/reset
  2094. * @todo Should this not be using __set() magic function?
  2095. */
  2096. public function set($name, $value = '') {
  2097. try {
  2098. if (isset($this->$name) ) {
  2099. $this->$name = $value;
  2100. } else {
  2101. throw new phpmailerException($this->Lang('variable_set') . $name, self::STOP_CRITICAL);
  2102. }
  2103. } catch (Exception $e) {
  2104. $this->SetError($e->getMessage());
  2105. if ($e->getCode() == self::STOP_CRITICAL) {
  2106. return false;
  2107. }
  2108. }
  2109. return true;
  2110. }
  2111. /**
  2112. * Strips newlines to prevent header injection.
  2113. * @access public
  2114. * @param string $str String
  2115. * @return string
  2116. */
  2117. public function SecureHeader($str) {
  2118. $str = str_replace("\r", '', $str);
  2119. $str = str_replace("\n", '', $str);
  2120. return trim($str);
  2121. }
  2122. /**
  2123. * Set the private key file and password to sign the message.
  2124. *
  2125. * @access public
  2126. * @param string $key_filename Parameter File Name
  2127. * @param string $key_pass Password for private key
  2128. */
  2129. public function Sign($cert_filename, $key_filename, $key_pass) {
  2130. $this->sign_cert_file = $cert_filename;
  2131. $this->sign_key_file = $key_filename;
  2132. $this->sign_key_pass = $key_pass;
  2133. }
  2134. /**
  2135. * Set the private key file and password to sign the message.
  2136. *
  2137. * @access public
  2138. * @param string $key_filename Parameter File Name
  2139. * @param string $key_pass Password for private key
  2140. */
  2141. public function DKIM_QP($txt) {
  2142. $tmp = '';
  2143. $line = '';
  2144. for ($i = 0; $i < strlen($txt); $i++) {
  2145. $ord = ord($txt[$i]);
  2146. if ( ((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E)) ) {
  2147. $line .= $txt[$i];
  2148. } else {
  2149. $line .= "=".sprintf("%02X", $ord);
  2150. }
  2151. }
  2152. return $line;
  2153. }
  2154. /**
  2155. * Generate DKIM signature
  2156. *
  2157. * @access public
  2158. * @param string $s Header
  2159. */
  2160. public function DKIM_Sign($s) {
  2161. $privKeyStr = file_get_contents($this->DKIM_private);
  2162. if ($this->DKIM_passphrase != '') {
  2163. $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
  2164. } else {
  2165. $privKey = $privKeyStr;
  2166. }
  2167. if (openssl_sign($s, $signature, $privKey)) {
  2168. return base64_encode($signature);
  2169. }
  2170. }
  2171. /**
  2172. * Generate DKIM Canonicalization Header
  2173. *
  2174. * @access public
  2175. * @param string $s Header
  2176. */
  2177. public function DKIM_HeaderC($s) {
  2178. $s = preg_replace("/\r\n\s+/", " ", $s);
  2179. $lines = explode("\r\n", $s);
  2180. foreach ($lines as $key => $line) {
  2181. list($heading, $value) = explode(":", $line, 2);
  2182. $heading = strtolower($heading);
  2183. $value = preg_replace("/\s+/", " ", $value) ; // Compress useless spaces
  2184. $lines[$key] = $heading.":".trim($value) ; // Don't forget to remove WSP around the value
  2185. }
  2186. $s = implode("\r\n", $lines);
  2187. return $s;
  2188. }
  2189. /**
  2190. * Generate DKIM Canonicalization Body
  2191. *
  2192. * @access public
  2193. * @param string $body Message Body
  2194. */
  2195. public function DKIM_BodyC($body) {
  2196. if ($body == '') return "\r\n";
  2197. // stabilize line endings
  2198. $body = str_replace("\r\n", "\n", $body);
  2199. $body = str_replace("\n", "\r\n", $body);
  2200. // END stabilize line endings
  2201. while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") {
  2202. $body = substr($body, 0, strlen($body) - 2);
  2203. }
  2204. return $body;
  2205. }
  2206. /**
  2207. * Create the DKIM header, body, as new header
  2208. *
  2209. * @access public
  2210. * @param string $headers_line Header lines
  2211. * @param string $subject Subject
  2212. * @param string $body Body
  2213. */
  2214. public function DKIM_Add($headers_line, $subject, $body) {
  2215. $DKIMsignatureType = 'rsa-sha1'; // Signature & hash algorithms
  2216. $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
  2217. $DKIMquery = 'dns/txt'; // Query method
  2218. $DKIMtime = time() ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
  2219. $subject_header = "Subject: $subject";
  2220. $headers = explode($this->LE, $headers_line);
  2221. foreach($headers as $header) {
  2222. if (strpos($header, 'From:') === 0) {
  2223. $from_header = $header;
  2224. } elseif (strpos($header, 'To:') === 0) {
  2225. $to_header = $header;
  2226. }
  2227. }
  2228. $from = str_replace('|', '=7C', $this->DKIM_QP($from_header));
  2229. $to = str_replace('|', '=7C', $this->DKIM_QP($to_header));
  2230. $subject = str_replace('|', '=7C', $this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
  2231. $body = $this->DKIM_BodyC($body);
  2232. $DKIMlen = strlen($body) ; // Length of body
  2233. $DKIMb64 = base64_encode(pack("H*", sha1($body))) ; // Base64 of packed binary SHA-1 hash of body
  2234. $ident = ($this->DKIM_identity == '')? '' : " i=" . $this->DKIM_identity . ";";
  2235. $dkimhdrs = "DKIM-Signature: v=1; a=" . $DKIMsignatureType . "; q=" . $DKIMquery . "; l=" . $DKIMlen . "; s=" . $this->DKIM_selector . ";\r\n".
  2236. "\tt=" . $DKIMtime . "; c=" . $DKIMcanonicalization . ";\r\n".
  2237. "\th=From:To:Subject;\r\n".
  2238. "\td=" . $this->DKIM_domain . ";" . $ident . "\r\n".
  2239. "\tz=$from\r\n".
  2240. "\t|$to\r\n".
  2241. "\t|$subject;\r\n".
  2242. "\tbh=" . $DKIMb64 . ";\r\n".
  2243. "\tb=";
  2244. $toSign = $this->DKIM_HeaderC($from_header . "\r\n" . $to_header . "\r\n" . $subject_header . "\r\n" . $dkimhdrs);
  2245. $signed = $this->DKIM_Sign($toSign);
  2246. return "X-PHPMAILER-DKIM: phpmailer.worxware.com\r\n".$dkimhdrs.$signed."\r\n";
  2247. }
  2248. protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body) {
  2249. if (!empty($this->action_function) && function_exists($this->action_function)) {
  2250. $params = array($isSent, $to, $cc, $bcc, $subject, $body);
  2251. call_user_func_array($this->action_function, $params);
  2252. }
  2253. }
  2254. }
  2255. class phpmailerException extends Exception {
  2256. public function errorMessage() {
  2257. $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
  2258. return $errorMsg;
  2259. }
  2260. }