CMailFile.class.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. <?php
  2. /**
  3. * Copyright (C) Dan Potter
  4. * Copyright (C) Eric Seigne
  5. * Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  6. * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
  7. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  8. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. * or see http://www.gnu.org/
  23. *
  24. * Lots of code inspired from Dan Potter's CMailFile class
  25. */
  26. /**
  27. * \file htdocs/core/class/CMailFile.class.php
  28. * \brief File of class to send emails (with attachments or not)
  29. */
  30. /**
  31. * Class to send emails (with attachments or not)
  32. * Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext);
  33. * $mailfile->sendfile();
  34. */
  35. class CMailFile
  36. {
  37. public $sendcontext;
  38. public $sendmode;
  39. public $sendsetup;
  40. var $subject; // Topic: Subject of email
  41. var $addr_from; // From: Label and EMail of sender (must include '<>'). For example '<myemail@example.com>' or 'John Doe <myemail@example.com>' or '<myemail+trackingid@example.com>'). Note that with gmail smtps, value here is forced by google to account (but not the reply-to).
  42. // Sender: Who send the email ("Sender" has sent emails on behalf of "From").
  43. // Use it when the "From" is an email of a domain that is a SPF protected domain, and sending smtp server is not this domain. In such case, add Sender field with an email of the protected domain.
  44. // Return-Path: Email where to send bounds.
  45. var $reply_to; // Reply-To: Email where to send replies from mailer software (mailer use From if reply-to not defined, Gmail use gmail account if reply-to not defined)
  46. var $errors_to; // Errors-To: Email where to send errors.
  47. var $addr_to;
  48. var $addr_cc;
  49. var $addr_bcc;
  50. var $trackid;
  51. var $mixed_boundary;
  52. var $related_boundary;
  53. var $alternative_boundary;
  54. var $deliveryreceipt;
  55. var $eol;
  56. var $eol2;
  57. /**
  58. * @var string Error code (or message)
  59. */
  60. public $error='';
  61. var $smtps; // Contains SMTPs object (if this method is used)
  62. var $phpmailer; // Contains PHPMailer object (if this method is used)
  63. /**
  64. * @var string CSS
  65. */
  66. public $css;
  67. //! Defined css style for body background
  68. var $styleCSS;
  69. //! Defined background directly in body tag
  70. var $bodyCSS;
  71. var $headers;
  72. var $message;
  73. // Image
  74. var $html;
  75. var $image_boundary;
  76. var $atleastoneimage=0; // at least one image file with file=xxx.ext into content (TODO Debug this. How can this case be tested. Remove if not used).
  77. var $html_images=array();
  78. var $images_encoded=array();
  79. var $image_types = array(
  80. 'gif' => 'image/gif',
  81. 'jpg' => 'image/jpeg',
  82. 'jpeg' => 'image/jpeg',
  83. 'jpe' => 'image/jpeg',
  84. 'bmp' => 'image/bmp',
  85. 'png' => 'image/png',
  86. 'tif' => 'image/tiff',
  87. 'tiff' => 'image/tiff',
  88. );
  89. /**
  90. * CMailFile
  91. *
  92. * @param string $subject Topic/Subject of mail
  93. * @param string $to Recipients emails (RFC 2822: "Name firstname <email>[, ...]" or "email[, ...]" or "<email>[, ...]"). Note: the keyword '__SUPERVISOREMAIL__' is not allowed here and must be replaced by caller.
  94. * @param string $from Sender email (RFC 2822: "Name firstname <email>[, ...]" or "email[, ...]" or "<email>[, ...]")
  95. * @param string $msg Message
  96. * @param array $filename_list List of files to attach (full path of filename on file system)
  97. * @param array $mimetype_list List of MIME type of attached files
  98. * @param array $mimefilename_list List of attached file name in message
  99. * @param string $addr_cc Email cc
  100. * @param string $addr_bcc Email bcc (Note: This is autocompleted with MAIN_MAIL_AUTOCOPY_TO if defined)
  101. * @param int $deliveryreceipt Ask a delivery receipt
  102. * @param int $msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown make autodetection (with fast mode, not reliable)
  103. * @param string $errors_to Email for errors-to
  104. * @param string $css Css option
  105. * @param string $trackid Tracking string (contains type and id of related element)
  106. * @param string $moreinheader More in header. $moreinheader must contains the "\r\n" (TODO not supported for other MAIL_SEND_MODE different than 'phpmail' and 'smtps' for the moment)
  107. * @param string $sendcontext 'standard', 'emailing', ... (used to define with sending mode and parameters to use)
  108. * @param string $replyto Reply-to email (will be set to same value than From by default if not provided)
  109. */
  110. function __construct($subject, $to, $from, $msg, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=0, $errors_to='', $css='', $trackid='', $moreinheader='', $sendcontext='standard', $replyto='')
  111. {
  112. global $conf, $dolibarr_main_data_root;
  113. $this->sendcontext = $sendcontext;
  114. if (empty($replyto)) $replyto=$from;
  115. // Define this->sendmode
  116. $this->sendmode = '';
  117. if ($this->sendcontext == 'emailing' && !empty($conf->global->MAIN_MAIL_SENDMODE_EMAILING) && $conf->global->MAIN_MAIL_SENDMODE_EMAILING != 'default')
  118. {
  119. $this->sendmode = $conf->global->MAIN_MAIL_SENDMODE_EMAILING;
  120. }
  121. if (empty($this->sendmode)) $this->sendmode=$conf->global->MAIN_MAIL_SENDMODE;
  122. if (empty($this->sendmode)) $this->sendmode='mail';
  123. // We define end of line (RFC 821).
  124. $this->eol="\r\n";
  125. // We define end of line for header fields (RFC 822bis section 2.3 says header must contains \r\n).
  126. $this->eol2="\r\n";
  127. if (! empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA))
  128. {
  129. $this->eol="\n";
  130. $this->eol2="\n";
  131. $moreinheader = str_replace("\r\n","\n",$moreinheader);
  132. }
  133. // On defini mixed_boundary
  134. $this->mixed_boundary = "multipart_x." . time() . ".x_boundary";
  135. // On defini related_boundary
  136. $this->related_boundary = 'mul_'.dol_hash(uniqid("dolibarr2"), 3); // Force md5 hash (does not contains special chars)
  137. // On defini alternative_boundary
  138. $this->alternative_boundary = 'mul_'.dol_hash(uniqid("dolibarr3"), 3); // Force md5 hash (does not contains special chars)
  139. dol_syslog("CMailFile::CMailfile: sendmode=".$this->sendmode." charset=".$conf->file->character_set_client." from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to, trackid=$trackid sendcontext=$sendcontext", LOG_DEBUG);
  140. dol_syslog("CMailFile::CMailfile: subject=".$subject.", deliveryreceipt=".$deliveryreceipt.", msgishtml=".$msgishtml, LOG_DEBUG);
  141. if (empty($subject))
  142. {
  143. dol_syslog("CMailFile::CMailfile: Try to send an email with empty subject");
  144. $this->error='ErrorSubjectIsRequired';
  145. return;
  146. }
  147. if (empty($msg))
  148. {
  149. dol_syslog("CMailFile::CMailfile: Try to send an email with empty body");
  150. $msg='.'; // Avoid empty message (with empty message conten show a multipart structure)
  151. }
  152. // Detect if message is HTML (use fast method)
  153. if ($msgishtml == -1)
  154. {
  155. $this->msgishtml = 0;
  156. if (dol_textishtml($msg)) $this->msgishtml = 1;
  157. }
  158. else
  159. {
  160. $this->msgishtml = $msgishtml;
  161. }
  162. global $dolibarr_main_url_root;
  163. // Define $urlwithroot
  164. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  165. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  166. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  167. // Replace relative /viewimage to absolute path
  168. $msg = preg_replace('/src="'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage\.php/ims', 'src="'.$urlwithroot.'/viewimage.php', $msg, -1, $nbrep);
  169. if (! empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) $this->msgishtml=1; // To force to send everything with content type html.
  170. // Detect images
  171. if ($this->msgishtml)
  172. {
  173. $this->html = $msg;
  174. if (! empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS))
  175. {
  176. $findimg = $this->findHtmlImages($dolibarr_main_data_root.'/medias');
  177. }
  178. // Define if there is at least one file
  179. if ($findimg)
  180. {
  181. foreach ($this->html_images as $i => $val)
  182. {
  183. if ($this->html_images[$i])
  184. {
  185. $this->atleastoneimage=1;
  186. dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG);
  187. }
  188. }
  189. }
  190. }
  191. // Define if there is at least one file
  192. if (is_array($filename_list))
  193. {
  194. foreach ($filename_list as $i => $val)
  195. {
  196. if ($filename_list[$i])
  197. {
  198. $this->atleastonefile=1;
  199. dol_syslog("CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].", mimetype_list[$i]=".$mimetype_list[$i]." mimefilename_list[$i]=".$mimefilename_list[$i], LOG_DEBUG);
  200. }
  201. }
  202. }
  203. // Add autocopy to (Note: Adding bcc for specific modules are also done from pages)
  204. if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) $addr_bcc.=($addr_bcc?', ':'').$conf->global->MAIN_MAIL_AUTOCOPY_TO;
  205. // Action according to choosed sending method
  206. if ($this->sendmode == 'mail')
  207. {
  208. // Use mail php function (default PHP method)
  209. // ------------------------------------------
  210. $smtp_headers = "";
  211. $mime_headers = "";
  212. $text_body = "";
  213. $files_encoded = "";
  214. // Define smtp_headers
  215. $this->subject = $subject;
  216. $this->addr_from = $from;
  217. $this->reply_to = $replyto;
  218. $this->errors_to = $errors_to;
  219. $this->addr_to = $to;
  220. $this->addr_cc = $addr_cc;
  221. $this->addr_bcc = $addr_bcc;
  222. $this->deliveryreceipt = $deliveryreceipt;
  223. $this->trackid = $trackid;
  224. $smtp_headers = $this->write_smtpheaders();
  225. if (! empty($moreinheader)) $smtp_headers.=$moreinheader; // $moreinheader contains the \r\n
  226. // Define mime_headers
  227. $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
  228. if (! empty($this->html))
  229. {
  230. if (!empty($css))
  231. {
  232. $this->css = $css;
  233. $this->buildCSS(); // Build a css style (mode = all) into this->styleCSS and this->bodyCSS
  234. }
  235. $msg = $this->html;
  236. }
  237. // Define body in text_body
  238. $text_body = $this->write_body($msg);
  239. // Add attachments to text_encoded
  240. if ($this->atleastonefile)
  241. {
  242. $files_encoded = $this->write_files($filename_list,$mimetype_list,$mimefilename_list);
  243. }
  244. // We now define $this->headers and $this->message
  245. $this->headers = $smtp_headers . $mime_headers;
  246. // On nettoie le header pour qu'il ne se termine pas par un retour chariot.
  247. // Ceci evite aussi les lignes vides en fin qui peuvent etre interpretees
  248. // comme des injections mail par les serveurs de messagerie.
  249. $this->headers = preg_replace("/([\r\n]+)$/i","",$this->headers);
  250. //$this->message = $this->eol.'This is a message with multiple parts in MIME format.'.$this->eol;
  251. $this->message = 'This is a message with multiple parts in MIME format.'.$this->eol;
  252. $this->message.= $text_body . $files_encoded;
  253. $this->message.= "--" . $this->mixed_boundary . "--" . $this->eol;
  254. }
  255. else if ($this->sendmode == 'smtps')
  256. {
  257. // Use SMTPS library
  258. // ------------------------------------------
  259. require_once DOL_DOCUMENT_ROOT.'/core/class/smtps.class.php';
  260. $smtps = new SMTPs();
  261. $smtps->setCharSet($conf->file->character_set_client);
  262. $smtps->setSubject($this->encodetorfc2822($subject));
  263. $smtps->setTO($this->getValidAddress($to,0,1));
  264. $smtps->setFrom($this->getValidAddress($from,0,1));
  265. $smtps->setTrackId($trackid);
  266. $smtps->setReplyTo($this->getValidAddress($replyto,0,1));
  267. if (! empty($moreinheader)) $smtps->setMoreInHeader($moreinheader);
  268. if (! empty($this->html))
  269. {
  270. if (!empty($css))
  271. {
  272. $this->css = $css;
  273. $this->buildCSS();
  274. }
  275. $msg = $this->html;
  276. $msg = $this->checkIfHTML($msg);
  277. }
  278. if ($this->msgishtml) $smtps->setBodyContent($msg,'html');
  279. else $smtps->setBodyContent($msg,'plain');
  280. if ($this->atleastoneimage)
  281. {
  282. foreach ($this->images_encoded as $img)
  283. {
  284. $smtps->setImageInline($img['image_encoded'],$img['name'],$img['content_type'],$img['cid']);
  285. }
  286. }
  287. if ($this->atleastonefile)
  288. {
  289. foreach ($filename_list as $i => $val)
  290. {
  291. $content=file_get_contents($filename_list[$i]);
  292. $smtps->setAttachment($content,$mimefilename_list[$i],$mimetype_list[$i]);
  293. }
  294. }
  295. $smtps->setCC($addr_cc);
  296. $smtps->setBCC($addr_bcc);
  297. $smtps->setErrorsTo($errors_to);
  298. $smtps->setDeliveryReceipt($deliveryreceipt);
  299. $this->smtps=$smtps;
  300. }
  301. else if ($this->sendmode == 'swiftmailer')
  302. {
  303. // Use Swift Mailer library
  304. // ------------------------------------------
  305. $host = dol_getprefix('email');
  306. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
  307. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Exception/InvalidEmail.php';
  308. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Exception/NoDomainPart.php';
  309. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/EmailParser.php';
  310. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/EmailLexer.php';
  311. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/EmailValidator.php';
  312. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Warning/Warning.php';
  313. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Warning/LocalTooLong.php';
  314. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Parser/Parser.php';
  315. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Parser/DomainPart.php';
  316. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Parser/LocalPart.php';
  317. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Validation/EmailValidation.php';
  318. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/egulias/email-validator/EmailValidator/Validation/RFCValidation.php';
  319. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/classes/Swift/InputByteStream.php';
  320. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/classes/Swift/Signer.php';
  321. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/classes/Swift/Signers/HeaderSigner.php';
  322. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/classes/Swift/Signers/DKIMSigner.php';
  323. //require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/classes/Swift/SignedMessage.php';
  324. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
  325. // Create the message
  326. //$this->message = Swift_Message::newInstance();
  327. $this->message = new Swift_Message();
  328. //$this->message = new Swift_SignedMessage();
  329. // Adding a trackid header to a message
  330. $headers = $this->message->getHeaders();
  331. $headers->addTextHeader('X-Dolibarr-TRACKID', $trackid . '@' . $host);
  332. $headerID = time() . '.swiftmailer-dolibarr-' . $trackid . '@' . $host;
  333. $msgid = $headers->get('Message-ID');
  334. $msgid->setId($headerID);
  335. $headers->addIdHeader('References', $headerID);
  336. // TODO if (! empty($moreinheader)) ...
  337. // Give the message a subject
  338. try {
  339. $result = $this->message->setSubject($subject);
  340. } catch (Exception $e) {
  341. $this->errors[] = $e->getMessage();
  342. }
  343. // Set the From address with an associative array
  344. //$this->message->setFrom(array('john@doe.com' => 'John Doe'));
  345. if (! empty($from)) {
  346. try {
  347. $result = $this->message->setFrom($this->getArrayAddress($from));
  348. } catch (Exception $e) {
  349. $this->errors[] = $e->getMessage();
  350. }
  351. }
  352. // Set the To addresses with an associative array
  353. if (! empty($to)) {
  354. try {
  355. $result = $this->message->setTo($this->getArrayAddress($to));
  356. } catch (Exception $e) {
  357. $this->errors[] = $e->getMessage();
  358. }
  359. }
  360. if (! empty($replyto)) {
  361. try {
  362. $result = $this->message->SetReplyTo($this->getArrayAddress($replyto));
  363. } catch (Exception $e) {
  364. $this->errors[] = $e->getMessage();
  365. }
  366. }
  367. try {
  368. $result = $this->message->setCharSet($conf->file->character_set_client);
  369. } catch (Exception $e) {
  370. $this->errors[] = $e->getMessage();
  371. }
  372. if (! empty($this->html))
  373. {
  374. if (!empty($css))
  375. {
  376. $this->css = $css;
  377. $this->buildCSS();
  378. }
  379. $msg = $this->html;
  380. $msg = $this->checkIfHTML($msg);
  381. }
  382. if ($this->atleastoneimage)
  383. {
  384. foreach ($this->images_encoded as $img)
  385. {
  386. //$img['fullpath'],$img['image_encoded'],$img['name'],$img['content_type'],$img['cid']
  387. $attachment = Swift_Image::fromPath($img['fullpath'], $img['content_type']);
  388. // embed image
  389. $imgcid = $this->message->embed($attachment);
  390. // replace cid by the one created by swiftmail in html message
  391. $msg = str_replace("cid:".$img['cid'], $imgcid, $msg);
  392. }
  393. }
  394. if ($this->msgishtml) {
  395. $this->message->setBody($msg,'text/html');
  396. // And optionally an alternative body
  397. $this->message->addPart(html_entity_decode(strip_tags($msg)), 'text/plain');
  398. } else {
  399. $this->message->setBody($msg,'text/plain');
  400. // And optionally an alternative body
  401. $this->message->addPart($msg, 'text/html');
  402. }
  403. if ($this->atleastonefile)
  404. {
  405. foreach ($filename_list as $i => $val)
  406. {
  407. //$this->message->attach(Swift_Attachment::fromPath($filename_list[$i],$mimetype_list[$i]));
  408. $attachment = Swift_Attachment::fromPath($filename_list[$i],$mimetype_list[$i]);
  409. $this->message->attach($attachment);
  410. }
  411. }
  412. if (! empty($addr_cc)) $this->message->setCc($this->getArrayAddress($addr_cc));
  413. if (! empty($addr_bcc)) $this->message->setBcc($this->getArrayAddress($addr_bcc));
  414. //if (! empty($errors_to)) $this->message->setErrorsTo($this->getArrayAddress($errors_to);
  415. if (isset($deliveryreceipt) && $deliveryreceipt == 1) $this->message->setReadReceiptTo($this->getArrayAddress($from));
  416. }
  417. else
  418. {
  419. // Send mail method not correctly defined
  420. // --------------------------------------
  421. $this->error = 'Bad value for sendmode';
  422. }
  423. }
  424. /**
  425. * Send mail that was prepared by constructor.
  426. *
  427. * @return boolean True if mail sent, false otherwise
  428. */
  429. function sendfile()
  430. {
  431. global $conf,$db,$langs;
  432. $errorlevel=error_reporting();
  433. //error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
  434. $res=false;
  435. if (empty($conf->global->MAIN_DISABLE_ALL_MAILS) || !empty($conf->global->MAIN_MAIL_FORCE_SENDTO))
  436. {
  437. require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
  438. $hookmanager = new HookManager($db);
  439. $hookmanager->initHooks(array('mail'));
  440. $parameters=array(); $action='';
  441. $reshook = $hookmanager->executeHooks('sendMail', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  442. if ($reshook < 0)
  443. {
  444. $this->error = "Error in hook maildao sendMail " . $reshook;
  445. dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_ERR);
  446. return $reshook;
  447. }
  448. if ($reshook == 1) // Hook replace standard code
  449. {
  450. return true;
  451. }
  452. // Check number of recipient is lower or equal than MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL
  453. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL=10;
  454. $tmparray1 = explode(',', $this->addr_to);
  455. if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)
  456. {
  457. $this->error = 'Too much recipients in to:';
  458. dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_WARNING);
  459. return false;
  460. }
  461. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL=10;
  462. $tmparray2 = explode(',', $this->addr_cc);
  463. if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)
  464. {
  465. $this->error = 'Too much recipients in cc:';
  466. dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_WARNING);
  467. return false;
  468. }
  469. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL=10;
  470. $tmparray3 = explode(',', $this->addr_bcc);
  471. if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)
  472. {
  473. $this->error = 'Too much recipients in bcc:';
  474. dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_WARNING);
  475. return false;
  476. }
  477. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL=10;
  478. if ((count($tmparray1)+count($tmparray2)+count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)
  479. {
  480. $this->error = 'Too much recipients in to:, cc:, bcc:';
  481. dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_WARNING);
  482. return false;
  483. }
  484. $keyforsmtpserver='MAIN_MAIL_SMTP_SERVER';
  485. $keyforsmtpport ='MAIN_MAIL_SMTP_PORT';
  486. $keyforsmtpid ='MAIN_MAIL_SMTPS_ID';
  487. $keyforsmtppw ='MAIN_MAIL_SMTPS_PW';
  488. $keyfortls ='MAIN_MAIL_EMAIL_TLS';
  489. $keyforstarttls ='MAIN_MAIL_EMAIL_STARTTLS';
  490. if ($this->sendcontext == 'emailing' && !empty($conf->global->MAIN_MAIL_SENDMODE_EMAILING) && $conf->global->MAIN_MAIL_SENDMODE_EMAILING != 'default')
  491. {
  492. $keyforsmtpserver='MAIN_MAIL_SMTP_SERVER_EMAILING';
  493. $keyforsmtpport ='MAIN_MAIL_SMTP_PORT_EMAILING';
  494. $keyforsmtpid ='MAIN_MAIL_SMTPS_ID_EMAILING';
  495. $keyforsmtppw ='MAIN_MAIL_SMTPS_PW_EMAILING';
  496. $keyfortls ='MAIN_MAIL_EMAIL_TLS_EMAILING';
  497. $keyforstarttls ='MAIN_MAIL_EMAIL_STARTTLS_EMAILING';
  498. }
  499. if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO))
  500. {
  501. $this->addr_to = $conf->global->MAIN_MAIL_FORCE_SENDTO;
  502. $this->addr_cc = '';
  503. $this->addr_bcc = '';
  504. }
  505. // Action according to choosed sending method
  506. if ($this->sendmode == 'mail')
  507. {
  508. // Use mail php function (default PHP method)
  509. // ------------------------------------------
  510. dol_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject, LOG_DEBUG);
  511. dol_syslog("CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
  512. //dol_syslog("CMailFile::sendfile message=\n".$message);
  513. // If Windows, sendmail_from must be defined
  514. if (isset($_SERVER["WINDIR"]))
  515. {
  516. if (empty($this->addr_from)) $this->addr_from = 'robot@example.com';
  517. @ini_set('sendmail_from',$this->getValidAddress($this->addr_from,2));
  518. }
  519. // Force parameters
  520. if (! empty($conf->global->$keyforsmtpserver)) ini_set('SMTP',$conf->global->$keyforsmtpserver);
  521. if (! empty($conf->global->$keyforsmtpport)) ini_set('smtp_port',$conf->global->$keyforsmtpport);
  522. $res=true;
  523. if ($res && ! $this->subject)
  524. {
  525. $this->error="Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Subject is empty";
  526. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  527. $res=false;
  528. }
  529. $dest=$this->getValidAddress($this->addr_to,2);
  530. if ($res && ! $dest)
  531. {
  532. $this->error="Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid";
  533. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  534. $res=false;
  535. }
  536. if ($res)
  537. {
  538. $additionnalparam = ''; // By default
  539. if (! empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F))
  540. {
  541. // le "Return-Path" (retour des messages bounced) dans les header ne fonctionne pas avec tous les MTA
  542. // Le forcage de la valeur grace à l'option -f de sendmail est donc possible si la constante MAIN_MAIL_ALLOW_SENDMAIL_F est definie.
  543. // Having this variable defined may create problems with some sendmail (option -f refused)
  544. // Having this variable not defined may create problems with some other sendmail (option -f required)
  545. $additionnalparam .= ($additionnalparam?' ':'').(! empty($conf->global->MAIN_MAIL_ERRORS_TO) ? '-f' . $this->getValidAddress($conf->global->MAIN_MAIL_ERRORS_TO,2) : ($this->addr_from != '' ? '-f' . $this->getValidAddress($this->addr_from,2) : '') );
  546. }
  547. 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
  548. {
  549. $additionnalparam .= ($additionnalparam?' ':'').'-ba';
  550. }
  551. if (! empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) $additionnalparam .= ($additionnalparam?' ':'').'-U '.$additionnalparam; // Use -U to add additionnal params
  552. dol_syslog("CMailFile::sendfile: mail start HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port').", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
  553. $this->message=stripslashes($this->message);
  554. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
  555. if (! empty($additionnalparam)) $res = mail($dest, $this->encodetorfc2822($this->subject), $this->message, $this->headers, $additionnalparam);
  556. else $res = mail($dest, $this->encodetorfc2822($this->subject), $this->message, $this->headers);
  557. if (! $res)
  558. {
  559. $langs->load("errors");
  560. $this->error="Failed to send mail with php mail";
  561. $linuxlike=1;
  562. if (preg_match('/^win/i',PHP_OS)) $linuxlike=0;
  563. if (preg_match('/^mac/i',PHP_OS)) $linuxlike=0;
  564. if (! $linuxlike)
  565. {
  566. $this->error.=" to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port'); // This values are value used only for non linuxlike systems
  567. }
  568. $this->error.=".<br>";
  569. $this->error.=$langs->trans("ErrorPhpMailDelivery");
  570. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  571. }
  572. else
  573. {
  574. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  575. }
  576. }
  577. if (isset($_SERVER["WINDIR"]))
  578. {
  579. @ini_restore('sendmail_from');
  580. }
  581. // Restore parameters
  582. if (! empty($conf->global->$keyforsmtpserver)) ini_restore('SMTP');
  583. if (! empty($conf->global->$keyforsmtpport)) ini_restore('smtp_port');
  584. }
  585. else if ($this->sendmode == 'smtps')
  586. {
  587. if (! is_object($this->smtps))
  588. {
  589. $this->error="Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport."<br>Constructor of object CMailFile was not initialized without errors.";
  590. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  591. return false;
  592. }
  593. // Use SMTPS library
  594. // ------------------------------------------
  595. $this->smtps->setTransportType(0); // Only this method is coded in SMTPs library
  596. // Clean parameters
  597. if (empty($conf->global->$keyforsmtpserver)) $conf->global->$keyforsmtpserver=ini_get('SMTP');
  598. if (empty($conf->global->$keyforsmtpport)) $conf->global->$keyforsmtpport=ini_get('smtp_port');
  599. // If we use SSL/TLS
  600. $server=$conf->global->$keyforsmtpserver;
  601. $secure='';
  602. if (! empty($conf->global->$keyfortls) && function_exists('openssl_open')) $secure='ssl';
  603. if (! empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) $secure='tls';
  604. $server=($secure?$secure.'://':'').$server;
  605. $port=$conf->global->$keyforsmtpport;
  606. $this->smtps->setHost($server);
  607. $this->smtps->setPort($port); // 25, 465...;
  608. $loginid=''; $loginpass='';
  609. if (! empty($conf->global->$keyforsmtpid))
  610. {
  611. $loginid = $conf->global->$keyforsmtpid;
  612. $this->smtps->setID($loginid);
  613. }
  614. if (! empty($conf->global->$keyforsmtppw))
  615. {
  616. $loginpass = $conf->global->$keyforsmtppw;
  617. $this->smtps->setPW($loginpass);
  618. }
  619. $res=true;
  620. $from=$this->smtps->getFrom('org');
  621. if ($res && ! $from)
  622. {
  623. $this->error="Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport."<br>Sender address '$from' invalid";
  624. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  625. $res=false;
  626. }
  627. $dest=$this->smtps->getTo();
  628. if ($res && ! $dest)
  629. {
  630. $this->error="Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport."<br>Recipient address '$dest' invalid";
  631. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  632. $res=false;
  633. }
  634. if ($res)
  635. {
  636. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->smtps->setDebug(true);
  637. $result=$this->smtps->sendMsg();
  638. //print $result;
  639. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
  640. $result=$this->smtps->getErrors();
  641. if (empty($this->error) && empty($result))
  642. {
  643. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  644. $res=true;
  645. }
  646. else
  647. {
  648. if (empty($this->error)) $this->error=$result;
  649. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  650. $res=false;
  651. }
  652. }
  653. }
  654. else if ($this->sendmode == 'swiftmailer')
  655. {
  656. // Use Swift Mailer library
  657. // ------------------------------------------
  658. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
  659. // Clean parameters
  660. if (empty($conf->global->$keyforsmtpserver)) $conf->global->$keyforsmtpserver=ini_get('SMTP');
  661. if (empty($conf->global->$keyforsmtpport)) $conf->global->$keyforsmtpport=ini_get('smtp_port');
  662. // If we use SSL/TLS
  663. $server = $conf->global->$keyforsmtpserver;
  664. $secure = '';
  665. if (! empty($conf->global->$keyfortls) && function_exists('openssl_open')) $secure='ssl';
  666. if (! empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) $secure='tls';
  667. $this->transport = new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
  668. if (! empty($conf->global->$keyforsmtpid)) $this->transport->setUsername($conf->global->$keyforsmtpid);
  669. if (! empty($conf->global->$keyforsmtppw)) $this->transport->setPassword($conf->global->$keyforsmtppw);
  670. //$smtps->_msgReplyTo = 'reply@web.com';
  671. // Create the Mailer using your created Transport
  672. $this->mailer = new Swift_Mailer($this->transport);
  673. // DKIM SIGN
  674. if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
  675. $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
  676. $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
  677. $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
  678. $signer = new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
  679. $this->message->attachSigner($signer->ignoreHeader('Return-Path'));
  680. }
  681. if (! empty($conf->global->MAIN_MAIL_DEBUG)) {
  682. // To use the ArrayLogger
  683. $this->logger = new Swift_Plugins_Loggers_ArrayLogger();
  684. // Or to use the Echo Logger
  685. //$this->logger = new Swift_Plugins_Loggers_EchoLogger();
  686. $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->logger));
  687. }
  688. // send mail
  689. try {
  690. $result = $this->mailer->send($this->message);
  691. } catch (Exception $e) {
  692. $this->error = $e->getMessage();
  693. }
  694. if (! empty($conf->global->MAIN_MAIL_DEBUG)) $this->dump_mail();
  695. $res = true;
  696. if (! empty($this->error) || ! $result) {
  697. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  698. $res=false;
  699. }
  700. else
  701. {
  702. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  703. }
  704. }
  705. else
  706. {
  707. // Send mail method not correctly defined
  708. // --------------------------------------
  709. return 'Bad value for sendmode';
  710. }
  711. $parameters=array(); $action='';
  712. $reshook = $hookmanager->executeHooks('sendMailAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  713. if ($reshook < 0)
  714. {
  715. $this->error = "Error in hook maildao sendMailAfter " . $reshook;
  716. dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_ERR);
  717. return $reshook;
  718. }
  719. }
  720. else
  721. {
  722. $this->error='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
  723. dol_syslog("CMailFile::sendfile: ".$this->error, LOG_WARNING);
  724. }
  725. error_reporting($errorlevel); // Reactive niveau erreur origine
  726. return $res;
  727. }
  728. /**
  729. * Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word
  730. *
  731. * @param string $stringtoencode String to encode
  732. * @return string string encoded
  733. */
  734. static function encodetorfc2822($stringtoencode)
  735. {
  736. global $conf;
  737. return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
  738. }
  739. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  740. /**
  741. * Read a file on disk and return encoded content for emails (mode = 'mail')
  742. *
  743. * @param string $sourcefile Path to file to encode
  744. * @return int <0 if KO, encoded string if OK
  745. */
  746. function _encode_file($sourcefile)
  747. {
  748. // phpcs:enable
  749. $newsourcefile=dol_osencode($sourcefile);
  750. if (is_readable($newsourcefile))
  751. {
  752. $contents = file_get_contents($newsourcefile); // Need PHP 4.3
  753. $encoded = chunk_split(base64_encode($contents), 76, $this->eol); // 76 max is defined into http://tools.ietf.org/html/rfc2047
  754. return $encoded;
  755. }
  756. else
  757. {
  758. $this->error="Error: Can't read file '".$sourcefile."' into _encode_file";
  759. dol_syslog("CMailFile::encode_file: ".$this->error, LOG_ERR);
  760. return -1;
  761. }
  762. }
  763. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  764. /**
  765. * Write content of a SMTP request into a dump file (mode = all)
  766. * Used for debugging.
  767. * Note that to see full SMTP protocol, you can use tcpdump -w /tmp/smtp -s 2000 port 25"
  768. *
  769. * @return void
  770. */
  771. function dump_mail()
  772. {
  773. // phpcs:enable
  774. global $conf,$dolibarr_main_data_root;
  775. if (@is_writeable($dolibarr_main_data_root)) // Avoid fatal error on fopen with open_basedir
  776. {
  777. $outputfile=$dolibarr_main_data_root."/dolibarr_mail.log";
  778. $fp = fopen($outputfile,"w");
  779. if ($this->sendmode == 'mail')
  780. {
  781. fputs($fp, $this->headers);
  782. fputs($fp, $this->eol); // This eol is added by the mail function, so we add it in log
  783. fputs($fp, $this->message);
  784. }
  785. elseif ($this->sendmode == 'smtps')
  786. {
  787. fputs($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
  788. }
  789. elseif ($this->sendmode == 'swiftmailer')
  790. {
  791. fputs($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on
  792. }
  793. fclose($fp);
  794. if (! empty($conf->global->MAIN_UMASK))
  795. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  796. }
  797. }
  798. /**
  799. * Correct an uncomplete html string
  800. *
  801. * @param string $msg String
  802. * @return string Completed string
  803. */
  804. function checkIfHTML($msg)
  805. {
  806. if (!preg_match('/^[\s\t]*<html/i',$msg))
  807. {
  808. $out = "<html><head><title></title>";
  809. if (!empty($this->styleCSS)) $out.= $this->styleCSS;
  810. $out.= "</head><body";
  811. if (!empty($this->bodyCSS)) $out.= $this->bodyCSS;
  812. $out.= ">";
  813. $out.= $msg;
  814. $out.= "</body></html>";
  815. }
  816. else
  817. {
  818. $out = $msg;
  819. }
  820. return $out;
  821. }
  822. /**
  823. * Build a css style (mode = all) into this->styleCSS and this->bodyCSS
  824. *
  825. * @return string
  826. */
  827. function buildCSS()
  828. {
  829. if (! empty($this->css))
  830. {
  831. // Style CSS
  832. $this->styleCSS = '<style type="text/css">';
  833. $this->styleCSS.= 'body {';
  834. if ($this->css['bgcolor'])
  835. {
  836. $this->styleCSS.= ' background-color: '.$this->css['bgcolor'].';';
  837. $this->bodyCSS.= ' bgcolor="'.$this->css['bgcolor'].'"';
  838. }
  839. if ($this->css['bgimage'])
  840. {
  841. // TODO recuperer cid
  842. $this->styleCSS.= ' background-image: url("cid:'.$this->css['bgimage_cid'].'");';
  843. }
  844. $this->styleCSS.= '}';
  845. $this->styleCSS.= '</style>';
  846. }
  847. }
  848. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  849. /**
  850. * Create SMTP headers (mode = 'mail')
  851. *
  852. * @return string headers
  853. */
  854. function write_smtpheaders()
  855. {
  856. // phpcs:enable
  857. global $conf;
  858. $out = "";
  859. $host = dol_getprefix('email');
  860. // Sender
  861. //$out.= "Sender: ".getValidAddress($this->addr_from,2)).$this->eol2;
  862. $out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol2;
  863. if (! empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA))
  864. {
  865. $out.= "To: ".$this->getValidAddress($this->addr_to,0,1).$this->eol2;
  866. }
  867. // Return-Path is important because it is used by SPF. Some MTA does not read Return-Path from header but from command line. See option MAIN_MAIL_ALLOW_SENDMAIL_F for that.
  868. $out.= "Return-Path: ".$this->getValidAddress($this->addr_from,0,1).$this->eol2;
  869. if (isset($this->reply_to) && $this->reply_to) $out.= "Reply-To: ".$this->getValidAddress($this->reply_to,2).$this->eol2;
  870. if (isset($this->errors_to) && $this->errors_to) $out.= "Errors-To: ".$this->getValidAddress($this->errors_to,2).$this->eol2;
  871. // Receiver
  872. if (isset($this->addr_cc) && $this->addr_cc) $out.= "Cc: ".$this->getValidAddress($this->addr_cc,2).$this->eol2;
  873. if (isset($this->addr_bcc) && $this->addr_bcc) $out.= "Bcc: ".$this->getValidAddress($this->addr_bcc,2).$this->eol2; // TODO Question: bcc must not be into header, only into SMTP command "RCPT TO". Does php mail support this ?
  874. // Delivery receipt
  875. if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) $out.= "Disposition-Notification-To: ".$this->getValidAddress($this->addr_from,2).$this->eol2;
  876. //$out.= "X-Priority: 3".$this->eol2;
  877. $out.= 'Date: ' . date("r") . $this->eol2;
  878. $trackid = $this->trackid;
  879. if ($trackid)
  880. {
  881. // References is kept in response and Message-ID is returned into In-Reply-To:
  882. $out.= 'Message-ID: <' . time() . '.phpmail-dolibarr-'. $trackid . '@' . $host . ">" . $this->eol2; // Uppercase seems replaced by phpmail
  883. $out.= 'References: <' . time() . '.phpmail-dolibarr-'. $trackid . '@' . $host . ">" . $this->eol2;
  884. $out.= 'X-Dolibarr-TRACKID: ' . $trackid . '@' . $host. $this->eol2;
  885. }
  886. else
  887. {
  888. $out.= 'Message-ID: <' . time() . '.phpmail@' . $host . ">" . $this->eol2;
  889. }
  890. if (! empty($_SERVER['REMOTE_ADDR'])) $out.= "X-RemoteAddr: " . $_SERVER['REMOTE_ADDR']. $this->eol2;
  891. $out.= "X-Mailer: Dolibarr version " . DOL_VERSION ." (using php mail)".$this->eol2;
  892. $out.= "Mime-Version: 1.0".$this->eol2;
  893. //$out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol;
  894. $out.= "Content-Type: multipart/mixed;".$this->eol2." boundary=\"".$this->mixed_boundary."\"".$this->eol2;
  895. $out.= "Content-Transfer-Encoding: 8bit".$this->eol2; // TODO Seems to be ignored. Header is 7bit once received.
  896. dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out);
  897. return $out;
  898. }
  899. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  900. /**
  901. * Create header MIME (mode = 'mail')
  902. *
  903. * @param array $filename_list Array of filenames
  904. * @param array $mimefilename_list Array of mime types
  905. * @return string mime headers
  906. */
  907. function write_mimeheaders($filename_list, $mimefilename_list)
  908. {
  909. // phpcs:enable
  910. $mimedone=0;
  911. $out = "";
  912. if (is_array($filename_list))
  913. {
  914. $filename_list_size=count($filename_list);
  915. for($i=0;$i < $filename_list_size;$i++)
  916. {
  917. if ($filename_list[$i])
  918. {
  919. if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
  920. $out.= "X-attachments: $filename_list[$i]".$this->eol2;
  921. }
  922. }
  923. }
  924. dol_syslog("CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
  925. return $out;
  926. }
  927. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  928. /**
  929. * Return email content (mode = 'mail')
  930. *
  931. * @param string $msgtext Message string
  932. * @return string String content
  933. */
  934. function write_body($msgtext)
  935. {
  936. // phpcs:enable
  937. global $conf;
  938. $out='';
  939. $out.= "--" . $this->mixed_boundary . $this->eol;
  940. if ($this->atleastoneimage)
  941. {
  942. $out.= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
  943. $out.= $this->eol;
  944. $out.= "--" . $this->alternative_boundary . $this->eol;
  945. }
  946. // Make RFC821 Compliant, replace bare linefeeds
  947. $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $msgtext); // PCRE modifier /s means new lines are common chars
  948. if (! empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA))
  949. {
  950. $strContent = preg_replace("/\r\n/si", "\n", $strContent); // PCRE modifier /s means new lines are common chars
  951. }
  952. $strContentAltText = '';
  953. if ($this->msgishtml)
  954. {
  955. // Similar code to forge a text from html is also in CMailFile.class.php
  956. $strContentAltText = preg_replace("/<br\s*[^>]*>/"," ", $strContent);
  957. $strContentAltText = html_entity_decode(strip_tags($strContentAltText));
  958. $strContentAltText = rtrim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)?"\r\n":"\n"));
  959. // Check if html header already in message, if not complete the message
  960. $strContent = $this->checkIfHTML($strContent);
  961. }
  962. // Make RFC2045 Compliant, split lines
  963. //$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems ko if not used on a base64 content
  964. // TODO Encode main content into base64 and use the chunk_split, or quoted-printable
  965. $strContent = rtrim(wordwrap($strContent, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)?"\r\n":"\n")); // TODO Using this method creates unexpected line break on text/plain content.
  966. if ($this->msgishtml)
  967. {
  968. if ($this->atleastoneimage)
  969. {
  970. $out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  971. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
  972. $out.= $this->eol.($strContentAltText?$strContentAltText:strip_tags($strContent)).$this->eol; // Add plain text message
  973. $out.= "--" . $this->alternative_boundary . $this->eol;
  974. $out.= "Content-Type: multipart/related;".$this->eol." boundary=\"".$this->related_boundary."\"".$this->eol;
  975. $out.= $this->eol;
  976. $out.= "--" . $this->related_boundary . $this->eol;
  977. }
  978. if (! $this->atleastoneimage && $strContentAltText && ! empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) // Add plain text message part before html part
  979. {
  980. $out.= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
  981. $out.= $this->eol;
  982. $out.= "--" . $this->alternative_boundary . $this->eol;
  983. $out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  984. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
  985. $out.= $this->eol.$strContentAltText.$this->eol;
  986. $out.= "--" . $this->alternative_boundary . $this->eol;
  987. }
  988. $out.= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
  989. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol; // TODO Use base64
  990. $out.= $this->eol.$strContent.$this->eol;
  991. if (! $this->atleastoneimage && $strContentAltText && ! empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) // Add plain text message part after html part
  992. {
  993. $out.= "--" . $this->alternative_boundary . "--". $this->eol;
  994. }
  995. }
  996. else
  997. {
  998. $out.= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  999. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
  1000. $out.= $this->eol.$strContent.$this->eol;
  1001. }
  1002. $out.= $this->eol;
  1003. // Encode images
  1004. if ($this->atleastoneimage)
  1005. {
  1006. $out .= $this->write_images($this->images_encoded);
  1007. // always end related and end alternative after inline images
  1008. $out .= "--" . $this->related_boundary . "--" . $this->eol;
  1009. $out .= $this->eol . "--" . $this->alternative_boundary . "--" . $this->eol;
  1010. $out .= $this->eol;
  1011. }
  1012. return $out;
  1013. }
  1014. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  1015. /**
  1016. * Attach file to email (mode = 'mail')
  1017. *
  1018. * @param array $filename_list Tableau
  1019. * @param array $mimetype_list Tableau
  1020. * @param array $mimefilename_list Tableau
  1021. * @return string Chaine fichiers encodes
  1022. */
  1023. function write_files($filename_list,$mimetype_list,$mimefilename_list)
  1024. {
  1025. // phpcs:enable
  1026. $out = '';
  1027. $filename_list_size=count($filename_list);
  1028. for($i=0;$i < $filename_list_size;$i++)
  1029. {
  1030. if ($filename_list[$i])
  1031. {
  1032. dol_syslog("CMailFile::write_files: i=$i");
  1033. $encoded = $this->_encode_file($filename_list[$i]);
  1034. if ($encoded >= 0)
  1035. {
  1036. if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
  1037. if (! $mimetype_list[$i]) {
  1038. $mimetype_list[$i] = "application/octet-stream";
  1039. }
  1040. $out.= "--" . $this->mixed_boundary . $this->eol;
  1041. $out.= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
  1042. $out.= "Content-Type: " . $mimetype_list[$i] . "; name=\"".$filename_list[$i]."\"".$this->eol;
  1043. $out.= "Content-Transfer-Encoding: base64".$this->eol;
  1044. $out.= "Content-Description: ".$filename_list[$i].$this->eol;
  1045. $out.= $this->eol;
  1046. $out.= $encoded;
  1047. $out.= $this->eol;
  1048. //$out.= $this->eol;
  1049. }
  1050. else
  1051. {
  1052. return $encoded;
  1053. }
  1054. }
  1055. }
  1056. return $out;
  1057. }
  1058. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  1059. /**
  1060. * Attach an image to email (mode = 'mail')
  1061. *
  1062. * @param array $images_list Array of array image
  1063. * @return string Chaine images encodees
  1064. */
  1065. function write_images($images_list)
  1066. {
  1067. // phpcs:enable
  1068. $out = '';
  1069. if (is_array($images_list))
  1070. {
  1071. foreach ($images_list as $img)
  1072. {
  1073. dol_syslog("CMailFile::write_images: ".$img["name"]);
  1074. $out.= "--" . $this->related_boundary . $this->eol; // always related for an inline image
  1075. $out.= "Content-Type: " . $img["content_type"] . "; name=\"".$img["name"]."\"".$this->eol;
  1076. $out.= "Content-Transfer-Encoding: base64".$this->eol;
  1077. $out.= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
  1078. $out.= "Content-ID: <".$img["cid"].">".$this->eol;
  1079. $out.= $this->eol;
  1080. $out.= $img["image_encoded"];
  1081. $out.= $this->eol;
  1082. }
  1083. }
  1084. return $out;
  1085. }
  1086. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  1087. /**
  1088. * Try to create a socket connection
  1089. *
  1090. * @param string $host Add ssl:// for SSL/TLS.
  1091. * @param int $port Example: 25, 465
  1092. * @return int Socket id if ok, 0 if KO
  1093. */
  1094. function check_server_port($host,$port)
  1095. {
  1096. // phpcs:enable
  1097. global $conf;
  1098. $_retVal=0;
  1099. $timeout=5; // Timeout in seconds
  1100. if (function_exists('fsockopen'))
  1101. {
  1102. $keyforsmtpserver='MAIN_MAIL_SMTP_SERVER';
  1103. $keyforsmtpport ='MAIN_MAIL_SMTP_PORT';
  1104. $keyforsmtpid ='MAIN_MAIL_SMTPS_ID';
  1105. $keyforsmtppw ='MAIN_MAIL_SMTPS_PW';
  1106. $keyfortls ='MAIN_MAIL_EMAIL_TLS';
  1107. $keyforstarttls ='MAIN_MAIL_EMAIL_STARTTLS';
  1108. if ($this->sendcontext == 'emailing' && !empty($conf->global->MAIN_MAIL_SENDMODE_EMAILING) && $conf->global->MAIN_MAIL_SENDMODE_EMAILING != 'default')
  1109. {
  1110. $keyforsmtpserver='MAIN_MAIL_SMTP_SERVER_EMAILING';
  1111. $keyforsmtpport ='MAIN_MAIL_SMTP_PORT_EMAILING';
  1112. $keyforsmtpid ='MAIN_MAIL_SMTPS_ID_EMAILING';
  1113. $keyforsmtppw ='MAIN_MAIL_SMTPS_PW_EMAILING';
  1114. $keyfortls ='MAIN_MAIL_EMAIL_TLS_EMAILING';
  1115. $keyforstarttls ='MAIN_MAIL_EMAIL_STARTTLS_EMAILING';
  1116. }
  1117. // If we use SSL/TLS
  1118. if (! empty($conf->global->$keyfortls) && function_exists('openssl_open')) $host='ssl://'.$host;
  1119. // tls smtp start with no encryption
  1120. //if (! empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS) && function_exists('openssl_open')) $host='tls://'.$host;
  1121. dol_syslog("Try socket connection to host=".$host." port=".$port);
  1122. //See if we can connect to the SMTP server
  1123. if ($socket = @fsockopen(
  1124. $host, // Host to test, IP or domain. Add ssl:// for SSL/TLS.
  1125. $port, // which Port number to use
  1126. $errno, // actual system level error
  1127. $errstr, // and any text that goes with the error
  1128. $timeout
  1129. )) // timeout for reading/writing data over the socket
  1130. {
  1131. // Windows still does not have support for this timeout function
  1132. if (function_exists('stream_set_timeout')) stream_set_timeout($socket, $timeout, 0);
  1133. dol_syslog("Now we wait for answer 220");
  1134. // Check response from Server
  1135. if ( $_retVal = $this->server_parse($socket, "220") ) $_retVal = $socket;
  1136. }
  1137. else
  1138. {
  1139. $this->error = utf8_check('Error '.$errno.' - '.$errstr)?'Error '.$errno.' - '.$errstr:utf8_encode('Error '.$errno.' - '.$errstr);
  1140. }
  1141. }
  1142. return $_retVal;
  1143. }
  1144. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  1145. /**
  1146. * This function has been modified as provided by SirSir to allow multiline responses when
  1147. * using SMTP Extensions.
  1148. *
  1149. * @param Socket $socket Socket
  1150. * @param string $response Response string
  1151. * @return boolean true if success
  1152. */
  1153. function server_parse($socket, $response)
  1154. {
  1155. // phpcs:enable
  1156. $_retVal = true; // Indicates if Object was created or not
  1157. $server_response = '';
  1158. while (substr($server_response,3,1) != ' ')
  1159. {
  1160. if (! ($server_response = fgets($socket, 256)) )
  1161. {
  1162. $this->error="Couldn't get mail server response codes";
  1163. return false;
  1164. }
  1165. }
  1166. if( !( substr($server_response, 0, 3) == $response ) )
  1167. {
  1168. $this->error="Ran into problems sending Mail.\r\nResponse: $server_response";
  1169. $_retVal = false;
  1170. }
  1171. return $_retVal;
  1172. }
  1173. /**
  1174. * Seearch images into html message and init array this->images_encoded if found
  1175. *
  1176. * @param string $images_dir Location of physical images files
  1177. * @return int >0 if OK, <0 if KO
  1178. */
  1179. function findHtmlImages($images_dir)
  1180. {
  1181. // Build the list of image extensions
  1182. $extensions = array_keys($this->image_types);
  1183. $matches = array();
  1184. preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
  1185. if (! empty($matches))
  1186. {
  1187. $i=0;
  1188. foreach ($matches[1] as $full)
  1189. {
  1190. if (preg_match('/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i',$full,$regs)) // If xxx is 'file=aaa'
  1191. {
  1192. $img = $regs[1];
  1193. if (file_exists($images_dir.'/'.$img))
  1194. {
  1195. // Image path in src
  1196. $src = preg_quote($full,'/');
  1197. // Image full path
  1198. $this->html_images[$i]["fullpath"] = $images_dir.'/'.$img;
  1199. // Image name
  1200. $this->html_images[$i]["name"] = $img;
  1201. // Content type
  1202. if (preg_match('/^.+\.(\w{3,4})$/', $img, $reg))
  1203. {
  1204. $ext=strtolower($reg[1]);
  1205. $this->html_images[$i]["content_type"] = $this->image_types[$ext];
  1206. }
  1207. // cid
  1208. $this->html_images[$i]["cid"] = dol_hash(uniqid(time()), 3); // Force md5 hash (does not contains special chars)
  1209. $this->html = preg_replace("/src=\"$src\"|src='$src'/i", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
  1210. }
  1211. $i++;
  1212. }
  1213. }
  1214. if (!empty($this->html_images))
  1215. {
  1216. $inline = array();
  1217. $i=0;
  1218. foreach ($this->html_images as $img)
  1219. {
  1220. $fullpath = $images_dir.'/'.$img["name"];
  1221. // If duplicate images are embedded, they may show up as attachments, so remove them.
  1222. if (!in_array($fullpath,$inline))
  1223. {
  1224. // Read image file
  1225. if ($image = file_get_contents($fullpath))
  1226. {
  1227. // On garde que le nom de l'image
  1228. preg_match('/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i',$img["name"],$regs);
  1229. $imgName = $regs[1];
  1230. $this->images_encoded[$i]['name'] = $imgName;
  1231. $this->images_encoded[$i]['fullpath'] = $fullpath;
  1232. $this->images_encoded[$i]['content_type'] = $img["content_type"];
  1233. $this->images_encoded[$i]['cid'] = $img["cid"];
  1234. // Encodage de l'image
  1235. $this->images_encoded[$i]["image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
  1236. $inline[] = $fullpath;
  1237. }
  1238. }
  1239. $i++;
  1240. }
  1241. }
  1242. else
  1243. {
  1244. return -1;
  1245. }
  1246. return 1;
  1247. }
  1248. else
  1249. {
  1250. return 0;
  1251. }
  1252. }
  1253. /**
  1254. * Return a formatted address string for SMTP protocol
  1255. *
  1256. * @param string $address Example: 'John Doe <john@doe.com>, Alan Smith <alan@smith.com>' or 'john@doe.com, alan@smith.com'
  1257. * @param int $format 0=auto, 1=emails with <>, 2=emails without <>, 3=auto + label between "
  1258. * @param int $encode 0=No encode name, 1=Encode name to RFC2822
  1259. * @param int $maxnumberofemail 0=No limit. Otherwise, maximum number of emails returned ($address may contains several email separated with ','). Add '...' if there is more.
  1260. * @return string If format 0: '<john@doe.com>' or 'John Doe <john@doe.com>' or '=?UTF-8?B?Sm9obiBEb2U=?= <john@doe.com>'
  1261. * If format 1: '<john@doe.com>'
  1262. * If format 2: 'john@doe.com'
  1263. * If format 3: '<john@doe.com>' or '"John Doe" <john@doe.com>' or '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>'
  1264. * If format 4: 'John Doe' or 'john@doe.com' if no label exists
  1265. */
  1266. static function getValidAddress($address,$format,$encode=0,$maxnumberofemail=0)
  1267. {
  1268. global $conf;
  1269. $ret='';
  1270. $arrayaddress=explode(',',$address);
  1271. // Boucle sur chaque composant de l'adresse
  1272. $i=0;
  1273. foreach($arrayaddress as $val)
  1274. {
  1275. if (preg_match('/^(.*)<(.*)>$/i',trim($val),$regs))
  1276. {
  1277. $name = trim($regs[1]);
  1278. $email = trim($regs[2]);
  1279. }
  1280. else
  1281. {
  1282. $name = '';
  1283. $email = trim($val);
  1284. }
  1285. if ($email)
  1286. {
  1287. $i++;
  1288. $newemail='';
  1289. if ($format == 4)
  1290. {
  1291. $newemail = $name?$name:$email;
  1292. }
  1293. if ($format == 2)
  1294. {
  1295. $newemail=$email;
  1296. }
  1297. if ($format == 1 || $format == 3)
  1298. {
  1299. $newemail='<'.$email.'>';
  1300. }
  1301. if ($format == 0 || $format == 3)
  1302. {
  1303. if (! empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) $newemail='<'.$email.'>';
  1304. elseif (! $name) $newemail='<'.$email.'>';
  1305. else $newemail=($format==3?'"':'').($encode?self::encodetorfc2822($name):$name).($format==3?'"':'').' <'.$email.'>';
  1306. }
  1307. $ret=($ret ? $ret.',' : '').$newemail;
  1308. // Stop if we have too much records
  1309. if ($maxnumberofemail && $i >= $maxnumberofemail)
  1310. {
  1311. if (count($arrayaddress) > $maxnumberofemail) $ret.='...';
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. return $ret;
  1317. }
  1318. /**
  1319. * Return a formatted array of address string for SMTP protocol
  1320. *
  1321. * @param string $address Example: 'John Doe <john@doe.com>, Alan Smith <alan@smith.com>' or 'john@doe.com, alan@smith.com'
  1322. * @return array array of email => name
  1323. */
  1324. function getArrayAddress($address)
  1325. {
  1326. global $conf;
  1327. $ret=array();
  1328. $arrayaddress=explode(',',$address);
  1329. // Boucle sur chaque composant de l'adresse
  1330. foreach($arrayaddress as $val)
  1331. {
  1332. if (preg_match('/^(.*)<(.*)>$/i',trim($val),$regs))
  1333. {
  1334. $name = trim($regs[1]);
  1335. $email = trim($regs[2]);
  1336. }
  1337. else
  1338. {
  1339. $name = null;
  1340. $email = trim($val);
  1341. }
  1342. $ret[$email]=empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)?$name:null;
  1343. }
  1344. return $ret;
  1345. }
  1346. }