Wall posté le Jeudi 28 février 2008


Class mail - phpmynewsletter


  1. <?php
  2. ////////////////////////////////////////////////////  
  3. // PHPMailer - PHP email class  
  4. //
  5. // Class for sending email using either  
  6. // sendmail, PHP mail(), or SMTP. Methods are  
  7. // based upon the standard AspEmail(tm) classes.  
  8. //
  9. // Copyright (C) 2001 - 2003 Brent R. Matzelle  
  10. //
  11. // License: LGPL, see LICENSE  
  12. ////////////////////////////////////////////////////  
  13.  
  14. /**
  15. * PHPMailer - PHP email transport class
  16. * @package PHPMailer
  17. * @author Brent R. Matzelle
  18. * @copyright 2001 - 2003 Brent R. Matzelle
  19. *
  20. * Support for online.net by G. Kokanosky (c) 2005
  21. *
  22. */  
  23. class PHPMailer  
  24. {  
  25. /////////////////////////////////////////////////  
  26. // PUBLIC VARIABLES  
  27. /////////////////////////////////////////////////  
  28.  
  29. /**
  30. * Email priority (1 = High, 3 = Normal, 5 = low).
  31. * @var int
  32. */  
  33. var $Priority = 3;  
  34.  
  35. /**
  36. * Sets the CharSet of the message.
  37. * @var string
  38. */  
  39. var $CharSet = "iso-8859-1";  
  40.  
  41. /**
  42. * Sets the Content-type of the message.
  43. * @var string
  44. */  
  45. var $ContentType = "text/plain";  
  46.  
  47. /**
  48. * Sets the Encoding of the message. Options for this are "8bit",
  49. * "7bit", "binary", "base64", and "quoted-printable".
  50. * @var string
  51. */  
  52. var $Encoding = "8bit";  
  53.  
  54. /**
  55. * Holds the most recent mailer error message.
  56. * @var string
  57. */  
  58. var $ErrorInfo = "";  
  59.  
  60. /**
  61. * Sets the From email address for the message.
  62. * @var string
  63. */  
  64. var $From = "root@localhost";  
  65.  
  66. /**
  67. * Sets the From name of the message.
  68. * @var string
  69. */  
  70. var $FromName = "Root User";  
  71.  
  72. /**
  73. * Sets the Sender email (Return-Path) of the message. If not empty,
  74. * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  75. * @var string
  76. */  
  77. var $Sender = "";  
  78.  
  79. /**
  80. * Sets the Subject of the message.
  81. * @var string
  82. */  
  83. var $Subject = "";  
  84.  
  85. /**
  86. * Sets the Body of the message. This can be either an HTML or text body.
  87. * If HTML then run IsHTML(true).
  88. * @var string
  89. */  
  90. var $Body = "";  
  91.  
  92. /**
  93. * Sets the text-only body of the message. This automatically sets the
  94. * email to multipart/alternative. This body can be read by mail
  95. * clients that do not have HTML email capability such as mutt. Clients
  96. * that can read HTML will view the normal Body.
  97. * @var string
  98. */  
  99. var $AltBody = "";  
  100.  
  101. /**
  102. * Sets word wrapping on the body of the message to a given number of
  103. * characters.
  104. * @var int
  105. */  
  106. var $WordWrap = 0;  
  107.  
  108. /**
  109. * Method to send mail: ("mail", "sendmail", or "smtp", or even "online").
  110. * @var string
  111. */  
  112. var $Mailer = "mail";  
  113.  
  114. /**
  115. * Sets the path of the sendmail program.
  116. * @var string
  117. */  
  118. var $Sendmail = "/usr/sbin/sendmail";  
  119.  
  120. /**
  121. * Path to PHPMailer plugins. This is now only useful if the SMTP class
  122. * is in a different directory than the PHP include path.
  123. * @var string
  124. */  
  125. var $PluginDir = "";  
  126.  
  127. /**
  128. * Holds PHPMailer version.
  129. * @var string
  130. */  
  131. var $Version = "1.72";  
  132.  
  133. /**
  134. * Sets the email address that a reading confirmation will be sent.
  135. * @var string
  136. */  
  137. var $ConfirmReadingTo = "";  
  138.  
  139. /**
  140. * Sets the hostname to use in Message-Id and Received headers
  141. * and as default HELO string. If empty, the value returned
  142. * by SERVER_NAME is used or 'localhost.localdomain'.
  143. * @var string
  144. */  
  145. var $Hostname = "";  
  146.  
  147. /////////////////////////////////////////////////  
  148. // SMTP VARIABLES  
  149. /////////////////////////////////////////////////  
  150.  
  151. /**
  152. * Sets the SMTP hosts. All hosts must be separated by a
  153. * semicolon. You can also specify a different port
  154. * for each host by using this format: [hostname:port]
  155. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  156. * Hosts will be tried in order.
  157. * @var string
  158. */  
  159. var $Host = "localhost";  
  160.  
  161. /**
  162. * Sets the default SMTP server port.
  163. * @var int
  164. */  
  165. var $Port = 25;  
  166.  
  167. /**
  168. * Sets the SMTP HELO of the message (Default is $Hostname).
  169. * @var string
  170. */  
  171. var $Helo = "";  
  172.  
  173. /**
  174. * Sets SMTP authentication. Utilizes the Username and Password variables.
  175. * @var bool
  176. */  
  177. var $SMTPAuth = false;  
  178.  
  179. /**
  180. * Sets SMTP username.
  181. * @var string
  182. */  
  183. var $Username = "";  
  184.  
  185. /**
  186. * Sets SMTP password.
  187. * @var string
  188. */  
  189. var $Password = "";  
  190.  
  191. /**
  192. * Sets the SMTP server timeout in seconds. This function will not
  193. * work with the win32 version.
  194. * @var int
  195. */  
  196. var $Timeout = 10;  
  197.  
  198. /**
  199. * Sets SMTP class debugging on or off.
  200. * @var bool
  201. */  
  202. var $SMTPDebug = false;  
  203.  
  204. /**
  205. * Prevents the SMTP connection from being closed after each mail
  206. * sending. If this is set to true then to close the connection
  207. * requires an explicit call to SmtpClose().
  208. * @var bool
  209. */  
  210. var $SMTPKeepAlive = false;  
  211.  
  212. /**#@+
  213. * @access private
  214. */  
  215. var $smtp = NULL;  
  216. var $to = array();  
  217. var $cc = array();  
  218. var $bcc = array();  
  219. var $ReplyTo = array();  
  220. var $attachment = array();  
  221. var $CustomHeader = array();  
  222. var $message_type = "";  
  223. var $boundary = array();  
  224. var $language = array();  
  225. var $error_count = 0;  
  226. var $LE = "\n";  
  227. /**#@-*/  
  228.  
  229. /////////////////////////////////////////////////  
  230. // VARIABLE METHODS  
  231. /////////////////////////////////////////////////  
  232.  
  233. /**
  234. * Sets message type to HTML.
  235. * @param bool $bool
  236. * @return void
  237. */  
  238. function IsHTML($bool) {  
  239. if($bool == true)  
  240. $this->ContentType = "text/html";  
  241. else  
  242. $this->ContentType = "text/plain";  
  243. }  
  244.  
  245. /**
  246. * Sets Mailer to send message using SMTP.
  247. * @return void
  248. */  
  249. function IsSMTP() {  
  250. $this->Mailer = "smtp";  
  251. }  
  252.  
  253. /**
  254. * Sets Mailer to send message using PHP mail() function.
  255. * @return void
  256. */  
  257. function IsMail() {  
  258. $this->Mailer = "mail";  
  259. }  
  260.  
  261. /**
  262. * Sets Mailer to send message using PHP email() online.net
  263. * specific function.
  264. * Returns void.
  265. * @access public
  266. * @return void
  267. */  
  268. function IsOnlineEmail() {  
  269. $this->Mailer = "online";  
  270. }  
  271.  
  272. /**
  273. * Sets Mailer to send message using the $Sendmail program.
  274. * @return void
  275. */  
  276. function IsSendmail() {  
  277. $this->Mailer = "sendmail";  
  278. }  
  279.  
  280. /**
  281. * Sets Mailer to send message using the qmail MTA.
  282. * @return void
  283. */  
  284. function IsQmail() {  
  285. $this->Sendmail = "/var/qmail/bin/sendmail";  
  286. $this->Mailer = "sendmail";  
  287. }  
  288.  
  289.  
  290. /////////////////////////////////////////////////  
  291. // RECIPIENT METHODS  
  292. /////////////////////////////////////////////////  
  293.  
  294. /**
  295. * Adds a "To" address.
  296. * @param string $address
  297. * @param string $name
  298. * @return void
  299. */  
  300. function AddAddress($address, $name = "") {  
  301. $cur = count($this->to);  
  302. $this->to[$cur][0] = trim($address);  
  303. $this->to[$cur][1] = $name;  
  304. }  
  305.  
  306. /**
  307. * Adds a "Cc" address. Note: this function works
  308. * with the SMTP mailer on win32, not with the "mail"
  309. * mailer.
  310. * @param string $address
  311. * @param string $name
  312. * @return void
  313. */  
  314. function AddCC($address, $name = "") {  
  315. $cur = count($this->cc);  
  316. $this->cc[$cur][0] = trim($address);  
  317. $this->cc[$cur][1] = $name;  
  318. }  
  319.  
  320. /**
  321. * Adds a "Bcc" address. Note: this function works
  322. * with the SMTP mailer on win32, not with the "mail"
  323. * mailer.
  324. * @param string $address
  325. * @param string $name
  326. * @return void
  327. */  
  328. function AddBCC($address, $name = "") {  
  329. $cur = count($this->bcc);  
  330. $this->bcc[$cur][0] = trim($address);  
  331. $this->bcc[$cur][1] = $name;  
  332. }  
  333.  
  334. /**
  335. * Adds a "Reply-to" address.
  336. * @param string $address
  337. * @param string $name
  338. * @return void
  339. */  
  340. function AddReplyTo($address, $name = "") {  
  341. $cur = count($this->ReplyTo);  
  342. $this->ReplyTo[$cur][0] = trim($address);  
  343. $this->ReplyTo[$cur][1] = $name;  
  344. }  
  345.  
  346.  
  347. /////////////////////////////////////////////////  
  348. // MAIL SENDING METHODS  
  349. /////////////////////////////////////////////////  
  350.  
  351. /**
  352. * Creates message and assigns Mailer. If the message is
  353. * not sent successfully then it returns false. Use the ErrorInfo
  354. * variable to view description of the error.
  355. * @return bool
  356. */  
  357. function Send() {  
  358. $header = "";  
  359. $body = "";  
  360. $result = true;  
  361.  
  362. if((count($this->to) + count($this->cc) + count($this->bcc)) < 1)  
  363. {  
  364. $this->SetError($this->Lang("provide_address"));  
  365. return false;  
  366. }  
  367.  
  368. // Set whether the message is multipart/alternative  
  369. if(!empty($this->AltBody))  
  370. $this->ContentType = "multipart/alternative";  
  371.  
  372. $this->error_count = 0; // reset errors  
  373. $this->SetMessageType();  
  374. $header .= $this->CreateHeader();  
  375. $body = $this->CreateBody();  
  376.  
  377. if($body == "") { return false; }  
  378.  
  379. // Choose the mailer  
  380. switch($this->Mailer)  
  381. {  
  382. case "sendmail":  
  383. $result = $this->SendmailSend($header, $body);  
  384. break;  
  385. case "mail":  
  386. $result = $this->MailSend($header, $body);  
  387. break;  
  388. case "smtp":  
  389. $result = $this->SmtpSend($header, $body);  
  390. break; 
  391.    case "online": 
  392.       $result = $this->EmailSend($header, $body); 
  393.       break;  
  394. default:  
  395. $this->SetError($this->Mailer . $this->Lang("mailer_not_supported"));  
  396. $result = false;  
  397. break;  
  398. }  
  399.  
  400. return $result;  
  401. }  
  402.  
  403. /**
  404. * Sends mail using the $Sendmail program.
  405. * @access private
  406. * @return bool
  407. */  
  408. function SendmailSend($header, $body) {  
  409. if ($this->Sender != "")  
  410. $sendmail = sprintf("%s -oi -f %s -t", $this->Sendmail, $this->Sender);  
  411. else  
  412. $sendmail = sprintf("%s -oi -t", $this->Sendmail);  
  413.  
  414. if(!@$mail = popen($sendmail, "w"))  
  415. {  
  416. $this->SetError($this->Lang("execute") . $this->Sendmail);  
  417. return false;  
  418. }  
  419.  
  420. fputs($mail, $header);  
  421. fputs($mail, $body);  
  422.  
  423. $result = pclose($mail) >> 8 & 0xFF;  
  424. if($result != 0)  
  425. {  
  426. $this->SetError($this->Lang("execute") . $this->Sendmail);  
  427. return false;  
  428. }  
  429.  
  430. return true;  
  431. }  
  432.  
  433. /**
  434. * Sends mail using the PHP mail() function.
  435. * @access private
  436. * @return bool
  437. */  
  438. function MailSend($header, $body) {  
  439. $to = "";  
  440. for($i = 0; $i < count($this->to); $i++)  
  441. {  
  442. if($i != 0) { $to .= ", "; }  
  443. $to .= $this->to[$i][0];  
  444. }  
  445.  
  446. if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)  
  447. {  
  448. $old_from = ini_get("sendmail_from");  
  449. ini_set("sendmail_from", $this->Sender);  
  450. $params = sprintf("-oi -f %s", $this->Sender);  
  451. $rt = @mail($to, $this->EncodeHeader($this->Subject), $body,  
  452. $header, $params);  
  453. }  
  454. else  
  455. $rt = @mail($to, $this->EncodeHeader($this->Subject), $body, $header);  
  456.  
  457. if (isset($old_from))  
  458. ini_set("sendmail_from", $old_from);  
  459.  
  460. if(!$rt)  
  461. {  
  462. $this->SetError($this->Lang("instantiate"));  
  463. return false;  
  464. }  
  465.  
  466. return true;  
  467. }  
  468.  
  469. /**
  470. * Sends mail using the online.net specific email() function.
  471. * @access private
  472. * @return bool
  473. */  
  474. function EmailSend($header, $body) {  
  475. $to = "";  
  476. for($i = 0; $i < count($this->to); $i++)  
  477. {  
  478. if($i != 0) { $to .= ", "; }  
  479. $to .= $this->to[$i][0];  
  480. } 
  481.  
  482.    list($from, $foo) = split("@", $this->From); 
  483.    $replyTo = $from; 
  484.  
  485.    //Online.net seems to want only this part of mail header ...  
  486. $header = "Content-Type:".$this->ContentType.";charset=".$this->CharSet."\n";  
  487. $header .= "Content-Transfer-Encoding: ".$this->Encoding."\n";  
  488. $header .= "Return-path: ".$this->From."\n";  
  489.  
  490. if ($this->Sender != "" && strlen(ini_get("safe_mode"))< 1)  
  491. {  
  492. $old_from = ini_get("sendmail_from");  
  493. ini_set("sendmail_from", $this->Sender);  
  494. $params = sprintf("-oi -f %s", $this->Sender);  
  495. $rt = @email($from, $to, $this->EncodeHeader($this->Subject), $body, $replyTo,  
  496. $header, $params);  
  497. }  
  498. else  
  499. $rt = @email($from, $to, $this->EncodeHeader($this->Subject), $body, $replyTo, $header);  
  500.  
  501. if (isset($old_from))  
  502. ini_set("sendmail_from", $old_from);  
  503.  
  504. if(!$rt)  
  505. {  
  506. $this->SetError($this->Lang("instantiate"));  
  507. return false;  
  508. }  
  509.  
  510. return true;  
  511. }  
  512.  
  513.  
  514. /**
  515. * Sends mail via SMTP using PhpSMTP (Author:
  516. * Chris Ryan). Returns bool. Returns false if there is a
  517. * bad MAIL FROM, RCPT, or DATA input.
  518. * @access private
  519. * @return bool
  520. */  
  521. function SmtpSend($header, $body) {  
  522. include_once($this->PluginDir . "class.smtp.php");  
  523. $error = "";  
  524. $bad_rcpt = array();  
  525.  
  526. if(!$this->SmtpConnect())  
  527. return false;  
  528.  
  529. $smtp_from = ($this->Sender == "") ? $this->From : $this->Sender;  
  530. if(!$this->smtp->Mail($smtp_from))  
  531. {  
  532. $error = $this->Lang("from_failed") . $smtp_from