class.phpmailer.php 88 KB

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