新版订单消耗系统

ClassPhpMailer.php 99KB

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