class.phpmailer.php 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  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) [email protected] |
  10. | : Marcus Bointon (coolbru) [email protected] |
  11. | : Jim Jagielski (jimjag) [email protected] |
  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 '[email protected]' 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)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)((?>(?>(?>((?>(?>(?>\x0D\x0A)?[ ])+|(?>[ ]*\x0D\x0A)?[ ]+)?)(\((?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\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][:\]]){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);
  664. }
  665. /////////////////////////////////////////////////
  666. // METHODS, MAIL SENDING
  667. /////////////////////////////////////////////////
  668. /**
  669. * Creates message and assigns Mailer. If the message is
  670. * not sent successfully then it returns false. Use the ErrorInfo
  671. * variable to view description of the error.
  672. * @throws phpmailerException
  673. * @return bool
  674. */
  675. public function Send() {
  676. try {
  677. if(!$this->PreSend()) return false;
  678. return $this->PostSend();
  679. } catch (phpmailerException $e) {
  680. $this->mailHeader = '';
  681. $this->SetError($e->getMessage());
  682. if ($this->exceptions) {
  683. throw $e;
  684. }
  685. return false;
  686. }
  687. }
  688. /**
  689. * Prep mail by constructing all message entities
  690. * @throws phpmailerException
  691. * @return bool
  692. */
  693. public function PreSend() {
  694. try {
  695. $this->mailHeader = "";
  696. if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  697. throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
  698. }
  699. // Set whether the message is multipart/alternative
  700. if(!empty($this->AltBody)) {
  701. $this->ContentType = 'multipart/alternative';
  702. }
  703. $this->error_count = 0; // reset errors
  704. $this->SetMessageType();
  705. //Refuse to send an empty message
  706. if (empty($this->Body)) {
  707. throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
  708. }
  709. $this->MIMEHeader = $this->CreateHeader();
  710. $this->MIMEBody = $this->CreateBody();
  711. // To capture the complete message when using mail(), create
  712. // an extra header list which CreateHeader() doesn't fold in
  713. if ($this->Mailer == 'mail') {
  714. if (count($this->to) > 0) {
  715. $this->mailHeader .= $this->AddrAppend("To", $this->to);
  716. } else {
  717. $this->mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
  718. }
  719. $this->mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
  720. // if(count($this->cc) > 0) {
  721. // $this->mailHeader .= $this->AddrAppend("Cc", $this->cc);
  722. // }
  723. }
  724. // digitally sign with DKIM if enabled
  725. if (!empty($this->DKIM_domain) && !empty($this->DKIM_private) && !empty($this->DKIM_selector) && !empty($this->DKIM_domain) && file_exists($this->DKIM_private)) {
  726. $header_dkim = $this->DKIM_Add($this->MIMEHeader, $this->EncodeHeader($this->SecureHeader($this->Subject)), $this->MIMEBody);
  727. $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
  728. }
  729. return true;
  730. } catch (phpmailerException $e) {
  731. $this->SetError($e->getMessage());
  732. if ($this->exceptions) {
  733. throw $e;
  734. }
  735. return false;
  736. }
  737. }
  738. /**
  739. * Actual Email transport function
  740. * Send the email via the selected mechanism
  741. * @throws phpmailerException
  742. * @return bool
  743. */
  744. public function PostSend() {
  745. try {
  746. // Choose the mailer and send through it
  747. switch($this->Mailer) {
  748. case 'sendmail':
  749. return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
  750. case 'smtp':
  751. return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
  752. case 'mail':
  753. return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
  754. default:
  755. return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
  756. }
  757. } catch (phpmailerException $e) {
  758. $this->SetError($e->getMessage());
  759. if ($this->exceptions) {
  760. throw $e;
  761. }
  762. if ($this->SMTPDebug) {
  763. $this->edebug($e->getMessage()."\n");
  764. }
  765. }
  766. return false;
  767. }
  768. /**
  769. * Sends mail using the $Sendmail program.
  770. * @param string $header The message headers
  771. * @param string $body The message body
  772. * @throws phpmailerException
  773. * @access protected
  774. * @return bool
  775. */
  776. protected function SendmailSend($header, $body) {
  777. if ($this->Sender != '') {
  778. $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  779. } else {
  780. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  781. }
  782. if ($this->SingleTo === true) {
  783. foreach ($this->SingleToArray as $val) {
  784. if(!@$mail = popen($sendmail, 'w')) {
  785. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  786. }
  787. fputs($mail, "To: " . $val . "\n");
  788. fputs($mail, $header);
  789. fputs($mail, $body);
  790. $result = pclose($mail);
  791. // implement call back function if it exists
  792. $isSent = ($result == 0) ? 1 : 0;
  793. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  794. if($result != 0) {
  795. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  796. }
  797. }
  798. } else {
  799. if(!@$mail = popen($sendmail, 'w')) {
  800. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  801. }
  802. fputs($mail, $header);
  803. fputs($mail, $body);
  804. $result = pclose($mail);
  805. // implement call back function if it exists
  806. $isSent = ($result == 0) ? 1 : 0;
  807. $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body);
  808. if($result != 0) {
  809. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  810. }
  811. }
  812. return true;
  813. }
  814. /**
  815. * Sends mail using the PHP mail() function.
  816. * @param string $header The message headers
  817. * @param string $body The message body
  818. * @throws phpmailerException
  819. * @access protected
  820. * @return bool
  821. */
  822. protected function MailSend($header, $body) {
  823. $toArr = array();
  824. foreach($this->to as $t) {
  825. $toArr[] = $this->AddrFormat($t);
  826. }
  827. $to = implode(', ', $toArr);
  828. if (empty($this->Sender)) {
  829. $params = "-oi ";
  830. } else {
  831. $params = sprintf("-oi -f%s", $this->Sender);
  832. }
  833. if ($this->Sender != '' and !ini_get('safe_mode')) {
  834. $old_from = ini_get('sendmail_from');
  835. ini_set('sendmail_from', $this->Sender);
  836. }
  837. $rt = false;
  838. if ($this->SingleTo === true && count($toArr) > 1) {
  839. foreach ($toArr as $val) {
  840. $rt = $this->mail_passthru($val, $this->Subject, $body, $header, $params);
  841. // implement call back function if it exists
  842. $isSent = ($rt == 1) ? 1 : 0;
  843. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  844. }
  845. } else {
  846. $rt = $this->mail_passthru($to, $this->Subject, $body, $header, $params);
  847. // implement call back function if it exists
  848. $isSent = ($rt == 1) ? 1 : 0;
  849. $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
  850. }
  851. if (isset($old_from)) {
  852. ini_set('sendmail_from', $old_from);
  853. }
  854. if(!$rt) {
  855. throw new phpmailerException($this->Lang('instantiate'), self::STOP_CRITICAL);
  856. }
  857. return true;
  858. }
  859. /**
  860. * Sends mail via SMTP using PhpSMTP
  861. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
  862. * @param string $header The message headers
  863. * @param string $body The message body
  864. * @throws phpmailerException
  865. * @uses SMTP
  866. * @access protected
  867. * @return bool
  868. */
  869. protected function SmtpSend($header, $body) {
  870. require_once $this->PluginDir . 'class.smtp.php';
  871. $bad_rcpt = array();
  872. if(!$this->SmtpConnect()) {
  873. throw new phpmailerException($this->Lang('smtp_connect_failed'), self::STOP_CRITICAL);
  874. }
  875. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  876. if(!$this->smtp->Mail($smtp_from)) {
  877. throw new phpmailerException($this->Lang('from_failed') . $smtp_from, self::STOP_CRITICAL);
  878. }
  879. // Attempt to send attach all recipients
  880. foreach($this->to as $to) {
  881. if (!$this->smtp->Recipient($to[0])) {
  882. $bad_rcpt[] = $to[0];
  883. // implement call back function if it exists
  884. $isSent = 0;
  885. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
  886. } else {
  887. // implement call back function if it exists
  888. $isSent = 1;
  889. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
  890. }
  891. }
  892. foreach($this->cc as $cc) {
  893. if (!$this->smtp->Recipient($cc[0])) {
  894. $bad_rcpt[] = $cc[0];
  895. // implement call back function if it exists
  896. $isSent = 0;
  897. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
  898. } else {
  899. // implement call back function if it exists
  900. $isSent = 1;
  901. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
  902. }
  903. }
  904. foreach($this->bcc as $bcc) {
  905. if (!$this->smtp->Recipient($bcc[0])) {
  906. $bad_rcpt[] = $bcc[0];
  907. // implement call back function if it exists
  908. $isSent = 0;
  909. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
  910. } else {
  911. // implement call back function if it exists
  912. $isSent = 1;
  913. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
  914. }
  915. }
  916. if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses
  917. $badaddresses = implode(', ', $bad_rcpt);
  918. throw new phpmailerException($this->Lang('recipients_failed') . $badaddresses);
  919. }
  920. if(!$this->smtp->Data($header . $body)) {
  921. throw new phpmailerException($this->Lang('data_not_accepted'), self::STOP_CRITICAL);
  922. }
  923. if($this->SMTPKeepAlive == true) {
  924. $this->smtp->Reset();
  925. } else {
  926. $this->smtp->Quit();
  927. $this->smtp->Close();
  928. }
  929. return true;
  930. }
  931. /**
  932. * Initiates a connection to an SMTP server.
  933. * Returns false if the operation failed.
  934. * @uses SMTP
  935. * @access public
  936. * @throws phpmailerException
  937. * @return bool
  938. */
  939. public function SmtpConnect() {
  940. if(is_null($this->smtp)) {
  941. $this->smtp = new SMTP;
  942. }
  943. $this->smtp->Timeout = $this->Timeout;
  944. $this->smtp->do_debug = $this->SMTPDebug;
  945. $hosts = explode(';', $this->Host);
  946. $index = 0;
  947. $connection = $this->smtp->Connected();
  948. // Retry while there is no connection
  949. try {
  950. while($index < count($hosts) && !$connection) {
  951. $hostinfo = array();
  952. if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
  953. $host = $hostinfo[1];
  954. $port = $hostinfo[2];
  955. } else {
  956. $host = $hosts[$index];
  957. $port = $this->Port;
  958. }
  959. $tls = ($this->SMTPSecure == 'tls');
  960. $ssl = ($this->SMTPSecure == 'ssl');
  961. if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
  962. $hello = ($this->Helo != '' ? $this->Helo : $this->ServerHostname());
  963. $this->smtp->Hello($hello);
  964. if ($tls) {
  965. if (!$this->smtp->StartTLS()) {
  966. throw new phpmailerException($this->Lang('tls'));
  967. }
  968. //We must resend HELO after tls negotiation
  969. $this->smtp->Hello($hello);
  970. }
  971. $connection = true;
  972. if ($this->SMTPAuth) {
  973. if (!$this->smtp->Authenticate($this->Username, $this->Password, $this->AuthType,
  974. $this->Realm, $this->Workstation)) {
  975. throw new phpmailerException($this->Lang('authenticate'));
  976. }
  977. }
  978. }
  979. $index++;
  980. if (!$connection) {
  981. throw new phpmailerException($this->Lang('connect_host'));
  982. }
  983. }
  984. } catch (phpmailerException $e) {
  985. $this->smtp->Reset();
  986. if ($this->exceptions) {
  987. throw $e;
  988. }
  989. }
  990. return true;
  991. }
  992. /**
  993. * Closes the active SMTP session if one exists.
  994. * @return void
  995. */
  996. public function SmtpClose() {
  997. if ($this->smtp !== null) {
  998. if($this->smtp->Connected()) {
  999. $this->smtp->Quit();
  1000. $this->smtp->Close();
  1001. }
  1002. }
  1003. }
  1004. /**
  1005. * Sets the language for all class error messages.
  1006. * Returns false if it cannot load the language file. The default language is English.
  1007. * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
  1008. * @param string $lang_path Path to the language file directory
  1009. * @return bool
  1010. * @access public
  1011. */
  1012. function SetLanguage($langcode = 'en', $lang_path = 'language/') {
  1013. //Define full set of translatable strings
  1014. $PHPMAILER_LANG = array(
  1015. 'authenticate' => 'SMTP Error: Could not authenticate.',
  1016. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  1017. 'data_not_accepted' => 'SMTP Error: Data not accepted.',
  1018. 'empty_message' => 'Message body empty',
  1019. 'encoding' => 'Unknown encoding: ',
  1020. 'execute' => 'Could not execute: ',
  1021. 'file_access' => 'Could not access file: ',
  1022. 'file_open' => 'File Error: Could not open file: ',
  1023. 'from_failed' => 'The following From address failed: ',
  1024. 'instantiate' => 'Could not instantiate mail function.',
  1025. 'invalid_address' => 'Invalid address',
  1026. 'mailer_not_supported' => ' mailer is not supported.',
  1027. 'provide_address' => 'You must provide at least one recipient email address.',
  1028. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  1029. 'signing' => 'Signing Error: ',
  1030. 'smtp_connect_failed' => 'SMTP Connect() failed.',
  1031. 'smtp_error' => 'SMTP server error: ',
  1032. 'variable_set' => 'Cannot set or reset variable: '
  1033. );
  1034. //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
  1035. $l = true;
  1036. if ($langcode != 'en') { //There is no English translation file
  1037. $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
  1038. }
  1039. $this->language = $PHPMAILER_LANG;
  1040. return ($l == true); //Returns false if language not found
  1041. }
  1042. /**
  1043. * Return the current array of language strings
  1044. * @return array
  1045. */
  1046. public function GetTranslations() {
  1047. return $this->language;
  1048. }
  1049. /////////////////////////////////////////////////
  1050. // METHODS, MESSAGE CREATION
  1051. /////////////////////////////////////////////////
  1052. /**
  1053. * Creates recipient headers.
  1054. * @access public
  1055. * @param string $type
  1056. * @param array $addr
  1057. * @return string
  1058. */
  1059. public function AddrAppend($type, $addr) {
  1060. $addr_str = $type . ': ';
  1061. $addresses = array();
  1062. foreach ($addr as $a) {
  1063. $addresses[] = $this->AddrFormat($a);
  1064. }
  1065. $addr_str .= implode(', ', $addresses);
  1066. $addr_str .= $this->LE;
  1067. return $addr_str;
  1068. }
  1069. /**
  1070. * Formats an address correctly.
  1071. * @access public
  1072. * @param string $addr
  1073. * @return string
  1074. */
  1075. public function AddrFormat($addr) {
  1076. if (empty($addr[1])) {
  1077. return $this->SecureHeader($addr[0]);
  1078. } else {
  1079. return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  1080. }
  1081. }
  1082. /**
  1083. * Wraps message for use with mailers that do not
  1084. * automatically perform wrapping and for quoted-printable.
  1085. * Original written by philippe.
  1086. * @param string $message The message to wrap
  1087. * @param integer $length The line length to wrap to
  1088. * @param boolean $qp_mode Whether to run in Quoted-Printable mode
  1089. * @access public
  1090. * @return string
  1091. */
  1092. public function WrapText($message, $length, $qp_mode = false) {
  1093. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  1094. // If utf-8 encoding is used, we will need to make sure we don't
  1095. // split multibyte characters when we wrap
  1096. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  1097. $lelen = strlen($this->LE);
  1098. $crlflen = strlen(self::CRLF);
  1099. $message = $this->FixEOL($message);
  1100. if (substr($message, -$lelen) == $this->LE) {
  1101. $message = substr($message, 0, -$lelen);
  1102. }
  1103. $line = explode($this->LE, $message); // Magic. We know FixEOL uses $LE
  1104. $message = '';
  1105. for ($i = 0 ;$i < count($line); $i++) {
  1106. $line_part = explode(' ', $line[$i]);
  1107. $buf = '';
  1108. for ($e = 0; $e<count($line_part); $e++) {
  1109. $word = $line_part[$e];
  1110. if ($qp_mode and (strlen($word) > $length)) {
  1111. $space_left = $length - strlen($buf) - $crlflen;
  1112. if ($e != 0) {
  1113. if ($space_left > 20) {
  1114. $len = $space_left;
  1115. if ($is_utf8) {
  1116. $len = $this->UTF8CharBoundary($word, $len);
  1117. } elseif (substr($word, $len - 1, 1) == "=") {
  1118. $len--;
  1119. } elseif (substr($word, $len - 2, 1) == "=") {
  1120. $len -= 2;
  1121. }
  1122. $part = substr($word, 0, $len);
  1123. $word = substr($word, $len);
  1124. $buf .= ' ' . $part;
  1125. $message .= $buf . sprintf("=%s", self::CRLF);
  1126. } else {
  1127. $message .= $buf . $soft_break;
  1128. }
  1129. $buf = '';
  1130. }
  1131. while (strlen($word) > 0) {
  1132. $len = $length;
  1133. if ($is_utf8) {
  1134. $len = $this->UTF8CharBoundary($word, $len);
  1135. } elseif (substr($word, $len - 1, 1) == "=") {
  1136. $len--;
  1137. } elseif (substr($word, $len - 2, 1) == "=") {
  1138. $len -= 2;
  1139. }
  1140. $part = substr($word, 0, $len);
  1141. $word = substr($word, $len);
  1142. if (strlen($word) > 0) {
  1143. $message .= $part . sprintf("=%s", self::CRLF);
  1144. } else {
  1145. $buf = $part;
  1146. }
  1147. }
  1148. } else {
  1149. $buf_o = $buf;
  1150. $buf .= ($e == 0) ? $word : (' ' . $word);
  1151. if (strlen($buf) > $length and $buf_o != '') {
  1152. $message .= $buf_o . $soft_break;
  1153. $buf = $word;
  1154. }
  1155. }
  1156. }
  1157. $message .= $buf . self::CRLF;
  1158. }
  1159. return $message;
  1160. }
  1161. /**
  1162. * Finds last character boundary prior to maxLength in a utf-8
  1163. * quoted (printable) encoded string.
  1164. * Original written by Colin Brown.
  1165. * @access public
  1166. * @param string $encodedText utf-8 QP text
  1167. * @param int $maxLength find last character boundary prior to this length
  1168. * @return int
  1169. */
  1170. public function UTF8CharBoundary($encodedText, $maxLength) {
  1171. $foundSplitPos = false;
  1172. $lookBack = 3;
  1173. while (!$foundSplitPos) {
  1174. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  1175. $encodedCharPos = strpos($lastChunk, "=");
  1176. if ($encodedCharPos !== false) {
  1177. // Found start of encoded character byte within $lookBack block.
  1178. // Check the encoded byte value (the 2 chars after the '=')
  1179. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  1180. $dec = hexdec($hex);
  1181. if ($dec < 128) { // Single byte character.
  1182. // If the encoded char was found at pos 0, it will fit
  1183. // otherwise reduce maxLength to start of the encoded char
  1184. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  1185. $maxLength - ($lookBack - $encodedCharPos);
  1186. $foundSplitPos = true;
  1187. } elseif ($dec >= 192) { // First byte of a multi byte character
  1188. // Reduce maxLength to split at start of character
  1189. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  1190. $foundSplitPos = true;
  1191. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  1192. $lookBack += 3;
  1193. }
  1194. } else {
  1195. // No encoded character found
  1196. $foundSplitPos = true;
  1197. }
  1198. }
  1199. return $maxLength;
  1200. }
  1201. /**
  1202. * Set the body wrapping.
  1203. * @access public
  1204. * @return void
  1205. */
  1206. public function SetWordWrap() {
  1207. if($this->WordWrap < 1) {
  1208. return;
  1209. }
  1210. switch($this->message_type) {
  1211. case 'alt':
  1212. case 'alt_inline':
  1213. case 'alt_attach':
  1214. case 'alt_inline_attach':
  1215. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  1216. break;
  1217. default:
  1218. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  1219. break;
  1220. }
  1221. }
  1222. /**
  1223. * Assembles message header.
  1224. * @access public
  1225. * @return string The assembled header
  1226. */
  1227. public function CreateHeader() {
  1228. $result = '';
  1229. // Set the boundaries
  1230. $uniq_id = md5(uniqid(time()));
  1231. $this->boundary[1] = 'b1_' . $uniq_id;
  1232. $this->boundary[2] = 'b2_' . $uniq_id;
  1233. $this->boundary[3] = 'b3_' . $uniq_id;
  1234. if ($this->MessageDate == '') {
  1235. $result .= $this->HeaderLine('Date', self::RFCDate());
  1236. } else {
  1237. $result .= $this->HeaderLine('Date', $this->MessageDate);
  1238. }
  1239. if ($this->ReturnPath) {
  1240. $result .= $this->HeaderLine('Return-Path', trim($this->ReturnPath));
  1241. } elseif ($this->Sender == '') {
  1242. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  1243. } else {
  1244. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  1245. }
  1246. // To be created automatically by mail()
  1247. if($this->Mailer != 'mail') {
  1248. if ($this->SingleTo === true) {
  1249. foreach($this->to as $t) {
  1250. $this->SingleToArray[] = $this->AddrFormat($t);
  1251. }
  1252. } else {
  1253. if(count($this->to) > 0) {
  1254. $result .= $this->AddrAppend('To', $this->to);
  1255. } elseif (count($this->cc) == 0) {
  1256. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  1257. }
  1258. }
  1259. }
  1260. $from = array();
  1261. $from[0][0] = trim($this->From);
  1262. $from[0][1] = $this->FromName;
  1263. $result .= $this->AddrAppend('From', $from);
  1264. // sendmail and mail() extract Cc from the header before sending
  1265. if(count($this->cc) > 0) {
  1266. $result .= $this->AddrAppend('Cc', $this->cc);
  1267. }
  1268. // sendmail and mail() extract Bcc from the header before sending
  1269. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  1270. $result .= $this->AddrAppend('Bcc', $this->bcc);
  1271. }
  1272. if(count($this->ReplyTo) > 0) {
  1273. $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
  1274. }
  1275. // mail() sets the subject itself
  1276. if($this->Mailer != 'mail') {
  1277. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  1278. }
  1279. if($this->MessageID != '') {
  1280. $result .= $this->HeaderLine('Message-ID', $this->MessageID);
  1281. } else {
  1282. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  1283. }
  1284. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  1285. if ($this->XMailer == '') {
  1286. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (http://code.google.com/a/apache-extras.org/p/phpmailer/)');
  1287. } else {
  1288. $myXmailer = trim($this->XMailer);
  1289. if ($myXmailer) {
  1290. $result .= $this->HeaderLine('X-Mailer', $myXmailer);
  1291. }
  1292. }
  1293. if($this->ConfirmReadingTo != '') {
  1294. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  1295. }
  1296. // Add custom headers
  1297. for($index = 0; $index < count($this->CustomHeader); $index++) {
  1298. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  1299. }
  1300. if (!$this->sign_key_file) {
  1301. $result .= $this->HeaderLine('MIME-Version', '1.0');
  1302. $result .= $this->GetMailMIME();
  1303. }
  1304. return $result;
  1305. }
  1306. /**
  1307. * Returns the message MIME.
  1308. * @access public
  1309. * @return string
  1310. */
  1311. public function GetMailMIME() {
  1312. $result = '';
  1313. switch($this->message_type) {
  1314. case 'inline':
  1315. $result .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1316. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1317. break;
  1318. case 'attach':
  1319. case 'inline_attach':
  1320. case 'alt_attach':
  1321. case 'alt_inline_attach':
  1322. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  1323. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1324. break;
  1325. case 'alt':
  1326. case 'alt_inline':
  1327. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1328. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1329. break;
  1330. default:
  1331. // Catches case 'plain': and case '':
  1332. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  1333. $result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset='.$this->CharSet);
  1334. break;
  1335. }
  1336. if($this->Mailer != 'mail') {
  1337. $result .= $this->LE;
  1338. }
  1339. return $result;
  1340. }
  1341. /**
  1342. * Returns the MIME message (headers and body). Only really valid post PreSend().
  1343. * @access public
  1344. * @return string
  1345. */
  1346. public function GetSentMIMEMessage() {
  1347. return $this->MIMEHeader . $this->mailHeader . self::CRLF . $this->MIMEBody;
  1348. }
  1349. /**
  1350. * Assembles the message body. Returns an empty string on failure.
  1351. * @access public
  1352. * @throws phpmailerException
  1353. * @return string The assembled message body
  1354. */
  1355. public function CreateBody() {
  1356. $body = '';
  1357. if ($this->sign_key_file) {
  1358. $body .= $this->GetMailMIME().$this->LE;
  1359. }
  1360. $this->SetWordWrap();
  1361. switch($this->message_type) {
  1362. case 'inline':
  1363. $body .= $this->GetBoundary($this->boundary[1], '', '', '');
  1364. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1365. $body .= $this->LE.$this->LE;
  1366. $body .= $this->AttachAll("inline", $this->boundary[1]);
  1367. break;
  1368. case 'attach':
  1369. $body .= $this->GetBoundary($this->boundary[1], '', '', '');
  1370. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1371. $body .= $this->LE.$this->LE;
  1372. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1373. break;
  1374. case 'inline_attach':
  1375. $body .= $this->TextLine("--" . $this->boundary[1]);
  1376. $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1377. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1378. $body .= $this->LE;
  1379. $body .= $this->GetBoundary($this->boundary[2], '', '', '');
  1380. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1381. $body .= $this->LE.$this->LE;
  1382. $body .= $this->AttachAll("inline", $this->boundary[2]);
  1383. $body .= $this->LE;
  1384. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1385. break;
  1386. case 'alt':
  1387. $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  1388. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1389. $body .= $this->LE.$this->LE;
  1390. $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  1391. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1392. $body .= $this->LE.$this->LE;
  1393. $body .= $this->EndBoundary($this->boundary[1]);
  1394. break;
  1395. case 'alt_inline':
  1396. $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  1397. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1398. $body .= $this->LE.$this->LE;
  1399. $body .= $this->TextLine("--" . $this->boundary[1]);
  1400. $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1401. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1402. $body .= $this->LE;
  1403. $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
  1404. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1405. $body .= $this->LE.$this->LE;
  1406. $body .= $this->AttachAll("inline", $this->boundary[2]);
  1407. $body .= $this->LE;
  1408. $body .= $this->EndBoundary($this->boundary[1]);
  1409. break;
  1410. case 'alt_attach':
  1411. $body .= $this->TextLine("--" . $this->boundary[1]);
  1412. $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1413. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1414. $body .= $this->LE;
  1415. $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
  1416. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1417. $body .= $this->LE.$this->LE;
  1418. $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '');
  1419. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1420. $body .= $this->LE.$this->LE;
  1421. $body .= $this->EndBoundary($this->boundary[2]);
  1422. $body .= $this->LE;
  1423. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1424. break;
  1425. case 'alt_inline_attach':
  1426. $body .= $this->TextLine("--" . $this->boundary[1]);
  1427. $body .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1428. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[2] . '"');
  1429. $body .= $this->LE;
  1430. $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '');
  1431. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1432. $body .= $this->LE.$this->LE;
  1433. $body .= $this->TextLine("--" . $this->boundary[2]);
  1434. $body .= $this->HeaderLine('Content-Type', 'multipart/related;');
  1435. $body .= $this->TextLine("\tboundary=\"" . $this->boundary[3] . '"');
  1436. $body .= $this->LE;
  1437. $body .= $this->GetBoundary($this->boundary[3], '', 'text/html', '');
  1438. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1439. $body .= $this->LE.$this->LE;
  1440. $body .= $this->AttachAll("inline", $this->boundary[3]);
  1441. $body .= $this->LE;
  1442. $body .= $this->EndBoundary($this->boundary[2]);
  1443. $body .= $this->LE;
  1444. $body .= $this->AttachAll("attachment", $this->boundary[1]);
  1445. break;
  1446. default:
  1447. // catch case 'plain' and case ''
  1448. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1449. break;
  1450. }
  1451. if ($this->IsError()) {
  1452. $body = '';
  1453. } elseif ($this->sign_key_file) {
  1454. try {
  1455. $file = tempnam('', 'mail');
  1456. file_put_contents($file, $body); //TODO check this worked
  1457. $signed = tempnam("", "signed");
  1458. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {
  1459. @unlink($file);
  1460. $body = file_get_contents($signed);
  1461. @unlink($signed);
  1462. } else {
  1463. @unlink($file);
  1464. @unlink($signed);
  1465. throw new phpmailerException($this->Lang("signing").openssl_error_string());
  1466. }
  1467. } catch (phpmailerException $e) {
  1468. $body = '';
  1469. if ($this->exceptions) {
  1470. throw $e;
  1471. }
  1472. }
  1473. }
  1474. return $body;
  1475. }
  1476. /**
  1477. * Returns the start of a message boundary.
  1478. * @access protected
  1479. * @param string $boundary
  1480. * @param string $charSet
  1481. * @param string $contentType
  1482. * @param string $encoding
  1483. * @return string
  1484. */
  1485. protected function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  1486. $result = '';
  1487. if($charSet == '') {
  1488. $charSet = $this->CharSet;
  1489. }
  1490. if($contentType == '') {
  1491. $contentType = $this->ContentType;
  1492. }
  1493. if($encoding == '') {
  1494. $encoding = $this->Encoding;
  1495. }
  1496. $result .= $this->TextLine('--' . $boundary);
  1497. $result .= sprintf("Content-Type: %s; charset=%s", $contentType, $charSet);
  1498. $result .= $this->LE;
  1499. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  1500. $result .= $this->LE;
  1501. return $result;
  1502. }
  1503. /**
  1504. * Returns the end of a message boundary.
  1505. * @access protected
  1506. * @param string $boundary
  1507. * @return string
  1508. */
  1509. protected function EndBoundary($boundary) {
  1510. return $this->LE . '--' . $boundary . '--' . $this->LE;
  1511. }
  1512. /**
  1513. * Sets the message type.
  1514. * @access protected
  1515. * @return void
  1516. */
  1517. protected function SetMessageType() {
  1518. $this->message_type = array();
  1519. if($this->AlternativeExists()) $this->message_type[] = "alt";
  1520. if($this->InlineImageExists()) $this->message_type[] = "inline";
  1521. if($this->AttachmentExists()) $this->message_type[] = "attach";
  1522. $this->message_type = implode("_", $this->message_type);
  1523. if($this->message_type == "") $this->message_type = "plain";
  1524. }
  1525. /**
  1526. * Returns a formatted header line.
  1527. * @access public
  1528. * @param string $name
  1529. * @param string $value
  1530. * @return string
  1531. */
  1532. public function HeaderLine($name, $value) {
  1533. return $name . ': ' . $value . $this->LE;
  1534. }
  1535. /**
  1536. * Returns a formatted mail line.
  1537. * @access public
  1538. * @param string $value
  1539. * @return string
  1540. */
  1541. public function TextLine($value) {
  1542. return $value . $this->LE;
  1543. }
  1544. /////////////////////////////////////////////////
  1545. // CLASS METHODS, ATTACHMENTS
  1546. /////////////////////////////////////////////////
  1547. /**
  1548. * Adds an attachment from a path on the filesystem.
  1549. * Returns false if the file could not be found
  1550. * or accessed.
  1551. * @param string $path Path to the attachment.
  1552. * @param string $name Overrides the attachment name.
  1553. * @param string $encoding File encoding (see $Encoding).
  1554. * @param string $type File extension (MIME) type.
  1555. * @throws phpmailerException
  1556. * @return bool
  1557. */
  1558. public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1559. try {
  1560. if ( !@is_file($path) ) {
  1561. throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
  1562. }
  1563. $filename = basename($path);
  1564. if ( $name == '' ) {
  1565. $name = $filename;
  1566. }
  1567. $this->attachment[] = array(
  1568. 0 => $path,
  1569. 1 => $filename,
  1570. 2 => $name,
  1571. 3 => $encoding,
  1572. 4 => $type,
  1573. 5 => false, // isStringAttachment
  1574. 6 => 'attachment',
  1575. 7 => 0
  1576. );
  1577. } catch (phpmailerException $e) {
  1578. $this->SetError($e->getMessage());
  1579. if ($this->exceptions) {
  1580. throw $e;
  1581. }
  1582. if ($this->SMTPDebug) {
  1583. $this->edebug($e->getMessage()."\n");
  1584. }
  1585. if ( $e->getCode() == self::STOP_CRITICAL ) {
  1586. return false;
  1587. }
  1588. }
  1589. return true;
  1590. }
  1591. /**
  1592. * Return the current array of attachments
  1593. * @return array
  1594. */
  1595. public function GetAttachments() {
  1596. return $this->attachment;
  1597. }
  1598. /**
  1599. * Attaches all fs, string, and binary attachments to the message.
  1600. * Returns an empty string on failure.
  1601. * @access protected
  1602. * @param string $disposition_type
  1603. * @param string $boundary
  1604. * @return string
  1605. */
  1606. protected function AttachAll($disposition_type, $boundary) {
  1607. // Return text of body
  1608. $mime = array();
  1609. $cidUniq = array();
  1610. $incl = array();
  1611. // Add all attachments
  1612. foreach ($this->attachment as $attachment) {
  1613. // CHECK IF IT IS A VALID DISPOSITION_FILTER
  1614. if($attachment[6] == $disposition_type) {
  1615. // Check for string attachment
  1616. $string = '';
  1617. $path = '';
  1618. $bString = $attachment[5];
  1619. if ($bString) {
  1620. $string = $attachment[0];
  1621. } else {
  1622. $path = $attachment[0];
  1623. }
  1624. $inclhash = md5(serialize($attachment));
  1625. if (in_array($inclhash, $incl)) { continue; }
  1626. $incl[] = $inclhash;
  1627. $filename = $attachment[1];
  1628. $name = $attachment[2];
  1629. $encoding = $attachment[3];
  1630. $type = $attachment[4];
  1631. $disposition = $attachment[6];
  1632. $cid = $attachment[7];
  1633. if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
  1634. $cidUniq[$cid] = true;
  1635. $mime[] = sprintf("--%s%s", $boundary, $this->LE);
  1636. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
  1637. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1638. if($disposition == 'inline') {
  1639. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1640. }
  1641. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
  1642. // Encode as string attachment
  1643. if($bString) {
  1644. $mime[] = $this->EncodeString($string, $encoding);
  1645. if($this->IsError()) {
  1646. return '';
  1647. }
  1648. $mime[] = $this->LE.$this->LE;
  1649. } else {
  1650. $mime[] = $this->EncodeFile($path, $encoding);
  1651. if($this->IsError()) {
  1652. return '';
  1653. }
  1654. $mime[] = $this->LE.$this->LE;
  1655. }
  1656. }
  1657. }
  1658. $mime[] = sprintf("--%s--%s", $boundary, $this->LE);
  1659. return implode("", $mime);
  1660. }
  1661. /**
  1662. * Encodes attachment in requested format.
  1663. * Returns an empty string on failure.
  1664. * @param string $path The full path to the file
  1665. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
  1666. * @throws phpmailerException
  1667. * @see EncodeFile()
  1668. * @access protected
  1669. * @return string
  1670. */
  1671. protected function EncodeFile($path, $encoding = 'base64') {
  1672. try {
  1673. if (!is_readable($path)) {
  1674. throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
  1675. }
  1676. // if (!function_exists('get_magic_quotes')) {
  1677. // function get_magic_quotes() {
  1678. // return false;
  1679. // }
  1680. // }
  1681. $magic_quotes = get_magic_quotes_runtime();
  1682. if ($magic_quotes) {
  1683. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  1684. set_magic_quotes_runtime(0);
  1685. } else {
  1686. ini_set('magic_quotes_runtime', 0);
  1687. }
  1688. }
  1689. $file_buffer = file_get_contents($path);
  1690. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1691. if ($magic_quotes) {
  1692. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  1693. set_magic_quotes_runtime($magic_quotes);
  1694. } else {
  1695. ini_set('magic_quotes_runtime', $magic_quotes);
  1696. }
  1697. }
  1698. return $file_buffer;
  1699. } catch (Exception $e) {
  1700. $this->SetError($e->getMessage());
  1701. return '';
  1702. }
  1703. }
  1704. /**
  1705. * Encodes string to requested format.
  1706. * Returns an empty string on failure.
  1707. * @param string $str The text to encode
  1708. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
  1709. * @access public
  1710. * @return string
  1711. */
  1712. public function EncodeString($str, $encoding = 'base64') {
  1713. $encoded = '';
  1714. switch(strtolower($encoding)) {
  1715. case 'base64':
  1716. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1717. break;
  1718. case '7bit':
  1719. case '8bit':
  1720. $encoded = $this->FixEOL($str);
  1721. //Make sure it ends with a line break
  1722. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1723. $encoded .= $this->LE;
  1724. break;
  1725. case 'binary':
  1726. $encoded = $str;
  1727. break;
  1728. case 'quoted-printable':
  1729. $encoded = $this->EncodeQP($str);
  1730. break;
  1731. default:
  1732. $this->SetError($this->Lang('encoding') . $encoding);
  1733. break;
  1734. }
  1735. return $encoded;
  1736. }
  1737. /**
  1738. * Encode a header string to best (shortest) of Q, B, quoted or none.
  1739. * @access public
  1740. * @param string $str
  1741. * @param string $position
  1742. * @return string
  1743. */
  1744. public function EncodeHeader($str, $position = 'text') {
  1745. $x = 0;
  1746. switch (strtolower($position)) {
  1747. case 'phrase':
  1748. if (!preg_match('/[\200-\377]/', $str)) {
  1749. // Can't use addslashes as we don't know what value has magic_quotes_sybase
  1750. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1751. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1752. return ($encoded);
  1753. } else {
  1754. return ("\"$encoded\"");
  1755. }
  1756. }
  1757. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1758. break;
  1759. case 'comment':
  1760. $x = preg_match_all('/[()"]/', $str, $matches);
  1761. // Fall-through
  1762. case 'text':
  1763. default:
  1764. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1765. break;
  1766. }
  1767. if ($x == 0) {
  1768. return ($str);
  1769. }
  1770. $maxlen = 75 - 7 - strlen($this->CharSet);
  1771. // Try to select the encoding which should produce the shortest output
  1772. if (strlen($str)/3 < $x) {
  1773. $encoding = 'B';
  1774. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1775. // Use a custom function which correctly encodes and wraps long
  1776. // multibyte strings without breaking lines within a character
  1777. $encoded = $this->Base64EncodeWrapMB($str, "\n");
  1778. } else {
  1779. $encoded = base64_encode($str);
  1780. $maxlen -= $maxlen % 4;
  1781. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1782. }
  1783. } else {
  1784. $encoding = 'Q';
  1785. $encoded = $this->EncodeQ($str, $position);
  1786. $encoded = $this->WrapText($encoded, $maxlen, true);
  1787. $encoded = str_replace('='.self::CRLF, "\n", trim($encoded));
  1788. }
  1789. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1790. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1791. return $encoded;
  1792. }
  1793. /**
  1794. * Checks if a string contains multibyte characters.
  1795. * @access public
  1796. * @param string $str multi-byte text to wrap encode
  1797. * @return bool
  1798. */
  1799. public function HasMultiBytes($str) {
  1800. if (function_exists('mb_strlen')) {
  1801. return (strlen($str) > mb_strlen($str, $this->CharSet));
  1802. } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
  1803. return false;
  1804. }
  1805. }
  1806. /**
  1807. * Correctly encodes and wraps long multibyte strings for mail headers
  1808. * without breaking lines within a character.
  1809. * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
  1810. * @access public
  1811. * @param string $str multi-byte text to wrap encode
  1812. * @param string $lf string to use as linefeed/end-of-line
  1813. * @return string
  1814. */
  1815. public function Base64EncodeWrapMB($str, $lf=null) {
  1816. $start = "=?".$this->CharSet."?B?";
  1817. $end = "?=";
  1818. $encoded = "";
  1819. if ($lf === null) {
  1820. $lf = $this->LE;
  1821. }
  1822. $mb_length = mb_strlen($str, $this->CharSet);
  1823. // Each line must have length <= 75, including $start and $end
  1824. $length = 75 - strlen($start) - strlen($end);
  1825. // Average multi-byte ratio
  1826. $ratio = $mb_length / strlen($str);
  1827. // Base64 has a 4:3 ratio
  1828. $offset = $avgLength = floor($length * $ratio * .75);
  1829. for ($i = 0; $i < $mb_length; $i += $offset) {
  1830. $lookBack = 0;
  1831. do {
  1832. $offset = $avgLength - $lookBack;
  1833. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1834. $chunk = base64_encode($chunk);
  1835. $lookBack++;
  1836. }
  1837. while (strlen($chunk) > $length);
  1838. $encoded .= $chunk . $lf;
  1839. }
  1840. // Chomp the last linefeed
  1841. $encoded = substr($encoded, 0, -strlen($lf));
  1842. return $encoded;
  1843. }
  1844. /**
  1845. * Encode string to quoted-printable.
  1846. * Only uses standard PHP, slow, but will always work
  1847. * @access public
  1848. * @param string $input
  1849. * @param integer $line_max Number of chars allowed on a line before wrapping
  1850. * @param bool $space_conv
  1851. * @internal param string $string the text to encode
  1852. * @return string
  1853. */
  1854. public function EncodeQPphp( $input = '', $line_max = 76, $space_conv = false) {
  1855. $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
  1856. $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
  1857. $eol = "\r\n";
  1858. $escape = '=';
  1859. $output = '';
  1860. while( list(, $line) = each($lines) ) {
  1861. $linlen = strlen($line);
  1862. $newline = '';
  1863. for($i = 0; $i < $linlen; $i++) {
  1864. $c = substr( $line, $i, 1 );
  1865. $dec = ord( $c );
  1866. if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
  1867. $c = '=2E';
  1868. }
  1869. if ( $dec == 32 ) {
  1870. if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
  1871. $c = '=20';
  1872. } else if ( $space_conv ) {
  1873. $c = '=20';
  1874. }
  1875. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1876. $h2 = (integer)floor($dec/16);
  1877. $h1 = (integer)floor($dec%16);
  1878. $c = $escape.$hex[$h2].$hex[$h1];
  1879. }
  1880. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1881. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1882. $newline = '';
  1883. // check if newline first character will be point or not
  1884. if ( $dec == 46 ) {
  1885. $c = '=2E';
  1886. }
  1887. }
  1888. $newline .= $c;
  1889. } // end of for
  1890. $output .= $newline.$eol;
  1891. } // end of while
  1892. return $output;
  1893. }
  1894. /**
  1895. * Encode string to RFC2045 (6.7) quoted-printable format
  1896. * Uses a PHP5 stream filter to do the encoding about 64x faster than the old version
  1897. * Also results in same content as you started with after decoding
  1898. * @see EncodeQPphp()
  1899. * @access public
  1900. * @param string $string the text to encode
  1901. * @param integer $line_max Number of chars allowed on a line before wrapping
  1902. * @param boolean $space_conv Dummy param for compatibility with existing EncodeQP function
  1903. * @return string
  1904. * @author Marcus Bointon
  1905. */
  1906. public function EncodeQP($string, $line_max = 76, $space_conv = false) {
  1907. if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
  1908. return quoted_printable_encode($string);
  1909. }
  1910. $filters = stream_get_filters();
  1911. if (!in_array('convert.*', $filters)) { //Got convert stream filter?
  1912. return $this->EncodeQPphp($string, $line_max, $space_conv); //Fall back to old implementation
  1913. }
  1914. $fp = fopen('php://temp/', 'r+');
  1915. $string = preg_replace('/\r\n?/', $this->LE, $string); //Normalise line breaks
  1916. $params = array('line-length' => $line_max, 'line-break-chars' => $this->LE);
  1917. $s = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $params);
  1918. fputs($fp, $string);
  1919. rewind($fp);
  1920. $out = stream_get_contents($fp);
  1921. stream_filter_remove($s);
  1922. $out = preg_replace('/^\./m', '=2E', $out); //Encode . if it is first char on a line, workaround for bug in Exchange
  1923. fclose($fp);
  1924. return $out;
  1925. }
  1926. /**
  1927. * Encode string to q encoding.
  1928. * @link http://tools.ietf.org/html/rfc2047
  1929. * @param string $str the text to encode
  1930. * @param string $position Where the text is going to be used, see the RFC for what that means
  1931. * @access public
  1932. * @return string
  1933. */
  1934. public function EncodeQ($str, $position = 'text') {
  1935. //There should not be any EOL in the string
  1936. $pattern="";
  1937. $encoded = str_replace(array("\r", "\n"), '', $str);
  1938. switch (strtolower($position)) {
  1939. case 'phrase':
  1940. $pattern = '^A-Za-z0-9!*+\/ -';
  1941. break;
  1942. case 'comment':
  1943. $pattern = '\(\)"';
  1944. //note that we dont break here!
  1945. //for this reason we build the $pattern withoud including delimiters and []
  1946. case 'text':
  1947. default:
  1948. //Replace every high ascii, control =, ? and _ characters
  1949. //We put \075 (=) as first value to make sure it's the first one in being converted, preventing double encode
  1950. $pattern = '\075\000-\011\013\014\016-\037\077\137\177-\377' . $pattern;
  1951. break;
  1952. }
  1953. if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
  1954. foreach (array_unique($matches[0]) as $char) {
  1955. $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
  1956. }
  1957. }
  1958. //Replace every spaces to _ (more readable than =20)
  1959. return str_replace(' ', '_', $encoded);
  1960. }
  1961. /**
  1962. * Adds a string or binary attachment (non-filesystem) to the list.
  1963. * This method can be used to attach ascii or binary data,
  1964. * such as a BLOB record from a database.
  1965. * @param string $string String attachment data.
  1966. * @param string $filename Name of the attachment.
  1967. * @param string $encoding File encoding (see $Encoding).
  1968. * @param string $type File extension (MIME) type.
  1969. * @return void
  1970. */
  1971. public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
  1972. // Append to $attachment array
  1973. $this->attachment[] = array(
  1974. 0 => $string,
  1975. 1 => $filename,
  1976. 2 => basename($filename),
  1977. 3 => $encoding,
  1978. 4 => $type,
  1979. 5 => true, // isStringAttachment
  1980. 6 => 'attachment',
  1981. 7 => 0
  1982. );
  1983. }
  1984. /**
  1985. * Adds an embedded attachment. This can include images, sounds, and
  1986. * just about any other document. Make sure to set the $type to an
  1987. * image type. For JPEG images use "image/jpeg" and for GIF images
  1988. * use "image/gif".
  1989. * @param string $path Path to the attachment.
  1990. * @param string $cid Content ID of the attachment. Use this to identify
  1991. * the Id for accessing the image in an HTML form.
  1992. * @param string $name Overrides the attachment name.
  1993. * @param string $encoding File encoding (see $Encoding).
  1994. * @param string $type File extension (MIME) type.
  1995. * @return bool
  1996. */
  1997. public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1998. if ( !@is_file($path) ) {
  1999. $this->SetError($this->Lang('file_access') . $path);
  2000. return false;
  2001. }
  2002. $filename = basename($path);
  2003. if ( $name == '' ) {
  2004. $name = $filename;
  2005. }
  2006. // Append to $attachment array
  2007. $this->attachment[] = array(
  2008. 0 => $path,
  2009. 1 => $filename,
  2010. 2 => $name,
  2011. 3 => $encoding,
  2012. 4 => $type,
  2013. 5 => false, // isStringAttachment
  2014. 6 => 'inline',
  2015. 7 => $cid
  2016. );
  2017. return true;
  2018. }
  2019. /**
  2020. * Adds an embedded stringified attachment. This can include images, sounds, and
  2021. * just about any other document. Make sure to set the $type to an
  2022. * image type. For JPEG images use "image/jpeg" and for GIF images
  2023. * use "image/gif".
  2024. * @param string $string The attachment.
  2025. * @param string $cid Content ID of the attachment. Use this to identify
  2026. * the Id for accessing the image in an HTML form.
  2027. * @param string $name Overrides the attachment name.
  2028. * @param string $encoding File encoding (see $Encoding).
  2029. * @param string $type File extension (MIME) type.
  2030. * @return bool
  2031. */
  2032. public function AddStringEmbeddedImage($string, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  2033. // Append to $attachment array
  2034. $this->attachment[] = array(
  2035. 0 => $string,
  2036. 1 => $name,
  2037. 2 => $name,
  2038. 3 => $encoding,
  2039. 4 => $type,
  2040. 5 => true, // isStringAttachment
  2041. 6 => 'inline',
  2042. 7 => $cid
  2043. );
  2044. }
  2045. /**
  2046. * Returns true if an inline attachment is present.
  2047. * @access public
  2048. * @return bool
  2049. */
  2050. public function InlineImageExists() {
  2051. foreach($this->attachment as $attachment) {
  2052. if ($attachment[6] == 'inline') {
  2053. return true;
  2054. }
  2055. }
  2056. return false;
  2057. }
  2058. /**
  2059. * Returns true if an attachment (non-inline) is present.
  2060. * @return bool
  2061. */
  2062. public function AttachmentExists() {
  2063. foreach($this->attachment as $attachment) {
  2064. if ($attachment[6] == 'attachment') {
  2065. return true;
  2066. }
  2067. }
  2068. return false;
  2069. }
  2070. /**
  2071. * Does this message have an alternative body set?
  2072. * @return bool
  2073. */
  2074. public function AlternativeExists() {
  2075. return !empty($this->AltBody);
  2076. }
  2077. /////////////////////////////////////////////////
  2078. // CLASS METHODS, MESSAGE RESET
  2079. /////////////////////////////////////////////////
  2080. /**
  2081. * Clears all recipients assigned in the TO array. Returns void.
  2082. * @return void
  2083. */
  2084. public function ClearAddresses() {
  2085. foreach($this->to as $to) {
  2086. unset($this->all_recipients[strtolower($to[0])]);
  2087. }
  2088. $this->to = array();
  2089. }
  2090. /**
  2091. * Clears all recipients assigned in the CC array. Returns void.
  2092. * @return void
  2093. */
  2094. public function ClearCCs() {
  2095. foreach($this->cc as $cc) {
  2096. unset($this->all_recipients[strtolower($cc[0])]);
  2097. }
  2098. $this->cc = array();
  2099. }
  2100. /**
  2101. * Clears all recipients assigned in the BCC array. Returns void.
  2102. * @return void
  2103. */
  2104. public function ClearBCCs() {
  2105. foreach($this->bcc as $bcc) {
  2106. unset($this->all_recipients[strtolower($bcc[0])]);
  2107. }
  2108. $this->bcc = array();
  2109. }
  2110. /**
  2111. * Clears all recipients assigned in the ReplyTo array. Returns void.
  2112. * @return void
  2113. */
  2114. public function ClearReplyTos() {
  2115. $this->ReplyTo = array();
  2116. }
  2117. /**
  2118. * Clears all recipients assigned in the TO, CC and BCC
  2119. * array. Returns void.
  2120. * @return void
  2121. */
  2122. public function ClearAllRecipients() {
  2123. $this->to = array();
  2124. $this->cc = array();
  2125. $this->bcc = array();
  2126. $this->all_recipients = array();
  2127. }
  2128. /**
  2129. * Clears all previously set filesystem, string, and binary
  2130. * attachments. Returns void.
  2131. * @return void
  2132. */
  2133. public function ClearAttachments() {
  2134. $this->attachment = array();
  2135. }
  2136. /**
  2137. * Clears all custom headers. Returns void.
  2138. * @return void
  2139. */
  2140. public function ClearCustomHeaders() {
  2141. $this->CustomHeader = array();
  2142. }
  2143. /////////////////////////////////////////////////
  2144. // CLASS METHODS, MISCELLANEOUS
  2145. /////////////////////////////////////////////////
  2146. /**
  2147. * Adds the error message to the error container.
  2148. * @access protected
  2149. * @param string $msg
  2150. * @return void
  2151. */
  2152. protected function SetError($msg) {
  2153. $this->error_count++;
  2154. if ($this->Mailer == 'smtp' and !is_null($this->smtp)) {
  2155. $lasterror = $this->smtp->getError();
  2156. if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) {
  2157. $msg .= '<p>' . $this->Lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n";
  2158. }
  2159. }
  2160. $this->ErrorInfo = $msg;
  2161. }
  2162. /**
  2163. * Returns the proper RFC 822 formatted date.
  2164. * @access public
  2165. * @return string
  2166. * @static
  2167. */
  2168. public static function RFCDate() {
  2169. $tz = date('Z');
  2170. $tzs = ($tz < 0) ? '-' : '+';
  2171. $tz = abs($tz);
  2172. $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
  2173. $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
  2174. return $result;
  2175. }
  2176. /**
  2177. * Returns the server hostname or 'localhost.localdomain' if unknown.
  2178. * @access protected
  2179. * @return string
  2180. */
  2181. protected function ServerHostname() {
  2182. if (!empty($this->Hostname)) {
  2183. $result = $this->Hostname;
  2184. } elseif (isset($_SERVER['SERVER_NAME'])) {
  2185. $result = $_SERVER['SERVER_NAME'];
  2186. } else {
  2187. $result = 'localhost.localdomain';
  2188. }
  2189. return $result;
  2190. }
  2191. /**
  2192. * Returns a message in the appropriate language.
  2193. * @access protected
  2194. * @param string $key
  2195. * @return string
  2196. */
  2197. protected function Lang($key) {
  2198. if(count($this->language) < 1) {
  2199. $this->SetLanguage('en'); // set the default language
  2200. }
  2201. if(isset($this->language[$key])) {
  2202. return $this->language[$key];
  2203. } else {
  2204. return 'Language string failed to load: ' . $key;
  2205. }
  2206. }
  2207. /**
  2208. * Returns true if an error occurred.
  2209. * @access public
  2210. * @return bool
  2211. */
  2212. public function IsError() {
  2213. return ($this->error_count > 0);
  2214. }
  2215. /**
  2216. * Changes every end of line from CRLF, CR or LF to $this->LE.
  2217. * @access public
  2218. * @param string $str String to FixEOL
  2219. * @return string
  2220. */
  2221. public function FixEOL($str) {
  2222. // condense down to \n
  2223. $nstr = str_replace(array("\r\n", "\r"), "\n", $str);
  2224. // Now convert LE as needed
  2225. if ($this->LE !== "\n") {
  2226. $nstr = str_replace("\n", $this->LE, $nstr);
  2227. }
  2228. return $nstr;
  2229. }
  2230. /**
  2231. * Adds a custom header. $name value can be overloaded to contain
  2232. * both header name and value (name:value)
  2233. * @access public
  2234. * @param string $name custom header name
  2235. * @param string $value header value
  2236. * @return void
  2237. */
  2238. public function AddCustomHeader($name, $value=null) {
  2239. if ($value === null) {
  2240. // Value passed in as name:value
  2241. $this->CustomHeader[] = explode(':', $name, 2);
  2242. } else {
  2243. $this->CustomHeader[] = array($name, $value);
  2244. }
  2245. }
  2246. /**
  2247. * Evaluates the message and returns modifications for inline images and backgrounds
  2248. * @access public
  2249. * @param string $message Text to be HTML modified
  2250. * @param string $basedir baseline directory for path
  2251. * @return string $message
  2252. */
  2253. public function MsgHTML($message, $basedir = '') {
  2254. preg_match_all("/(src|background)=[\"'](.*)[\"']/Ui", $message, $images);
  2255. if(isset($images[2])) {
  2256. foreach($images[2] as $i => $url) {
  2257. // do not change urls for absolute images (thanks to corvuscorax)
  2258. if (!preg_match('#^[A-z]+://#', $url)) {
  2259. $filename = basename($url);
  2260. $directory = dirname($url);
  2261. if ($directory == '.') {
  2262. $directory = '';
  2263. }
  2264. $cid = 'cid:' . md5($filename);
  2265. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  2266. $mimeType = self::_mime_types($ext);
  2267. if ( strlen($basedir) > 1 && substr($basedir, -1) != '/') { $basedir .= '/'; }
  2268. if ( strlen($directory) > 1 && substr($directory, -1) != '/') { $directory .= '/'; }
  2269. if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType) ) {
  2270. $message = preg_replace("/".$images[1][$i]."=[\"']".preg_quote($url, '/')."[\"']/Ui", $images[1][$i]."=\"".$cid."\"", $message);
  2271. }
  2272. }
  2273. }
  2274. }
  2275. $this->IsHTML(true);
  2276. $this->Body = $message;
  2277. if (empty($this->AltBody)) {
  2278. $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s', '', $message)));
  2279. if (!empty($textMsg)) {
  2280. $this->AltBody = html_entity_decode($textMsg, ENT_QUOTES, $this->CharSet);
  2281. }
  2282. }
  2283. if (empty($this->AltBody)) {
  2284. $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
  2285. }
  2286. return $message;
  2287. }
  2288. /**
  2289. * Gets the MIME type of the embedded or inline image
  2290. * @param string $ext File extension
  2291. * @access public
  2292. * @return string MIME type of ext
  2293. * @static
  2294. */
  2295. public static function _mime_types($ext = '') {
  2296. $mimes = array(
  2297. 'xl' => 'application/excel',
  2298. 'hqx' => 'application/mac-binhex40',
  2299. 'cpt' => 'application/mac-compactpro',
  2300. 'bin' => 'application/macbinary',
  2301. 'doc' => 'application/msword',
  2302. 'word' => 'application/msword',
  2303. 'class' => 'application/octet-stream',
  2304. 'dll' => 'application/octet-stream',
  2305. 'dms' => 'application/octet-stream',
  2306. 'exe' => 'application/octet-stream',
  2307. 'lha' => 'application/octet-stream',
  2308. 'lzh' => 'application/octet-stream',
  2309. 'psd' => 'application/octet-stream',
  2310. 'sea' => 'application/octet-stream',
  2311. 'so' => 'application/octet-stream',
  2312. 'oda' => 'application/oda',
  2313. 'pdf' => 'application/pdf',
  2314. 'ai' => 'application/postscript',
  2315. 'eps' => 'application/postscript',
  2316. 'ps' => 'application/postscript',
  2317. 'smi' => 'application/smil',
  2318. 'smil' => 'application/smil',
  2319. 'mif' => 'application/vnd.mif',
  2320. 'xls' => 'application/vnd.ms-excel',
  2321. 'ppt' => 'application/vnd.ms-powerpoint',
  2322. 'wbxml' => 'application/vnd.wap.wbxml',
  2323. 'wmlc' => 'application/vnd.wap.wmlc',
  2324. 'dcr' => 'application/x-director',
  2325. 'dir' => 'application/x-director',
  2326. 'dxr' => 'application/x-director',
  2327. 'dvi' => 'application/x-dvi',
  2328. 'gtar' => 'application/x-gtar',
  2329. 'php3' => 'application/x-httpd-php',
  2330. 'php4' => 'application/x-httpd-php',
  2331. 'php' => 'application/x-httpd-php',
  2332. 'phtml' => 'application/x-httpd-php',
  2333. 'phps' => 'application/x-httpd-php-source',
  2334. 'js' => 'application/x-javascript',
  2335. 'swf' => 'application/x-shockwave-flash',
  2336. 'sit' => 'application/x-stuffit',
  2337. 'tar' => 'application/x-tar',
  2338. 'tgz' => 'application/x-tar',
  2339. 'xht' => 'application/xhtml+xml',
  2340. 'xhtml' => 'application/xhtml+xml',
  2341. 'zip' => 'application/zip',
  2342. 'mid' => 'audio/midi',
  2343. 'midi' => 'audio/midi',
  2344. 'mp2' => 'audio/mpeg',
  2345. 'mp3' => 'audio/mpeg',
  2346. 'mpga' => 'audio/mpeg',
  2347. 'aif' => 'audio/x-aiff',
  2348. 'aifc' => 'audio/x-aiff',
  2349. 'aiff' => 'audio/x-aiff',
  2350. 'ram' => 'audio/x-pn-realaudio',
  2351. 'rm' => 'audio/x-pn-realaudio',
  2352. 'rpm' => 'audio/x-pn-realaudio-plugin',
  2353. 'ra' => 'audio/x-realaudio',
  2354. 'wav' => 'audio/x-wav',
  2355. 'bmp' => 'image/bmp',
  2356. 'gif' => 'image/gif',
  2357. 'jpeg' => 'image/jpeg',
  2358. 'jpe' => 'image/jpeg',
  2359. 'jpg' => 'image/jpeg',
  2360. 'png' => 'image/png',
  2361. 'tiff' => 'image/tiff',
  2362. 'tif' => 'image/tiff',
  2363. 'eml' => 'message/rfc822',
  2364. 'css' => 'text/css',
  2365. 'html' => 'text/html',
  2366. 'htm' => 'text/html',
  2367. 'shtml' => 'text/html',
  2368. 'log' => 'text/plain',
  2369. 'text' => 'text/plain',
  2370. 'txt' => 'text/plain',
  2371. 'rtx' => 'text/richtext',
  2372. 'rtf' => 'text/rtf',
  2373. 'xml' => 'text/xml',
  2374. 'xsl' => 'text/xml',
  2375. 'mpeg' => 'video/mpeg',
  2376. 'mpe' => 'video/mpeg',
  2377. 'mpg' => 'video/mpeg',
  2378. 'mov' => 'video/quicktime',
  2379. 'qt' => 'video/quicktime',
  2380. 'rv' => 'video/vnd.rn-realvideo',
  2381. 'avi' => 'video/x-msvideo',
  2382. 'movie' => 'video/x-sgi-movie'
  2383. );
  2384. return (!isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
  2385. }
  2386. /**
  2387. * Set (or reset) Class Objects (variables)
  2388. *
  2389. * Usage Example:
  2390. * $page->set('X-Priority', '3');
  2391. *
  2392. * @access public
  2393. * @param string $name Parameter Name
  2394. * @param mixed $value Parameter Value
  2395. * NOTE: will not work with arrays, there are no arrays to set/reset
  2396. * @throws phpmailerException
  2397. * @return bool
  2398. * @todo Should this not be using __set() magic function?
  2399. */
  2400. public function set($name, $value = '') {
  2401. try {
  2402. if (isset($this->$name) ) {
  2403. $this->$name = $value;
  2404. } else {
  2405. throw new phpmailerException($this->Lang('variable_set') . $name, self::STOP_CRITICAL);
  2406. }
  2407. } catch (Exception $e) {
  2408. $this->SetError($e->getMessage());
  2409. if ($e->getCode() == self::STOP_CRITICAL) {
  2410. return false;
  2411. }
  2412. }
  2413. return true;
  2414. }
  2415. /**
  2416. * Strips newlines to prevent header injection.
  2417. * @access public
  2418. * @param string $str String
  2419. * @return string
  2420. */
  2421. public function SecureHeader($str) {
  2422. return trim(str_replace(array("\r", "\n"), '', $str));
  2423. }
  2424. /**
  2425. * Set the private key file and password to sign the message.
  2426. *
  2427. * @access public
  2428. * @param $cert_filename
  2429. * @param string $key_filename Parameter File Name
  2430. * @param string $key_pass Password for private key
  2431. */
  2432. public function Sign($cert_filename, $key_filename, $key_pass) {
  2433. $this->sign_cert_file = $cert_filename;
  2434. $this->sign_key_file = $key_filename;
  2435. $this->sign_key_pass = $key_pass;
  2436. }
  2437. /**
  2438. * Set the private key file and password to sign the message.
  2439. *
  2440. * @access public
  2441. * @param string $txt
  2442. * @return string
  2443. */
  2444. public function DKIM_QP($txt) {
  2445. $line = '';
  2446. for ($i = 0; $i < strlen($txt); $i++) {
  2447. $ord = ord($txt[$i]);
  2448. if ( ((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E)) ) {
  2449. $line .= $txt[$i];
  2450. } else {
  2451. $line .= "=".sprintf("%02X", $ord);
  2452. }
  2453. }
  2454. return $line;
  2455. }
  2456. /**
  2457. * Generate DKIM signature
  2458. *
  2459. * @access public
  2460. * @param string $s Header
  2461. * @return string
  2462. */
  2463. public function DKIM_Sign($s) {
  2464. $privKeyStr = file_get_contents($this->DKIM_private);
  2465. if ($this->DKIM_passphrase != '') {
  2466. $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
  2467. } else {
  2468. $privKey = $privKeyStr;
  2469. }
  2470. if (openssl_sign($s, $signature, $privKey)) {
  2471. return base64_encode($signature);
  2472. }
  2473. return '';
  2474. }
  2475. /**
  2476. * Generate DKIM Canonicalization Header
  2477. *
  2478. * @access public
  2479. * @param string $s Header
  2480. * @return string
  2481. */
  2482. public function DKIM_HeaderC($s) {
  2483. $s = preg_replace("/\r\n\s+/", " ", $s);
  2484. $lines = explode("\r\n", $s);
  2485. foreach ($lines as $key => $line) {
  2486. list($heading, $value) = explode(":", $line, 2);
  2487. $heading = strtolower($heading);
  2488. $value = preg_replace("/\s+/", " ", $value) ; // Compress useless spaces
  2489. $lines[$key] = $heading.":".trim($value) ; // Don't forget to remove WSP around the value
  2490. }
  2491. $s = implode("\r\n", $lines);
  2492. return $s;
  2493. }
  2494. /**
  2495. * Generate DKIM Canonicalization Body
  2496. *
  2497. * @access public
  2498. * @param string $body Message Body
  2499. * @return string
  2500. */
  2501. public function DKIM_BodyC($body) {
  2502. if ($body == '') return "\r\n";
  2503. // stabilize line endings
  2504. $body = str_replace("\r\n", "\n", $body);
  2505. $body = str_replace("\n", "\r\n", $body);
  2506. // END stabilize line endings
  2507. while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") {
  2508. $body = substr($body, 0, strlen($body) - 2);
  2509. }
  2510. return $body;
  2511. }
  2512. /**
  2513. * Create the DKIM header, body, as new header
  2514. *
  2515. * @access public
  2516. * @param string $headers_line Header lines
  2517. * @param string $subject Subject
  2518. * @param string $body Body
  2519. * @return string
  2520. */
  2521. public function DKIM_Add($headers_line, $subject, $body) {
  2522. $DKIMsignatureType = 'rsa-sha1'; // Signature & hash algorithms
  2523. $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
  2524. $DKIMquery = 'dns/txt'; // Query method
  2525. $DKIMtime = time() ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
  2526. $subject_header = "Subject: $subject";
  2527. $headers = explode($this->LE, $headers_line);
  2528. $from_header = "";
  2529. $to_header = "";
  2530. foreach($headers as $header) {
  2531. if (strpos($header, 'From:') === 0) {
  2532. $from_header = $header;
  2533. } elseif (strpos($header, 'To:') === 0) {
  2534. $to_header = $header;
  2535. }
  2536. }
  2537. $from = str_replace('|', '=7C', $this->DKIM_QP($from_header));
  2538. $to = str_replace('|', '=7C', $this->DKIM_QP($to_header));
  2539. $subject = str_replace('|', '=7C', $this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
  2540. $body = $this->DKIM_BodyC($body);
  2541. $DKIMlen = strlen($body) ; // Length of body
  2542. $DKIMb64 = base64_encode(pack("H*", sha1($body))) ; // Base64 of packed binary SHA-1 hash of body
  2543. $ident = ($this->DKIM_identity == '')? '' : " i=" . $this->DKIM_identity . ";";
  2544. $dkimhdrs = "DKIM-Signature: v=1; a=" . $DKIMsignatureType . "; q=" . $DKIMquery . "; l=" . $DKIMlen . "; s=" . $this->DKIM_selector . ";\r\n".
  2545. "\tt=" . $DKIMtime . "; c=" . $DKIMcanonicalization . ";\r\n".
  2546. "\th=From:To:Subject;\r\n".
  2547. "\td=" . $this->DKIM_domain . ";" . $ident . "\r\n".
  2548. "\tz=$from\r\n".
  2549. "\t|$to\r\n".
  2550. "\t|$subject;\r\n".
  2551. "\tbh=" . $DKIMb64 . ";\r\n".
  2552. "\tb=";
  2553. $toSign = $this->DKIM_HeaderC($from_header . "\r\n" . $to_header . "\r\n" . $subject_header . "\r\n" . $dkimhdrs);
  2554. $signed = $this->DKIM_Sign($toSign);
  2555. return "X-PHPMAILER-DKIM: code.google.com/a/apache-extras.org/p/phpmailer/\r\n".$dkimhdrs.$signed."\r\n";
  2556. }
  2557. /**
  2558. * Perform callback
  2559. * @param boolean $isSent
  2560. * @param string $to
  2561. * @param string $cc
  2562. * @param string $bcc
  2563. * @param string $subject
  2564. * @param string $body
  2565. * @param string $from
  2566. */
  2567. protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from=null) {
  2568. if (!empty($this->action_function) && is_callable($this->action_function)) {
  2569. $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from);
  2570. call_user_func_array($this->action_function, $params);
  2571. }
  2572. }
  2573. }
  2574. /**
  2575. * Exception handler for PHPMailer
  2576. * @package PHPMailer
  2577. */
  2578. class phpmailerException extends Exception {
  2579. /**
  2580. * Prettify error message output
  2581. * @return string
  2582. */
  2583. public function errorMessage() {
  2584. $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
  2585. return $errorMsg;
  2586. }
  2587. }
  2588. ?>