PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/class/CMailFile.class.php

https://github.com/asterix14/dolibarr
PHP | 997 lines | 757 code | 100 blank | 140 comment | 78 complexity | 5daf262194879ed61e667d46fb58518e MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * or see http://www.gnu.org/
  20. *
  21. * Lots of code inspired from Dan Potter's CMailFile class
  22. */
  23. /**
  24. * \file htdocs/core/class/CMailFile.class.php
  25. * \brief File of class to send emails (with attachments or not)
  26. * \author Dan Potter.
  27. * \author Eric Seigne
  28. * \author Laurent Destailleur.
  29. */
  30. /**
  31. * \class CMailFile
  32. * \brief Class to send emails (with attachments or not)
  33. * \remarks Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to);
  34. * \remarks $mailfile->sendfile();
  35. */
  36. class CMailFile
  37. {
  38. var $subject;
  39. var $addr_from;
  40. var $errors_to;
  41. var $addr_to;
  42. var $addr_cc;
  43. var $addr_bcc;
  44. var $mixed_boundary;
  45. var $related_boundary;
  46. var $alternative_boundary;
  47. var $deliveryreceipt;
  48. var $eol;
  49. var $atleastonefile=0;
  50. var $error='';
  51. var $smtps; // Contains SMTPs object (if this method is used)
  52. //CSS
  53. var $css;
  54. //! Defined css style for body background
  55. var $styleCSS;
  56. //! Defined bacckground directly in body tag
  57. var $bodyCSS;
  58. // Image
  59. var $html;
  60. var $image_boundary;
  61. var $atleastoneimage=0;
  62. var $html_images=array();
  63. var $images_encoded=array();
  64. var $image_types = array('gif' => 'image/gif',
  65. 'jpg' => 'image/jpeg',
  66. 'jpeg' => 'image/jpeg',
  67. 'jpe' => 'image/jpeg',
  68. 'bmp' => 'image/bmp',
  69. 'png' => 'image/png',
  70. 'tif' => 'image/tiff',
  71. 'tiff' => 'image/tiff');
  72. /**
  73. * CMailFile
  74. *
  75. * @param subject Topic/Subject of mail
  76. * @param to Recipients emails (RFC 2822: "Nom prenom <email>[, ...]" ou "email[, ...]" ou "<email>[, ...]")
  77. * @param from Sender email (RFC 2822: "Nom prenom <email>[, ...]" ou "email[, ...]" ou "<email>[, ...]")
  78. * @param msg Message
  79. * @param filename_list List of files to attach (full path of filename on file system)
  80. * @param mimetype_list List of MIME type of attached files
  81. * @param mimefilename_list List of attached file name in message
  82. * @param addr_cc Email cc
  83. * @param addr_bcc Email bcc
  84. * @param deliveryreceipt Ask a delivery receipt
  85. * @param msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown need autodetection
  86. * @param errors_to Email errors
  87. * @param css Css option
  88. */
  89. function CMailFile($subject,$to,$from,$msg,
  90. $filename_list=array(),$mimetype_list=array(),$mimefilename_list=array(),
  91. $addr_cc="",$addr_bcc="",$deliveryreceipt=0,$msgishtml=0,$errors_to='',$css='')
  92. {
  93. global $conf;
  94. // We define end of line (RFC 822bis section 2.3)
  95. $this->eol="\r\n";
  96. //if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
  97. //if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";
  98. // On defini mixed_boundary
  99. $this->mixed_boundary = dol_hash(uniqid("dolibarr1"));
  100. // On defini related_boundary
  101. $this->related_boundary = dol_hash(uniqid("dolibarr2"));
  102. // On defini alternative_boundary
  103. $this->alternative_boundary = dol_hash(uniqid("dolibarr3"));
  104. // If ending method not defined
  105. if (empty($conf->global->MAIN_MAIL_SENDMODE)) $conf->global->MAIN_MAIL_SENDMODE='mail';
  106. dol_syslog("CMailFile::CMailfile: MAIN_MAIL_SENDMODE=".$conf->global->MAIN_MAIL_SENDMODE." charset=".$conf->file->character_set_client." from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to", LOG_DEBUG);
  107. dol_syslog("CMailFile::CMailfile: subject=$subject, deliveryreceipt=$deliveryreceipt, msgishtml=$msgishtml", LOG_DEBUG);
  108. // Detect if message is HTML (use fast method)
  109. if ($msgishtml == -1)
  110. {
  111. $this->msgishtml = 0;
  112. if (dol_textishtml($msg)) $this->msgishtml = 1;
  113. }
  114. else
  115. {
  116. $this->msgishtml = $msgishtml;
  117. }
  118. // Detect images
  119. if ($this->msgishtml)
  120. {
  121. $this->html = $msg;
  122. $findimg = $this->findHtmlImages($conf->fckeditor->dir_output);
  123. // Define if there is at least one file
  124. if ($findimg)
  125. {
  126. foreach ($this->html_images as $i => $val)
  127. {
  128. if ($this->html_images[$i])
  129. {
  130. $this->atleastoneimage=1;
  131. dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG);
  132. }
  133. }
  134. }
  135. }
  136. // Define if there is at least one file
  137. foreach ($filename_list as $i => $val)
  138. {
  139. if ($filename_list[$i])
  140. {
  141. $this->atleastonefile=1;
  142. dol_syslog("CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].", mimetype_list[$i]=".$mimetype_list[$i]." mimefilename_list[$i]=".$mimefilename_list[$i], LOG_DEBUG);
  143. }
  144. }
  145. // Add autocopy to
  146. if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) $addr_bcc.=($addr_bcc?', ':'').$conf->global->MAIN_MAIL_AUTOCOPY_TO;
  147. // Action according to choosed sending method
  148. if ($conf->global->MAIN_MAIL_SENDMODE == 'mail')
  149. {
  150. // Use mail php function (default PHP method)
  151. // ------------------------------------------
  152. $smtp_headers = "";
  153. $mime_headers = "";
  154. $text_body = "";
  155. $files_encoded = "";
  156. // Define smtp_headers
  157. $this->subject = $subject;
  158. $this->addr_from = $from;
  159. $this->errors_to = $errors_to;
  160. $this->addr_to = $to;
  161. $this->addr_cc = $addr_cc;
  162. $this->addr_bcc = $addr_bcc;
  163. $this->deliveryreceipt = $deliveryreceipt;
  164. $smtp_headers = $this->write_smtpheaders();
  165. // Define mime_headers
  166. $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
  167. if (! empty($this->html))
  168. {
  169. if (!empty($css))
  170. {
  171. $this->css = $css;
  172. $this->buildCSS();
  173. }
  174. $msg = $this->html;
  175. }
  176. // Define body in text_body
  177. $text_body = $this->write_body($msg);
  178. // Encode images
  179. if ($this->atleastoneimage)
  180. {
  181. $images_encoded = $this->write_images($this->images_encoded);
  182. // always end related and end alternative after inline images
  183. $images_encoded.= "--" . $this->related_boundary . "--" . $this->eol;
  184. $images_encoded.= $this->eol . "--" . $this->alternative_boundary . "--" . $this->eol;
  185. $images_encoded.= $this->eol;
  186. }
  187. // Add attachments to text_encoded
  188. if ($this->atleastonefile)
  189. {
  190. $files_encoded = $this->write_files($filename_list,$mimetype_list,$mimefilename_list);
  191. }
  192. // We now define $this->headers et $this->message
  193. $this->headers = $smtp_headers . $mime_headers;
  194. $this->message = $text_body . $images_encoded . $files_encoded;
  195. $this->message.= "--" . $this->mixed_boundary . "--" . $this->eol;
  196. // On nettoie le header pour qu'il ne se termine pas par un retour chariot.
  197. // Ceci evite aussi les lignes vides en fin qui peuvent etre interpretees
  198. // comme des injections mail par les serveurs de messagerie.
  199. $this->headers = preg_replace("/([\r\n]+)$/i","",$this->headers);
  200. }
  201. else if ($conf->global->MAIN_MAIL_SENDMODE == 'smtps')
  202. {
  203. // Use SMTPS library
  204. // ------------------------------------------
  205. require_once(DOL_DOCUMENT_ROOT."/core/class/smtps.class.php");
  206. $smtps = new SMTPs();
  207. $smtps->setCharSet($conf->file->character_set_client);
  208. $smtps->setSubject($this->encodetorfc2822($subject));
  209. $smtps->setTO($this->getValidAddress($to,0,1));
  210. $smtps->setFrom($this->getValidAddress($from,0,1));
  211. if (! empty($this->html))
  212. {
  213. if (!empty($css))
  214. {
  215. $this->css = $css;
  216. $this->styleCSS = $this->buildCSS();
  217. }
  218. $msg = $this->html;
  219. $msg = $this->checkIfHTML($msg);
  220. }
  221. if ($this->msgishtml) $smtps->setBodyContent($msg,'html');
  222. else $smtps->setBodyContent($msg,'plain');
  223. if ($this->atleastoneimage)
  224. {
  225. foreach ($this->images_encoded as $img)
  226. {
  227. $smtps->setImageInline($img['image_encoded'],$img['name'],$img['content_type'],$img['cid']);
  228. }
  229. }
  230. if ($this->atleastonefile)
  231. {
  232. foreach ($filename_list as $i => $val)
  233. {
  234. $content=file_get_contents($filename_list[$i]);
  235. $smtps->setAttachment($content,$mimefilename_list[$i],$mimetype_list[$i]);
  236. }
  237. }
  238. $smtps->setCC($addr_cc);
  239. $smtps->setBCC($addr_bcc);
  240. $smtps->setErrorsTo($errors_to);
  241. $smtps->setDeliveryReceipt($deliveryreceipt);
  242. $this->smtps=$smtps;
  243. }
  244. else
  245. {
  246. // Send mail method not correctly defined
  247. // --------------------------------------
  248. return 'Bad value for MAIN_MAIL_SENDMODE constant';
  249. }
  250. }
  251. /**
  252. * Send mail that was prepared by constructor
  253. *
  254. * @return boolean True if mail sent, false otherwise
  255. */
  256. function sendfile()
  257. {
  258. global $conf;
  259. $errorlevel=error_reporting();
  260. error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
  261. $res=false;
  262. if (empty($conf->global->MAIN_DISABLE_ALL_MAILS))
  263. {
  264. // Action according to choosed sending method
  265. if ($conf->global->MAIN_MAIL_SENDMODE == 'mail')
  266. {
  267. // Use mail php function (default PHP method)
  268. // ------------------------------------------
  269. dol_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject, LOG_DEBUG);
  270. dol_syslog("CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
  271. //dol_syslog("CMailFile::sendfile message=\n".$message);
  272. // If Windows, sendmail_from must be defined
  273. if (isset($_SERVER["WINDIR"]))
  274. {
  275. if (empty($this->addr_from)) $this->addr_from = 'robot@mydomain.com';
  276. @ini_set('sendmail_from',$this->getValidAddress($this->addr_from,2));
  277. }
  278. // Forcage parametres
  279. if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_set('SMTP',$conf->global->MAIN_MAIL_SMTP_SERVER);
  280. if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_set('smtp_port',$conf->global->MAIN_MAIL_SMTP_PORT);
  281. $dest=$this->getValidAddress($this->addr_to,2);
  282. if (! $dest)
  283. {
  284. $this->error="Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid";
  285. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  286. }
  287. else
  288. {
  289. dol_syslog("CMailFile::sendfile: mail start HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port'), LOG_DEBUG);
  290. $bounce = ''; // By default
  291. if (! empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F))
  292. {
  293. // le return-path dans les header ne fonctionne pas avec tous les MTA
  294. // Le passage par -f est donc possible si la constante MAIN_MAIL_ALLOW_SENDMAIL_F est definie.
  295. // La variable definie pose des pb avec certains sendmail securisee (option -f refusee car dangereuse)
  296. $bounce .= ($bounce?' ':'').(! empty($conf->global->MAIN_MAIL_ERRORS_TO) ? '-f' . $conf->global->MAIN_MAIL_ERRORS_TO : ($this->addr_from != '' ? '-f' . $this->addr_from : '') );
  297. }
  298. if (! empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) // To force usage of -ba option. This option tells sendmail to read From: or Sender: to setup sender
  299. {
  300. $bounce .= ($bounce?' ':'').'-ba';
  301. }
  302. $this->message=stripslashes($this->message);
  303. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
  304. if (! empty($bounce)) $res = mail($dest,$this->encodetorfc2822($this->subject),$this->message,$this->headers, $bounce);
  305. else $res = mail($dest,$this->encodetorfc2822($this->subject),$this->message,$this->headers);
  306. if (! $res)
  307. {
  308. $this->error="Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Check your server logs and your firewalls setup";
  309. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  310. }
  311. else
  312. {
  313. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  314. }
  315. }
  316. if (isset($_SERVER["WINDIR"]))
  317. {
  318. @ini_restore('sendmail_from');
  319. }
  320. // Forcage parametres
  321. if (! empty($conf->global->MAIN_MAIL_SMTP_SERVER)) ini_restore('SMTP');
  322. if (! empty($conf->global->MAIN_MAIL_SMTP_PORT)) ini_restore('smtp_port');
  323. }
  324. else if ($conf->global->MAIN_MAIL_SENDMODE == 'smtps')
  325. {
  326. // Use SMTPS library
  327. // ------------------------------------------
  328. $this->smtps->setTransportType(0); // Only this method is coded in SMTPs library
  329. // Forcage parametres
  330. if (empty($conf->global->MAIN_MAIL_SMTP_SERVER)) $conf->global->MAIN_MAIL_SMTP_SERVER=ini_get('SMTP');
  331. if (empty($conf->global->MAIN_MAIL_SMTP_PORT)) $conf->global->MAIN_MAIL_SMTP_PORT=ini_get('smtp_port');
  332. // If we use SSL/TLS
  333. $server=$conf->global->MAIN_MAIL_SMTP_SERVER;
  334. if (! empty($conf->global->MAIN_MAIL_EMAIL_TLS) && function_exists('openssl_open')) $server='ssl://'.$server;
  335. $this->smtps->setHost($server);
  336. $this->smtps->setPort($conf->global->MAIN_MAIL_SMTP_PORT); // 25, 465...;
  337. if (! empty($conf->global->MAIN_MAIL_SMTPS_ID)) $this->smtps->setID($conf->global->MAIN_MAIL_SMTPS_ID);
  338. if (! empty($conf->global->MAIN_MAIL_SMTPS_PW)) $this->smtps->setPW($conf->global->MAIN_MAIL_SMTPS_PW);
  339. //$smtps->_msgReplyTo = 'reply@web.com';
  340. $res=true;
  341. $from=$this->smtps->getFrom('org');
  342. if (! $from)
  343. {
  344. $this->error="Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->MAIN_MAIL_SMTP_PORT."<br>Sender address '$from' invalid";
  345. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  346. $res=false;
  347. }
  348. $dest=$this->smtps->getTo();
  349. if (! $dest)
  350. {
  351. $this->error="Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->MAIN_MAIL_SMTP_PORT."<br>Recipient address '$dest' invalid";
  352. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  353. $res=false;
  354. }
  355. if ($res)
  356. {
  357. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->smtps->setDebug(true);
  358. $result=$this->smtps->sendMsg();
  359. //print $result;
  360. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
  361. $result=$this->smtps->getErrors();
  362. if (empty($this->error) && empty($result)) $res=true;
  363. else
  364. {
  365. if (empty($this->error)) $this->error=$result;
  366. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  367. $res=false;
  368. }
  369. }
  370. }
  371. else
  372. {
  373. // Send mail method not correctly defined
  374. // --------------------------------------
  375. return 'Bad value for MAIN_MAIL_SENDMODE constant';
  376. }
  377. }
  378. else
  379. {
  380. $this->error='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
  381. dol_syslog("CMailFile::sendfile: ".$this->error, LOG_WARNING);
  382. }
  383. error_reporting($errorlevel); // Reactive niveau erreur origine
  384. return $res;
  385. }
  386. // Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word
  387. function encodetorfc2822($stringtoencode)
  388. {
  389. global $conf;
  390. return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
  391. }
  392. /**
  393. * Read a file on disk and return encoded content for emails (mode = 'mail')
  394. *
  395. * @param sourcefile
  396. * @return <0 if KO, encoded string if OK
  397. */
  398. function _encode_file($sourcefile)
  399. {
  400. $newsourcefile=utf8_check($sourcefile)?utf8_decode($sourcefile):$sourcefile; // is_readable and file_get_contents need ISO filename
  401. if (is_readable($newsourcefile))
  402. {
  403. $contents = file_get_contents($newsourcefile); // Need PHP 4.3
  404. $encoded = chunk_split(base64_encode($contents), 68, $this->eol);
  405. return $encoded;
  406. }
  407. else
  408. {
  409. $this->error="Error: Can't read file '$sourcefile'";
  410. dol_syslog("CMailFile::encode_file: ".$this->error, LOG_ERR);
  411. return -1;
  412. }
  413. }
  414. /**
  415. * Write content of a SMTP request into a dump file (mode = all)
  416. * Used for debugging.
  417. */
  418. function dump_mail()
  419. {
  420. global $conf,$dolibarr_main_data_root;
  421. if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
  422. {
  423. $fp = fopen($dolibarr_main_data_root."/dolibarr_mail.log","w");
  424. if ($conf->global->MAIN_MAIL_SENDMODE == 'mail')
  425. {
  426. fputs($fp, $this->headers);
  427. fputs($fp, $this->eol); // This eol is added by the mail function, so we add it in log
  428. fputs($fp, $this->message);
  429. }
  430. elseif ($conf->global->MAIN_MAIL_SENDMODE == 'smtps')
  431. {
  432. fputs($fp, $this->smtps->log);
  433. }
  434. fclose($fp);
  435. if (! empty($conf->global->MAIN_UMASK))
  436. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  437. }
  438. }
  439. /**
  440. * Correct an uncomplete html string
  441. *
  442. * @param $msg
  443. * @return
  444. */
  445. function checkIfHTML($msg)
  446. {
  447. if (!preg_match('/^[\s\t]*<html/i',$msg))
  448. {
  449. $out = "<html><head><title></title>";
  450. if (!empty($this->styleCSS)) $out.= $this->styleCSS;
  451. $out.= "</head><body";
  452. if (!empty($this->bodyCSS)) $out.= $this->bodyCSS;
  453. $out.= ">";
  454. $out.= $msg;
  455. $out.= "</body></html>";
  456. }
  457. else
  458. {
  459. $out = $msg;
  460. }
  461. return $out;
  462. }
  463. /**
  464. * Build a css style (mode = all)
  465. *
  466. * @return css
  467. */
  468. function buildCSS()
  469. {
  470. if (! empty($this->css))
  471. {
  472. // Style CSS
  473. $this->styleCSS = '<style type="text/css">';
  474. $this->styleCSS.= 'body {';
  475. if ($this->css['bgcolor'])
  476. {
  477. $this->styleCSS.= ' background-color: '.$this->css['bgcolor'].';';
  478. $this->bodyCSS.= ' BGCOLOR="'.$this->css['bgcolor'].'"';
  479. }
  480. if ($this->css['bgimage'])
  481. {
  482. // TODO recuperer cid
  483. $this->styleCSS.= ' background-image: url("cid:'.$this->css['bgimage_cid'].'");';
  484. }
  485. $this->styleCSS.= '}';
  486. $this->styleCSS.= '</style>';
  487. }
  488. }
  489. /**
  490. * Create SMTP headers (mode = 'mail')
  491. *
  492. * @return smtp headers
  493. */
  494. function write_smtpheaders()
  495. {
  496. global $conf;
  497. $out = "";
  498. // Sender
  499. //$out .= "X-Sender: ".getValidAddress($this->addr_from,2).$this->eol;
  500. $out .= "From: ".$this->getValidAddress($this->addr_from,0,1).$this->eol;
  501. $out .= "Return-Path: ".$this->getValidAddress($this->addr_from,0,1).$this->eol;
  502. if (isset($this->reply_to) && $this->reply_to) $out .= "Reply-To: ".$this->getValidAddress($this->reply_to,2).$this->eol;
  503. if (isset($this->errors_to) && $this->errors_to) $out .= "Errors-To: ".$this->getValidAddress($this->errors_to,2).$this->eol;
  504. // Receiver
  505. if (isset($this->addr_cc) && $this->addr_cc) $out .= "Cc: ".$this->getValidAddress($this->addr_cc,2).$this->eol;
  506. if (isset($this->addr_bcc) && $this->addr_bcc) $out .= "Bcc: ".$this->getValidAddress($this->addr_bcc,2).$this->eol;
  507. // Accuse reception
  508. if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) $out .= "Disposition-Notification-To: ".$this->getValidAddress($this->addr_from,2).$this->eol;
  509. //$out .= "X-Priority: 3".$this->eol;
  510. $out.= "X-Mailer: Dolibarr version " . DOL_VERSION ." (using php mail)".$this->eol;
  511. $out.= "MIME-Version: 1.0".$this->eol;
  512. $out.= "Content-Type: multipart/mixed; boundary=\"".$this->mixed_boundary."\"".$this->eol;
  513. $out.= "Content-Transfer-Encoding: 8bit".$this->eol;
  514. dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out);
  515. return $out;
  516. }
  517. /**
  518. * Create header MIME (mode = 'mail')
  519. *
  520. * @param filename_list
  521. * @param mimefilename_list
  522. * @return mime headers
  523. */
  524. function write_mimeheaders($filename_list, $mimefilename_list)
  525. {
  526. $mimedone=0;
  527. $out = "";
  528. if ($filename_list)
  529. {
  530. $filename_list_size=count($filename_list);
  531. for($i=0;$i < $filename_list_size;$i++)
  532. {
  533. if ($filename_list[$i])
  534. {
  535. if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
  536. $out.= "X-attachments: $filename_list[$i]".$this->eol;
  537. }
  538. }
  539. }
  540. dol_syslog("CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
  541. return $out;
  542. }
  543. /**
  544. * Return email content (mode = 'mail')
  545. *
  546. * @param msgtext
  547. */
  548. function write_body($msgtext)
  549. {
  550. global $conf;
  551. $out='';
  552. if ($this->atleastoneimage)
  553. {
  554. $out.= "--" . $this->mixed_boundary . $this->eol;
  555. $out.= "Content-Type: multipart/alternative; boundary=\"".$this->alternative_boundary."\"".$this->eol;
  556. $out.= $this->eol;
  557. $out.= "--" . $this->alternative_boundary . $this->eol;
  558. }
  559. else
  560. {
  561. $out.= "--" . $this->mixed_boundary . $this->eol;
  562. }
  563. if ($this->msgishtml)
  564. {
  565. // Check if html header already in message
  566. $strContent = $this->checkIfHTML($msgtext);
  567. }
  568. else
  569. {
  570. $strContent.= $msgtext;
  571. }
  572. // Make RFC821 Compliant, replace bare linefeeds
  573. $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $strContent );
  574. //$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems bugged
  575. $strContent = rtrim(wordwrap($strContent));
  576. if ($this->msgishtml)
  577. {
  578. if ($this->atleastoneimage)
  579. {
  580. $out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  581. $out.= $this->eol.strip_tags($strContent).$this->eol; // Add plain text message
  582. $out.= "--" . $this->alternative_boundary . $this->eol;
  583. $out.= "Content-Type: multipart/related; boundary=\"".$this->related_boundary."\"".$this->eol;
  584. $out.= $this->eol;
  585. $out.= "--" . $this->related_boundary . $this->eol;
  586. }
  587. $out.= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
  588. $out.= $this->eol.$strContent.$this->eol;
  589. }
  590. else
  591. {
  592. $out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  593. $out.= $this->eol.$strContent.$this->eol;
  594. }
  595. $out.= $this->eol;
  596. return $out;
  597. }
  598. /**
  599. * Attach file to email (mode = 'mail')
  600. *
  601. * @param filename_list Tableau
  602. * @param mimetype_list Tableau
  603. * @param mimefilename_list Tableau
  604. * @return out Chaine fichiers encodes
  605. */
  606. function write_files($filename_list,$mimetype_list,$mimefilename_list)
  607. {
  608. $out = '';
  609. $filename_list_size=count($filename_list);
  610. for($i=0;$i < $filename_list_size;$i++)
  611. {
  612. if ($filename_list[$i])
  613. {
  614. dol_syslog("CMailFile::write_files: i=$i");
  615. $encoded = $this->_encode_file($filename_list[$i]);
  616. if ($encoded >= 0)
  617. {
  618. if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
  619. if (! $mimetype_list[$i]) { $mimetype_list[$i] = "application/octet-stream"; }
  620. $out.= "--" . $this->mixed_boundary . $this->eol;
  621. $out.= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
  622. $out.= "Content-Type: " . $mimetype_list[$i] . "; name=\"".$filename_list[$i]."\"".$this->eol;
  623. $out.= "Content-Transfer-Encoding: base64".$this->eol;
  624. $out.= "Content-Description: File Attachment".$this->eol;
  625. $out.= $this->eol;
  626. $out.= $encoded;
  627. $out.= $this->eol;
  628. //$out.= $this->eol;
  629. }
  630. else
  631. {
  632. return $encoded;
  633. }
  634. }
  635. }
  636. return $out;
  637. }
  638. /**
  639. * Attach an image to email (mode = 'mail')
  640. *
  641. * @param images_list Tableau
  642. * @return out Chaine images encodees
  643. */
  644. function write_images($images_list)
  645. {
  646. $out = '';
  647. if ($images_list)
  648. {
  649. foreach ($images_list as $img)
  650. {
  651. dol_syslog("CMailFile::write_images: i=$i");
  652. $out.= "--" . $this->related_boundary . $this->eol; // always related for an inline image
  653. $out.= "Content-Type: " . $img["content_type"] . "; name=\"".$img["name"]."\"".$this->eol;
  654. $out.= "Content-Transfer-Encoding: base64".$this->eol;
  655. $out.= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
  656. $out.= "Content-ID: <".$img["cid"].">".$this->eol;
  657. $out.= $this->eol;
  658. $out.= $img["image_encoded"];
  659. $out.= $this->eol;
  660. }
  661. }
  662. return $out;
  663. }
  664. /**
  665. * Try to create a socket connection
  666. *
  667. * @param $host Add ssl:// for SSL/TLS.
  668. * @param $port Example: 25, 465
  669. * @return Socket id if ok, 0 if KO
  670. */
  671. function check_server_port($host,$port)
  672. {
  673. $_retVal=0;
  674. $timeout=5; // Timeout in seconds
  675. if (function_exists('fsockopen'))
  676. {
  677. dol_syslog("Try socket connection to host=".$host." port=".$port);
  678. //See if we can connect to the SMTP server
  679. if ( $socket = @fsockopen($host, // Host to test, IP or domain. Add ssl:// for SSL/TLS.
  680. $port, // which Port number to use
  681. $errno, // actual system level error
  682. $errstr, // and any text that goes with the error
  683. $timeout) ) // timeout for reading/writing data over the socket
  684. {
  685. // Windows still does not have support for this timeout function
  686. if (function_exists('stream_set_timeout')) stream_set_timeout($socket, $timeout, 0);
  687. dol_syslog("Now we wait for answer 220");
  688. // Check response from Server
  689. if ( $_retVal = $this->server_parse($socket, "220") ) $_retVal = $socket;
  690. }
  691. else
  692. {
  693. $this->error = 'Error '.$errno.' - '.$errstr;
  694. }
  695. }
  696. return $_retVal;
  697. }
  698. /**
  699. * This function has been modified as provided by SirSir to allow multiline responses when
  700. * using SMTP Extensions.
  701. *
  702. * @param socket
  703. * @param response
  704. * @return boolean
  705. */
  706. function server_parse($socket, $response)
  707. {
  708. $_retVal = true; // Indicates if Object was created or not
  709. $server_response = '';
  710. while ( substr($server_response,3,1) != ' ' )
  711. {
  712. if( !( $server_response = fgets($socket, 256) ) )
  713. {
  714. $this->error="Couldn't get mail server response codes";
  715. $_retVal = false;
  716. }
  717. }
  718. if( !( substr($server_response, 0, 3) == $response ) )
  719. {
  720. $this->error="Ran into problems sending Mail.\r\nResponse: $server_response";
  721. $_retVal = false;
  722. }
  723. return $_retVal;
  724. }
  725. /**
  726. * Seearch images into html message and init array this->images_encoded if found
  727. *
  728. * @param images_dir Location of physical images files
  729. * @return int >0 if OK, <0 if KO
  730. */
  731. function findHtmlImages($images_dir)
  732. {
  733. // Build the list of image extensions
  734. $extensions = array_keys($this->image_types);
  735. preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $matches);
  736. if ($matches)
  737. {
  738. $i=0;
  739. foreach ($matches[1] as $full)
  740. {
  741. if (preg_match('/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i',$full,$regs))
  742. {
  743. $img = $regs[1];
  744. if (file_exists($images_dir.'/'.$img))
  745. {
  746. // Image path in src
  747. $src = preg_quote($full,'/');
  748. // Image full path
  749. $this->html_images[$i]["fullpath"] = $images_dir.'/'.$img;
  750. // Image name
  751. $this->html_images[$i]["name"] = $img;
  752. // Content type
  753. $ext = preg_replace('/^.*\.(\w{3,4})$/e', 'strtolower("$1")', $img);
  754. $this->html_images[$i]["content_type"] = $this->image_types[$ext];
  755. // cid
  756. $this->html_images[$i]["cid"] = dol_hash(uniqid(time()));
  757. $this->html = preg_replace("/src=\"$src\"|src='$src'/i", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
  758. }
  759. $i++;
  760. }
  761. }
  762. if (!empty($this->html_images))
  763. {
  764. $inline = array();
  765. $i=0;
  766. foreach ($this->html_images as $img)
  767. {
  768. $fullpath = $images_dir.'/'.$img["name"];
  769. // If duplicate images are embedded, they may show up as attachments, so remove them.
  770. if (!in_array($fullpath,$inline))
  771. {
  772. // Read image file
  773. if ($image = file_get_contents($fullpath))
  774. {
  775. // On garde que le nom de l'image
  776. preg_match('/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i',$img["name"],$regs);
  777. $imgName = $regs[1];
  778. $this->images_encoded[$i]['name'] = $imgName;
  779. $this->images_encoded[$i]['content_type'] = $img["content_type"];
  780. $this->images_encoded[$i]['cid'] = $img["cid"];
  781. // Encodage de l'image
  782. $this->images_encoded[$i]["image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
  783. $inline[] = $fullpath;
  784. }
  785. }
  786. $i++;
  787. }
  788. }
  789. else
  790. {
  791. return -1;
  792. }
  793. return 1;
  794. }
  795. else
  796. {
  797. return 0;
  798. }
  799. }
  800. /**
  801. * Return an address for SMTP protocol
  802. *
  803. * @param adresses Example: 'John Doe <john@doe.com>' or 'john@doe.com'
  804. * @param format 0=Auto, 1=emails with <>, 2=emails without <>
  805. * @param encode 1=Encode name to RFC2822
  806. * @return string If format 1: '<john@doe.com>' or 'John Doe <john@doe.com>'
  807. * If format 2: 'john@doe.com'
  808. */
  809. function getValidAddress($adresses,$format,$encode='')
  810. {
  811. global $conf;
  812. $ret='';
  813. $arrayaddress=explode(',',$adresses);
  814. // Boucle sur chaque composant de l'adresse
  815. foreach($arrayaddress as $val)
  816. {
  817. if (preg_match('/^(.*)<(.*)>$/i',trim($val),$regs))
  818. {
  819. $name = trim($regs[1]);
  820. $email = trim($regs[2]);
  821. }
  822. else
  823. {
  824. $name = '';
  825. $email = trim($val);
  826. }
  827. if ($email)
  828. {
  829. $newemail='';
  830. if ($format == 2)
  831. {
  832. $newemail=$email;
  833. }
  834. if ($format == 1)
  835. {
  836. $newemail='<'.$email.'>';
  837. }
  838. if ($format == 0)
  839. {
  840. if ($conf->global->MAIN_MAIL_NO_FULL_EMAIL) $newemail='<'.$email.'>';
  841. elseif (! $name) $newemail='<'.$email.'>';
  842. else $newemail=($encode?$this->encodetorfc2822($name):$name).' <'.$email.'>';
  843. }
  844. $ret=($ret ? $ret.',' : '').$newemail;
  845. }
  846. }
  847. return $ret;
  848. }
  849. }
  850. ?>