CMailFile.class.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. * Copyright (C) 2019-2022 Frédéric France <frederic.france@netlogic.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. * or see https://www.gnu.org/
  24. *
  25. * Lots of code inspired from Dan Potter's CMailFile class
  26. */
  27. /**
  28. * \file htdocs/core/class/CMailFile.class.php
  29. * \brief File of class to send emails (with attachments or not)
  30. */
  31. use OAuth\Common\Storage\DoliStorage;
  32. use OAuth\Common\Consumer\Credentials;
  33. /**
  34. * Class to send emails (with attachments or not)
  35. * Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto);
  36. * $mailfile->sendfile();
  37. */
  38. class CMailFile
  39. {
  40. public $sendcontext;
  41. public $sendmode;
  42. public $sendsetup;
  43. /**
  44. * @var string Subject of email
  45. */
  46. public $subject;
  47. public $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).
  48. // Sender: Who send the email ("Sender" has sent emails on behalf of "From").
  49. // 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.
  50. // Return-Path: Email where to send bounds.
  51. public $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)
  52. public $errors_to; // Errors-To: Email where to send errors.
  53. public $addr_to;
  54. public $addr_cc;
  55. public $addr_bcc;
  56. public $trackid;
  57. public $mixed_boundary;
  58. public $related_boundary;
  59. public $alternative_boundary;
  60. public $deliveryreceipt;
  61. public $atleastonefile;
  62. public $eol;
  63. public $eol2;
  64. /**
  65. * @var string Error code (or message)
  66. */
  67. public $error = '';
  68. /**
  69. * @var string[] Array of Error code (or message)
  70. */
  71. public $errors = array();
  72. public $smtps; // Contains SMTPs object (if this method is used)
  73. public $phpmailer; // Contains PHPMailer object (if this method is used)
  74. /**
  75. * @var Swift_SmtpTransport
  76. */
  77. public $transport;
  78. /**
  79. * @var Swift_Mailer
  80. */
  81. public $mailer;
  82. /**
  83. * @var Swift_Plugins_Loggers_ArrayLogger
  84. */
  85. public $logger;
  86. /**
  87. * @var string CSS
  88. */
  89. public $css;
  90. //! Defined css style for body background
  91. public $styleCSS;
  92. //! Defined background directly in body tag
  93. public $bodyCSS;
  94. public $msgid;
  95. public $headers;
  96. public $message;
  97. /**
  98. * @var array fullfilenames list (full path of filename on file system)
  99. */
  100. public $filename_list = array();
  101. /**
  102. * @var array mimetypes of files list (List of MIME type of attached files)
  103. */
  104. public $mimetype_list = array();
  105. /**
  106. * @var array filenames list (List of attached file name in message)
  107. */
  108. public $mimefilename_list = array();
  109. /**
  110. * @var array filenames cid
  111. */
  112. public $cid_list = array();
  113. // Image
  114. public $html;
  115. public $image_boundary;
  116. public $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).
  117. public $html_images = array();
  118. public $images_encoded = array();
  119. public $image_types = array(
  120. 'gif' => 'image/gif',
  121. 'jpg' => 'image/jpeg',
  122. 'jpeg' => 'image/jpeg',
  123. 'jpe' => 'image/jpeg',
  124. 'bmp' => 'image/bmp',
  125. 'png' => 'image/png',
  126. 'tif' => 'image/tiff',
  127. 'tiff' => 'image/tiff',
  128. );
  129. /**
  130. * CMailFile
  131. *
  132. * @param string $subject Topic/Subject of mail
  133. * @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.
  134. * @param string $from Sender email (RFC 2822: "Name firstname <email>[, ...]" or "email[, ...]" or "<email>[, ...]")
  135. * @param string $msg Message
  136. * @param array $filename_list List of files to attach (full path of filename on file system)
  137. * @param array $mimetype_list List of MIME type of attached files
  138. * @param array $mimefilename_list List of attached file name in message
  139. * @param string $addr_cc Email cc (Example: 'abc@def.com, ghk@lmn.com')
  140. * @param string $addr_bcc Email bcc (Note: This is autocompleted with MAIN_MAIL_AUTOCOPY_TO if defined)
  141. * @param int $deliveryreceipt Ask a delivery receipt
  142. * @param int $msgishtml 1=String IS already html, 0=String IS NOT html, -1=Unknown make autodetection (with fast mode, not reliable)
  143. * @param string $errors_to Email for errors-to
  144. * @param string $css Css option
  145. * @param string $trackid Tracking string (contains type and id of related element)
  146. * @param string $moreinheader More in header. $moreinheader must contains the "\r\n" (TODO not supported for other MAIL_SEND_MODE different than 'mail' and 'smtps' for the moment)
  147. * @param string $sendcontext 'standard', 'emailing', ... (used to define which sending mode and parameters to use)
  148. * @param string $replyto Reply-to email (will be set to same value than From by default if not provided)
  149. * @param string $upload_dir_tmp Temporary directory (used to convert images embedded as img src=data:image)
  150. */
  151. public 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 = '', $upload_dir_tmp = '')
  152. {
  153. global $conf, $dolibarr_main_data_root, $user;
  154. // Clean values of $mimefilename_list
  155. if (is_array($mimefilename_list)) {
  156. foreach ($mimefilename_list as $key => $val) {
  157. $mimefilename_list[$key] = dol_string_unaccent($mimefilename_list[$key]);
  158. }
  159. }
  160. $cid_list = array();
  161. $this->sendcontext = $sendcontext;
  162. // Define this->sendmode ('mail', 'smtps', 'siwftmailer', ...) according to $sendcontext ('standard', 'emailing', 'ticket')
  163. $this->sendmode = '';
  164. if (!empty($this->sendcontext)) {
  165. $smtpContextKey = strtoupper($this->sendcontext);
  166. $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
  167. if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
  168. $this->sendmode = $smtpContextSendMode;
  169. }
  170. }
  171. if (empty($this->sendmode)) {
  172. $this->sendmode = (!empty($conf->global->MAIN_MAIL_SENDMODE) ? $conf->global->MAIN_MAIL_SENDMODE : 'mail');
  173. }
  174. // We define end of line (RFC 821).
  175. $this->eol = "\r\n";
  176. // We define end of line for header fields (RFC 822bis section 2.3 says header must contains \r\n).
  177. $this->eol2 = "\r\n";
  178. if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
  179. $this->eol = "\n";
  180. $this->eol2 = "\n";
  181. $moreinheader = str_replace("\r\n", "\n", $moreinheader);
  182. }
  183. // On defini mixed_boundary
  184. $this->mixed_boundary = "multipart_x.".time().".x_boundary";
  185. // On defini related_boundary
  186. $this->related_boundary = 'mul_'.dol_hash(uniqid("dolibarr2"), 3); // Force md5 hash (does not contains special chars)
  187. // On defini alternative_boundary
  188. $this->alternative_boundary = 'mul_'.dol_hash(uniqid("dolibarr3"), 3); // Force md5 hash (does not contains special chars)
  189. 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, replyto=$replyto trackid=$trackid sendcontext=$sendcontext upload_dir_tmp=$upload_dir_tmp", LOG_DEBUG);
  190. dol_syslog("CMailFile::CMailfile: subject=".$subject.", deliveryreceipt=".$deliveryreceipt.", msgishtml=".$msgishtml, LOG_DEBUG);
  191. if (empty($subject)) {
  192. dol_syslog("CMailFile::CMailfile: Try to send an email with empty subject");
  193. $this->error = 'ErrorSubjectIsRequired';
  194. return;
  195. }
  196. if (empty($msg)) {
  197. dol_syslog("CMailFile::CMailfile: Try to send an email with empty body");
  198. $msg = '.'; // Avoid empty message (with empty message content, you will see a multipart structure)
  199. }
  200. // Detect if message is HTML (use fast method)
  201. if ($msgishtml == -1) {
  202. $this->msgishtml = 0;
  203. if (dol_textishtml($msg)) {
  204. $this->msgishtml = 1;
  205. }
  206. } else {
  207. $this->msgishtml = $msgishtml;
  208. }
  209. global $dolibarr_main_url_root;
  210. // Define $urlwithroot
  211. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  212. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  213. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  214. // Replace relative /viewimage to absolute path
  215. $msg = preg_replace('/src="'.preg_quote(DOL_URL_ROOT, '/').'\/viewimage\.php/ims', 'src="'.$urlwithroot.'/viewimage.php', $msg, -1);
  216. if (!empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) {
  217. $this->msgishtml = 1; // To force to send everything with content type html.
  218. }
  219. // Detect images
  220. if ($this->msgishtml) {
  221. $this->html = $msg;
  222. $findimg = 0;
  223. if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) {
  224. // Search into the body for <img tags of links in medias files to replace them with an embedded file
  225. // Note because media links are public, this should be useless, except avoid blocking images with email browser.
  226. // This convert an embedd file with src="/viewimage.php?modulepart... into a cid link
  227. $findimg = $this->findHtmlImages($dolibarr_main_data_root.'/medias');
  228. }
  229. if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_DATA)) {
  230. // Search into the body for <img src="data:image/ext;base64,..." to replace them with an embedded file
  231. // This convert an embedded file with src="data:image... into a cid link + attached file
  232. $findimg = $this->findHtmlImagesIsSrcData($upload_dir_tmp);
  233. }
  234. // Set atleastoneimage if there is at least one embedded file (into ->html_images)
  235. if ($findimg > 0) {
  236. foreach ($this->html_images as $i => $val) {
  237. if ($this->html_images[$i]) {
  238. $this->atleastoneimage = 1;
  239. if ($this->html_images[$i]['type'] == 'cidfromdata') {
  240. $posindice = count($filename_list);
  241. $filename_list[$posindice] = $this->html_images[$i]['fullpath'];
  242. $mimetype_list[$posindice] = $this->html_images[$i]['content_type'];
  243. $mimefilename_list[$posindice] = $this->html_images[$i]['name'];
  244. $cid_list[$posindice] = $this->html_images[$i]['cid'];
  245. }
  246. dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG);
  247. }
  248. }
  249. }
  250. }
  251. // Set atleastoneimage if there is at least one file (into $filename_list array)
  252. if (is_array($filename_list)) {
  253. foreach ($filename_list as $i => $val) {
  254. if ($filename_list[$i]) {
  255. $this->atleastonefile = 1;
  256. dol_syslog("CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].", mimetype_list[$i]=".$mimetype_list[$i]." mimefilename_list[$i]=".$mimefilename_list[$i]." cid_list[$i]=".$cid_list[$i], LOG_DEBUG);
  257. }
  258. }
  259. }
  260. // Add auto copy to if not already in $to (Note: Adding bcc for specific modules are also done from pages)
  261. // For example MAIN_MAIL_AUTOCOPY_TO can be 'email@example.com, __USER_EMAIL__, ...'
  262. if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
  263. $listofemailstoadd = explode(',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
  264. foreach ($listofemailstoadd as $key => $val) {
  265. $emailtoadd = $listofemailstoadd[$key];
  266. if (trim($emailtoadd) == '__USER_EMAIL__') {
  267. if (!empty($user) && !empty($user->email)) {
  268. $emailtoadd = $user->email;
  269. } else {
  270. $emailtoadd = '';
  271. }
  272. }
  273. if ($emailtoadd && preg_match('/'.preg_quote($emailtoadd, '/').'/i', $to)) {
  274. $emailtoadd = ''; // Email already in the "To"
  275. }
  276. if ($emailtoadd) {
  277. $listofemailstoadd[$key] = $emailtoadd;
  278. } else {
  279. unset($listofemailstoadd[$key]);
  280. }
  281. }
  282. if (!empty($listofemailstoadd)) {
  283. $addr_bcc .= ($addr_bcc ? ', ' : '').join(', ', $listofemailstoadd);
  284. }
  285. }
  286. $this->subject = $subject;
  287. $this->addr_to = dol_sanitizeEmail($to);
  288. $this->addr_from = dol_sanitizeEmail($from);
  289. $this->msg = $msg;
  290. $this->addr_cc = dol_sanitizeEmail($addr_cc);
  291. $this->addr_bcc = dol_sanitizeEmail($addr_bcc);
  292. $this->deliveryreceipt = $deliveryreceipt;
  293. if (empty($replyto)) {
  294. $replyto = dol_sanitizeEmail($from);
  295. }
  296. $this->reply_to = dol_sanitizeEmail($replyto);
  297. $this->errors_to = dol_sanitizeEmail($errors_to);
  298. $this->trackid = $trackid;
  299. // Set arrays with attached files info
  300. $this->filename_list = $filename_list;
  301. $this->mimetype_list = $mimetype_list;
  302. $this->mimefilename_list = $mimefilename_list;
  303. $this->cid_list = $cid_list;
  304. if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO)) {
  305. $this->addr_to = dol_sanitizeEmail($conf->global->MAIN_MAIL_FORCE_SENDTO);
  306. $this->addr_cc = '';
  307. $this->addr_bcc = '';
  308. }
  309. $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
  310. if (!empty($this->sendcontext)) {
  311. $smtpContextKey = strtoupper($this->sendcontext);
  312. $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
  313. if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
  314. $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
  315. }
  316. }
  317. // We set all data according to choosed sending method.
  318. // We also set a value for ->msgid
  319. if ($this->sendmode == 'mail') {
  320. // Use mail php function (default PHP method)
  321. // ------------------------------------------
  322. $smtp_headers = "";
  323. $mime_headers = "";
  324. $text_body = "";
  325. $files_encoded = "";
  326. // Define smtp_headers (this also set ->msgid)
  327. $smtp_headers = $this->write_smtpheaders();
  328. if (!empty($moreinheader)) {
  329. $smtp_headers .= $moreinheader; // $moreinheader contains the \r\n
  330. }
  331. // Define mime_headers
  332. $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
  333. if (!empty($this->html)) {
  334. if (!empty($css)) {
  335. $this->css = $css;
  336. $this->buildCSS(); // Build a css style (mode = all) into this->styleCSS and this->bodyCSS
  337. }
  338. $msg = $this->html;
  339. }
  340. // Define body in text_body
  341. $text_body = $this->write_body($msg);
  342. // Add attachments to text_encoded
  343. if (!empty($this->atleastonefile)) {
  344. $files_encoded = $this->write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
  345. }
  346. // We now define $this->headers and $this->message
  347. $this->headers = $smtp_headers.$mime_headers;
  348. // On nettoie le header pour qu'il ne se termine pas par un retour chariot.
  349. // This avoid also empty lines at end that can be interpreted as mail injection by email servers.
  350. $this->headers = preg_replace("/([\r\n]+)$/i", "", $this->headers);
  351. //$this->message = $this->eol.'This is a message with multiple parts in MIME format.'.$this->eol;
  352. $this->message = 'This is a message with multiple parts in MIME format.'.$this->eol;
  353. $this->message .= $text_body.$files_encoded;
  354. $this->message .= "--".$this->mixed_boundary."--".$this->eol;
  355. } elseif ($this->sendmode == 'smtps') {
  356. // Use SMTPS library
  357. // ------------------------------------------
  358. require_once DOL_DOCUMENT_ROOT.'/core/class/smtps.class.php';
  359. $smtps = new SMTPs();
  360. $smtps->setCharSet($conf->file->character_set_client);
  361. // Encode subject if required.
  362. $subjecttouse = $this->subject;
  363. if (!ascii_check($subjecttouse)) {
  364. $subjecttouse = $this->encodetorfc2822($subjecttouse);
  365. }
  366. $smtps->setSubject($subjecttouse);
  367. $smtps->setTO($this->getValidAddress($this->addr_to, 0, 1));
  368. $smtps->setFrom($this->getValidAddress($this->addr_from, 0, 1));
  369. $smtps->setTrackId($this->trackid);
  370. $smtps->setReplyTo($this->getValidAddress($this->reply_to, 0, 1));
  371. if (!empty($moreinheader)) {
  372. $smtps->setMoreInHeader($moreinheader);
  373. }
  374. if (!empty($this->html)) {
  375. if (!empty($css)) {
  376. $this->css = $css;
  377. $this->buildCSS();
  378. }
  379. $msg = $this->html;
  380. $msg = $this->checkIfHTML($msg);
  381. }
  382. // Replace . alone on a new line with .. to avoid to have SMTP interpret this as end of message
  383. $msg = preg_replace('/(\r|\n)\.(\r|\n)/ims', '\1..\2', $msg);
  384. if ($this->msgishtml) {
  385. $smtps->setBodyContent($msg, 'html');
  386. } else {
  387. $smtps->setBodyContent($msg, 'plain');
  388. }
  389. if ($this->atleastoneimage) {
  390. foreach ($this->images_encoded as $img) {
  391. $smtps->setImageInline($img['image_encoded'], $img['name'], $img['content_type'], $img['cid']);
  392. }
  393. }
  394. if (!empty($this->atleastonefile)) {
  395. foreach ($filename_list as $i => $val) {
  396. $content = file_get_contents($filename_list[$i]);
  397. $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], $cid_list[$i]);
  398. }
  399. }
  400. $smtps->setCC($this->addr_cc);
  401. $smtps->setBCC($this->addr_bcc);
  402. $smtps->setErrorsTo($this->errors_to);
  403. $smtps->setDeliveryReceipt($this->deliveryreceipt);
  404. if (!empty($conf->global->$keyforsslseflsigned)) {
  405. $smtps->setOptions(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)));
  406. }
  407. $host = dol_getprefix('email');
  408. $this->msgid = time().'.SMTPs-dolibarr-'.$this->trackid.'@'.$host;
  409. $this->smtps = $smtps;
  410. } elseif ($this->sendmode == 'swiftmailer') {
  411. // Use Swift Mailer library
  412. $host = dol_getprefix('email');
  413. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
  414. // egulias autoloader lib
  415. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/autoload.php';
  416. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
  417. // Create the message
  418. //$this->message = Swift_Message::newInstance();
  419. $this->message = new Swift_Message();
  420. //$this->message = new Swift_SignedMessage();
  421. // Adding a trackid header to a message
  422. $headers = $this->message->getHeaders();
  423. $headers->addTextHeader('X-Dolibarr-TRACKID', $this->trackid.'@'.$host);
  424. $this->msgid = time().'.swiftmailer-dolibarr-'.$this->trackid.'@'.$host;
  425. $headerID = $this->msgid;
  426. $msgid = $headers->get('Message-ID');
  427. $msgid->setId($headerID);
  428. $headers->addIdHeader('References', $headerID);
  429. // TODO if (!empty($moreinheader)) ...
  430. // Give the message a subject
  431. try {
  432. $result = $this->message->setSubject($this->subject);
  433. } catch (Exception $e) {
  434. $this->errors[] = $e->getMessage();
  435. }
  436. // Set the From address with an associative array
  437. //$this->message->setFrom(array('john@doe.com' => 'John Doe'));
  438. if (!empty($this->addr_from)) {
  439. try {
  440. if (!empty($conf->global->MAIN_FORCE_DISABLE_MAIL_SPOOFING)) {
  441. // Prevent email spoofing for smtp server with a strict configuration
  442. $regexp = '/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; // This regular expression extracts all emails from a string
  443. $adressEmailFrom = array();
  444. $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
  445. $adressEmailFrom = reset($adressEmailFrom);
  446. if ($emailMatchs !== false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
  447. $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
  448. } else {
  449. $this->message->setFrom($this->getArrayAddress($this->addr_from));
  450. }
  451. } else {
  452. $this->message->setFrom($this->getArrayAddress($this->addr_from));
  453. }
  454. } catch (Exception $e) {
  455. $this->errors[] = $e->getMessage();
  456. }
  457. }
  458. // Set the To addresses with an associative array
  459. if (!empty($this->addr_to)) {
  460. try {
  461. $this->message->setTo($this->getArrayAddress($this->addr_to));
  462. } catch (Exception $e) {
  463. $this->errors[] = $e->getMessage();
  464. }
  465. }
  466. if (!empty($this->reply_to)) {
  467. try {
  468. $this->message->SetReplyTo($this->getArrayAddress($this->reply_to));
  469. } catch (Exception $e) {
  470. $this->errors[] = $e->getMessage();
  471. }
  472. }
  473. try {
  474. $this->message->setCharSet($conf->file->character_set_client);
  475. } catch (Exception $e) {
  476. $this->errors[] = $e->getMessage();
  477. }
  478. if (!empty($this->html)) {
  479. if (!empty($css)) {
  480. $this->css = $css;
  481. $this->buildCSS();
  482. }
  483. $msg = $this->html;
  484. $msg = $this->checkIfHTML($msg);
  485. }
  486. if ($this->atleastoneimage) {
  487. foreach ($this->images_encoded as $img) {
  488. //$img['fullpath'],$img['image_encoded'],$img['name'],$img['content_type'],$img['cid']
  489. $attachment = Swift_Image::fromPath($img['fullpath']);
  490. // embed image
  491. $imgcid = $this->message->embed($attachment);
  492. // replace cid by the one created by swiftmail in html message
  493. $msg = str_replace("cid:".$img['cid'], $imgcid, $msg);
  494. }
  495. }
  496. if ($this->msgishtml) {
  497. $this->message->setBody($msg, 'text/html');
  498. // And optionally an alternative body
  499. $this->message->addPart(html_entity_decode(strip_tags($msg)), 'text/plain');
  500. } else {
  501. $this->message->setBody($msg, 'text/plain');
  502. // And optionally an alternative body
  503. $this->message->addPart(dol_nl2br($msg), 'text/html');
  504. }
  505. if (!empty($this->atleastonefile)) {
  506. foreach ($filename_list as $i => $val) {
  507. //$this->message->attach(Swift_Attachment::fromPath($filename_list[$i],$mimetype_list[$i]));
  508. $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
  509. if (!empty($mimefilename_list[$i])) {
  510. $attachment->setFilename($mimefilename_list[$i]);
  511. }
  512. $this->message->attach($attachment);
  513. }
  514. }
  515. if (!empty($this->addr_cc)) {
  516. try {
  517. $this->message->setCc($this->getArrayAddress($this->addr_cc));
  518. } catch (Exception $e) {
  519. $this->errors[] = $e->getMessage();
  520. }
  521. }
  522. if (!empty($this->addr_bcc)) {
  523. try {
  524. $this->message->setBcc($this->getArrayAddress($this->addr_bcc));
  525. } catch (Exception $e) {
  526. $this->errors[] = $e->getMessage();
  527. }
  528. }
  529. //if (!empty($this->errors_to)) $this->message->setErrorsTo($this->getArrayAddress($this->errors_to));
  530. if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
  531. try {
  532. $this->message->setReadReceiptTo($this->getArrayAddress($this->addr_from));
  533. } catch (Exception $e) {
  534. $this->errors[] = $e->getMessage();
  535. }
  536. }
  537. } else {
  538. // Send mail method not correctly defined
  539. // --------------------------------------
  540. $this->error = 'Bad value for sendmode';
  541. }
  542. }
  543. /**
  544. * Send mail that was prepared by constructor.
  545. *
  546. * @return boolean True if mail sent, false otherwise
  547. */
  548. public function sendfile()
  549. {
  550. global $conf, $db, $langs;
  551. $errorlevel = error_reporting();
  552. //error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
  553. $res = false;
  554. if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
  555. require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  556. $hookmanager = new HookManager($db);
  557. $hookmanager->initHooks(array('mail'));
  558. $parameters = array();
  559. $action = '';
  560. $reshook = $hookmanager->executeHooks('sendMail', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  561. if ($reshook < 0) {
  562. $this->error = "Error in hook maildao sendMail ".$reshook;
  563. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  564. return $reshook;
  565. }
  566. if ($reshook == 1) { // Hook replace standard code
  567. return true;
  568. }
  569. $sendingmode = $this->sendmode;
  570. if ($this->sendcontext == 'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode == 'mail') {
  571. // List of sending methods
  572. $listofmethods = array();
  573. $listofmethods['mail'] = 'PHP mail function';
  574. //$listofmethods['simplemail']='Simplemail class';
  575. $listofmethods['smtps'] = 'SMTP/SMTPS socket library';
  576. // EMailing feature may be a spam problem, so when you host several users/instance, having this option may force each user to use their own SMTP agent.
  577. // You ensure that every user is using its own SMTP server when using the mass emailing module.
  578. $linktoadminemailbefore = '<a href="'.DOL_URL_ROOT.'/admin/mails.php">';
  579. $linktoadminemailend = '</a>';
  580. $this->error = $langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]);
  581. $this->errors[] = $langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]);
  582. $this->error .= '<br>'.$langs->trans("MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $listofmethods['smtps']);
  583. $this->errors[] = $langs->trans("MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $listofmethods['smtps']);
  584. if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
  585. $this->error .= '<br>'.$langs->trans("MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
  586. $this->errors[] = $langs->trans("MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
  587. }
  588. return false;
  589. }
  590. // Check number of recipient is lower or equal than MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL
  591. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
  592. $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
  593. }
  594. $tmparray1 = explode(',', $this->addr_to);
  595. if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
  596. $this->error = 'Too much recipients in to:';
  597. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
  598. return false;
  599. }
  600. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
  601. $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
  602. }
  603. $tmparray2 = explode(',', $this->addr_cc);
  604. if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
  605. $this->error = 'Too much recipients in cc:';
  606. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
  607. return false;
  608. }
  609. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
  610. $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
  611. }
  612. $tmparray3 = explode(',', $this->addr_bcc);
  613. if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
  614. $this->error = 'Too much recipients in bcc:';
  615. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
  616. return false;
  617. }
  618. if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
  619. $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
  620. }
  621. if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
  622. $this->error = 'Too much recipients in to:, cc:, bcc:';
  623. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
  624. return false;
  625. }
  626. $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
  627. $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
  628. $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
  629. $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
  630. $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE';
  631. $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
  632. $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
  633. $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
  634. $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
  635. if (!empty($this->sendcontext)) {
  636. $smtpContextKey = strtoupper($this->sendcontext);
  637. $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
  638. if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
  639. $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
  640. $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
  641. $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
  642. $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
  643. $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
  644. $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
  645. $keyfortls = 'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
  646. $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
  647. $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
  648. }
  649. }
  650. // Action according to choosed sending method
  651. if ($this->sendmode == 'mail') {
  652. // Use mail php function (default PHP method)
  653. // ------------------------------------------
  654. dol_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject, LOG_DEBUG);
  655. dol_syslog("CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
  656. //dol_syslog("CMailFile::sendfile message=\n".$message);
  657. // If Windows, sendmail_from must be defined
  658. if (isset($_SERVER["WINDIR"])) {
  659. if (empty($this->addr_from)) {
  660. $this->addr_from = 'robot@example.com';
  661. }
  662. @ini_set('sendmail_from', $this->getValidAddress($this->addr_from, 2));
  663. }
  664. // Force parameters
  665. //dol_syslog("CMailFile::sendfile conf->global->".$keyforsmtpserver."=".$conf->global->$keyforsmtpserver." cpnf->global->".$keyforsmtpport."=".$conf->global->$keyforsmtpport, LOG_DEBUG);
  666. if (!empty($conf->global->$keyforsmtpserver)) {
  667. ini_set('SMTP', $conf->global->$keyforsmtpserver);
  668. }
  669. if (!empty($conf->global->$keyforsmtpport)) {
  670. ini_set('smtp_port', $conf->global->$keyforsmtpport);
  671. }
  672. $res = true;
  673. if ($res && !$this->subject) {
  674. $this->error = "Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Subject is empty";
  675. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  676. $res = false;
  677. }
  678. $dest = $this->getValidAddress($this->addr_to, 2);
  679. if ($res && !$dest) {
  680. $this->error = "Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid";
  681. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  682. $res = false;
  683. }
  684. if ($res) {
  685. $additionnalparam = ''; // By default
  686. if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
  687. // le "Return-Path" (retour des messages bounced) dans les header ne fonctionne pas avec tous les MTA
  688. // Le forcage de la valeur grace à l'option -f de sendmail est donc possible si la constante MAIN_MAIL_ALLOW_SENDMAIL_F est definie.
  689. // Having this variable defined may create problems with some sendmail (option -f refused)
  690. // Having this variable not defined may create problems with some other sendmail (option -f required)
  691. $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) : ''));
  692. }
  693. 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
  694. $additionnalparam .= ($additionnalparam ? ' ' : '').'-ba';
  695. }
  696. if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
  697. $additionnalparam .= ($additionnalparam ? ' ' : '').'-U '.$additionnalparam; // Use -U to add additionnal params
  698. }
  699. $linuxlike = 1;
  700. if (preg_match('/^win/i', PHP_OS)) {
  701. $linuxlike = 0;
  702. }
  703. if (preg_match('/^mac/i', PHP_OS)) {
  704. $linuxlike = 0;
  705. }
  706. dol_syslog("CMailFile::sendfile: mail start".($linuxlike ? '' : " HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')).", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
  707. $this->message = stripslashes($this->message);
  708. if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
  709. $this->dump_mail();
  710. }
  711. // Encode subject if required.
  712. $subjecttouse = $this->subject;
  713. if (!ascii_check($subjecttouse)) {
  714. $subjecttouse = $this->encodetorfc2822($subjecttouse);
  715. }
  716. if (!empty($additionnalparam)) {
  717. $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
  718. } else {
  719. $res = mail($dest, $subjecttouse, $this->message, $this->headers);
  720. }
  721. if (!$res) {
  722. $langs->load("errors");
  723. $this->error = "Failed to send mail with php mail";
  724. if (!$linuxlike) {
  725. $this->error .= " to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port'); // This values are value used only for non linuxlike systems
  726. }
  727. $this->error .= ".<br>";
  728. $this->error .= $langs->trans("ErrorPhpMailDelivery");
  729. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  730. } else {
  731. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  732. }
  733. }
  734. if (isset($_SERVER["WINDIR"])) {
  735. @ini_restore('sendmail_from');
  736. }
  737. // Restore parameters
  738. if (!empty($conf->global->$keyforsmtpserver)) {
  739. ini_restore('SMTP');
  740. }
  741. if (!empty($conf->global->$keyforsmtpport)) {
  742. ini_restore('smtp_port');
  743. }
  744. } elseif ($this->sendmode == 'smtps') {
  745. if (!is_object($this->smtps)) {
  746. $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.";
  747. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  748. return false;
  749. }
  750. // Use SMTPS library
  751. // ------------------------------------------
  752. $this->smtps->setTransportType(0); // Only this method is coded in SMTPs library
  753. // Clean parameters
  754. if (empty($conf->global->$keyforsmtpserver)) {
  755. $conf->global->$keyforsmtpserver = ini_get('SMTP');
  756. }
  757. if (empty($conf->global->$keyforsmtpport)) {
  758. $conf->global->$keyforsmtpport = ini_get('smtp_port');
  759. }
  760. // If we use SSL/TLS
  761. $server = $conf->global->$keyforsmtpserver;
  762. $secure = '';
  763. if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
  764. $secure = 'ssl';
  765. }
  766. if (!empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) {
  767. $secure = 'tls';
  768. }
  769. $server = ($secure ? $secure.'://' : '').$server;
  770. $port = $conf->global->$keyforsmtpport;
  771. $this->smtps->setHost($server);
  772. $this->smtps->setPort($port); // 25, 465...;
  773. $loginid = '';
  774. $loginpass = '';
  775. if (!empty($conf->global->$keyforsmtpid)) {
  776. $loginid = $conf->global->$keyforsmtpid;
  777. $this->smtps->setID($loginid);
  778. }
  779. if (!empty($conf->global->$keyforsmtppw)) {
  780. $loginpass = $conf->global->$keyforsmtppw;
  781. $this->smtps->setPW($loginpass);
  782. }
  783. if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
  784. require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
  785. $keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice;
  786. if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
  787. $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
  788. } else {
  789. $keyforprovider = '';
  790. }
  791. $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
  792. $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
  793. if (isset($supportedoauth2array)) {
  794. $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
  795. } else {
  796. $OAUTH_SERVICENAME = 'Unknown';
  797. }
  798. require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
  799. $storage = new DoliStorage($db, $conf);
  800. try {
  801. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
  802. $expire = false;
  803. // Is token expired or will token expire in the next 30 seconds
  804. if (is_object($tokenobj)) {
  805. $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
  806. }
  807. // Token expired so we refresh it
  808. if (is_object($tokenobj) && $expire) {
  809. $credentials = new Credentials(
  810. getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_ID'),
  811. getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_SECRET'),
  812. getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_URLAUTHORIZE')
  813. );
  814. $serviceFactory = new \OAuth\ServiceFactory();
  815. $oauthname = explode('-', $OAUTH_SERVICENAME);
  816. // ex service is Google-Emails we need only the first part Google
  817. $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
  818. // We have to save the token because Google give it only once
  819. $refreshtoken = $tokenobj->getRefreshToken();
  820. $tokenobj = $apiService->refreshAccessToken($tokenobj);
  821. $tokenobj->setRefreshToken($refreshtoken);
  822. $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
  823. }
  824. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
  825. if (is_object($tokenobj)) {
  826. $this->smtps->setToken($tokenobj->getAccessToken());
  827. } else {
  828. $this->error = "Token not found";
  829. }
  830. } catch (Exception $e) {
  831. // Return an error if token not found
  832. $this->error = $e->getMessage();
  833. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  834. }
  835. }
  836. $res = true;
  837. $from = $this->smtps->getFrom('org');
  838. if ($res && !$from) {
  839. $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport." - Sender address '$from' invalid";
  840. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  841. $res = false;
  842. }
  843. $dest = $this->smtps->getTo();
  844. if ($res && !$dest) {
  845. $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport." - Recipient address '$dest' invalid";
  846. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  847. $res = false;
  848. }
  849. if ($res) {
  850. if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
  851. $this->smtps->setDebug(true);
  852. }
  853. $result = $this->smtps->sendMsg();
  854. if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
  855. $this->dump_mail();
  856. }
  857. $result = $this->smtps->getErrors();
  858. if (empty($this->error) && empty($result)) {
  859. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  860. $res = true;
  861. } else {
  862. if (empty($this->error)) {
  863. $this->error = $result;
  864. }
  865. dol_syslog("CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport." - ".$this->error, LOG_ERR);
  866. $res = false;
  867. }
  868. }
  869. } elseif ($this->sendmode == 'swiftmailer') {
  870. // Use Swift Mailer library
  871. // ------------------------------------------
  872. require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
  873. // Clean parameters
  874. if (empty($conf->global->$keyforsmtpserver)) {
  875. $conf->global->$keyforsmtpserver = ini_get('SMTP');
  876. }
  877. if (empty($conf->global->$keyforsmtpport)) {
  878. $conf->global->$keyforsmtpport = ini_get('smtp_port');
  879. }
  880. // If we use SSL/TLS
  881. $server = $conf->global->$keyforsmtpserver;
  882. $secure = '';
  883. if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
  884. $secure = 'ssl';
  885. }
  886. if (!empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) {
  887. $secure = 'tls';
  888. }
  889. $this->transport = new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
  890. if (!empty($conf->global->$keyforsmtpid)) {
  891. $this->transport->setUsername($conf->global->$keyforsmtpid);
  892. }
  893. if (!empty($conf->global->$keyforsmtppw) && getDolGlobalString($keyforsmtpauthtype) != "XOAUTH2") {
  894. $this->transport->setPassword($conf->global->$keyforsmtppw);
  895. }
  896. if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
  897. require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
  898. $keyforsupportedoauth2array = getDolGlobalString($keyforsmtpoauthservice);
  899. if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
  900. $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
  901. } else {
  902. $keyforprovider = '';
  903. }
  904. $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
  905. $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
  906. $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
  907. require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
  908. $storage = new DoliStorage($db, $conf);
  909. try {
  910. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
  911. $expire = false;
  912. // Is token expired or will token expire in the next 30 seconds
  913. if (is_object($tokenobj)) {
  914. $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
  915. }
  916. // Token expired so we refresh it
  917. if (is_object($tokenobj) && $expire) {
  918. $credentials = new Credentials(
  919. getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_ID'),
  920. getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_SECRET'),
  921. getDolGlobalString('OAUTH_'.getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE').'_URLAUTHORIZE')
  922. );
  923. $serviceFactory = new \OAuth\ServiceFactory();
  924. $oauthname = explode('-', $OAUTH_SERVICENAME);
  925. // ex service is Google-Emails we need only the first part Google
  926. $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
  927. // We have to save the token because Google give it only once
  928. $refreshtoken = $tokenobj->getRefreshToken();
  929. $tokenobj = $apiService->refreshAccessToken($tokenobj);
  930. $tokenobj->setRefreshToken($refreshtoken);
  931. $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
  932. }
  933. if (is_object($tokenobj)) {
  934. $this->transport->setAuthMode('XOAUTH2');
  935. $this->transport->setPassword($tokenobj->getAccessToken());
  936. } else {
  937. $this->errors[] = "Token not found";
  938. }
  939. } catch (Exception $e) {
  940. // Return an error if token not found
  941. $this->errors[] = $e->getMessage();
  942. dol_syslog("CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
  943. }
  944. }
  945. if (!empty($conf->global->$keyforsslseflsigned)) {
  946. $this->transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
  947. }
  948. //$smtps->_msgReplyTo = 'reply@web.com';
  949. // Switch content encoding to base64 - avoid the doubledot issue with quoted-printable
  950. $contentEncoderBase64 = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
  951. $this->message->setEncoder($contentEncoderBase64);
  952. // Create the Mailer using your created Transport
  953. $this->mailer = new Swift_Mailer($this->transport);
  954. // DKIM SIGN
  955. if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
  956. $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
  957. $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
  958. $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
  959. $signer = new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
  960. $this->message->attachSigner($signer->ignoreHeader('Return-Path'));
  961. }
  962. if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
  963. // To use the ArrayLogger
  964. $this->logger = new Swift_Plugins_Loggers_ArrayLogger();
  965. // Or to use the Echo Logger
  966. //$this->logger = new Swift_Plugins_Loggers_EchoLogger();
  967. $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->logger));
  968. }
  969. // send mail
  970. try {
  971. $result = $this->mailer->send($this->message, $failedRecipients);
  972. } catch (Exception $e) {
  973. $this->errors[] = $e->getMessage();
  974. }
  975. if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
  976. $this->dump_mail();
  977. }
  978. $res = true;
  979. if (!empty($this->error) || !empty($this->errors) || !$result) {
  980. if (!empty($failedRecipients)) {
  981. $this->errors[] = 'Transport failed for the following addresses: "' . join('", "', $failedRecipients) . '".';
  982. }
  983. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  984. $res = false;
  985. } else {
  986. dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
  987. }
  988. } else {
  989. // Send mail method not correctly defined
  990. // --------------------------------------
  991. return 'Bad value for sendmode';
  992. }
  993. $parameters = array('sent' => $res);
  994. $action = '';
  995. $reshook = $hookmanager->executeHooks('sendMailAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  996. if ($reshook < 0) {
  997. $this->error = "Error in hook maildao sendMailAfter ".$reshook;
  998. dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
  999. return $reshook;
  1000. }
  1001. } else {
  1002. $this->error = 'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
  1003. dol_syslog("CMailFile::sendfile: ".$this->error, LOG_WARNING);
  1004. }
  1005. error_reporting($errorlevel); // Reactive niveau erreur origine
  1006. return $res;
  1007. }
  1008. /**
  1009. * Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word
  1010. *
  1011. * @param string $stringtoencode String to encode
  1012. * @return string string encoded
  1013. */
  1014. public static function encodetorfc2822($stringtoencode)
  1015. {
  1016. global $conf;
  1017. return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
  1018. }
  1019. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1020. /**
  1021. * Read a file on disk and return encoded content for emails (mode = 'mail')
  1022. *
  1023. * @param string $sourcefile Path to file to encode
  1024. * @return int|string <0 if KO, encoded string if OK
  1025. */
  1026. private function _encode_file($sourcefile)
  1027. {
  1028. // phpcs:enable
  1029. $newsourcefile = dol_osencode($sourcefile);
  1030. if (is_readable($newsourcefile)) {
  1031. $contents = file_get_contents($newsourcefile); // Need PHP 4.3
  1032. $encoded = chunk_split(base64_encode($contents), 76, $this->eol); // 76 max is defined into http://tools.ietf.org/html/rfc2047
  1033. return $encoded;
  1034. } else {
  1035. $this->error = "Error: Can't read file '".$sourcefile."' into _encode_file";
  1036. dol_syslog("CMailFile::encode_file: ".$this->error, LOG_ERR);
  1037. return -1;
  1038. }
  1039. }
  1040. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1041. /**
  1042. * Write content of a SMTP request into a dump file (mode = all)
  1043. * Used for debugging.
  1044. * Note that to see full SMTP protocol, you can use tcpdump -w /tmp/smtp -s 2000 port 25"
  1045. *
  1046. * @return void
  1047. */
  1048. public function dump_mail()
  1049. {
  1050. // phpcs:enable
  1051. global $conf, $dolibarr_main_data_root;
  1052. if (@is_writeable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
  1053. $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
  1054. $fp = fopen($outputfile, "w");
  1055. if ($this->sendmode == 'mail') {
  1056. fputs($fp, $this->headers);
  1057. fputs($fp, $this->eol); // This eol is added by the mail function, so we add it in log
  1058. fputs($fp, $this->message);
  1059. } elseif ($this->sendmode == 'smtps') {
  1060. fputs($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
  1061. } elseif ($this->sendmode == 'swiftmailer') {
  1062. fputs($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on
  1063. }
  1064. fclose($fp);
  1065. if (!empty($conf->global->MAIN_UMASK)) {
  1066. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  1067. }
  1068. }
  1069. }
  1070. /**
  1071. * Correct an uncomplete html string
  1072. *
  1073. * @param string $msg String
  1074. * @return string Completed string
  1075. */
  1076. public function checkIfHTML($msg)
  1077. {
  1078. if (!preg_match('/^[\s\t]*<html/i', $msg)) {
  1079. $out = "<html><head><title></title>";
  1080. if (!empty($this->styleCSS)) {
  1081. $out .= $this->styleCSS;
  1082. }
  1083. $out .= "</head><body";
  1084. if (!empty($this->bodyCSS)) {
  1085. $out .= $this->bodyCSS;
  1086. }
  1087. $out .= ">";
  1088. $out .= $msg;
  1089. $out .= "</body></html>";
  1090. } else {
  1091. $out = $msg;
  1092. }
  1093. return $out;
  1094. }
  1095. /**
  1096. * Build a css style (mode = all) into this->styleCSS and this->bodyCSS
  1097. *
  1098. * @return string
  1099. */
  1100. public function buildCSS()
  1101. {
  1102. if (!empty($this->css)) {
  1103. // Style CSS
  1104. $this->styleCSS = '<style type="text/css">';
  1105. $this->styleCSS .= 'body {';
  1106. if ($this->css['bgcolor']) {
  1107. $this->styleCSS .= ' background-color: '.$this->css['bgcolor'].';';
  1108. $this->bodyCSS .= ' bgcolor="'.$this->css['bgcolor'].'"';
  1109. }
  1110. if ($this->css['bgimage']) {
  1111. // TODO recuperer cid
  1112. $this->styleCSS .= ' background-image: url("cid:'.$this->css['bgimage_cid'].'");';
  1113. }
  1114. $this->styleCSS .= '}';
  1115. $this->styleCSS .= '</style>';
  1116. }
  1117. }
  1118. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1119. /**
  1120. * Create SMTP headers (mode = 'mail')
  1121. *
  1122. * @return string headers
  1123. */
  1124. public function write_smtpheaders()
  1125. {
  1126. // phpcs:enable
  1127. global $conf;
  1128. $out = "";
  1129. $host = dol_getprefix('email');
  1130. // Sender
  1131. //$out.= "Sender: ".getValidAddress($this->addr_from,2)).$this->eol2;
  1132. $out .= "From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
  1133. if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
  1134. $out .= "To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
  1135. }
  1136. // 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.
  1137. $out .= "Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
  1138. if (isset($this->reply_to) && $this->reply_to) {
  1139. $out .= "Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
  1140. }
  1141. if (isset($this->errors_to) && $this->errors_to) {
  1142. $out .= "Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
  1143. }
  1144. // Receiver
  1145. if (isset($this->addr_cc) && $this->addr_cc) {
  1146. $out .= "Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
  1147. }
  1148. if (isset($this->addr_bcc) && $this->addr_bcc) {
  1149. $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 ?
  1150. }
  1151. // Delivery receipt
  1152. if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
  1153. $out .= "Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
  1154. }
  1155. //$out.= "X-Priority: 3".$this->eol2;
  1156. $out .= 'Date: '.date("r").$this->eol2;
  1157. $trackid = $this->trackid;
  1158. if ($trackid) {
  1159. // References is kept in response and Message-ID is returned into In-Reply-To:
  1160. $this->msgid = time().'.phpmail-dolibarr-'.$trackid.'@'.$host;
  1161. $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2; // Uppercase seems replaced by phpmail
  1162. $out .= 'References: <'.$this->msgid.">".$this->eol2;
  1163. $out .= 'X-Dolibarr-TRACKID: '.$trackid.'@'.$host.$this->eol2;
  1164. } else {
  1165. $this->msgid = time().'.phpmail@'.$host;
  1166. $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2;
  1167. }
  1168. if (!empty($_SERVER['REMOTE_ADDR'])) {
  1169. $out .= "X-RemoteAddr: ".$_SERVER['REMOTE_ADDR'].$this->eol2;
  1170. }
  1171. $out .= "X-Mailer: Dolibarr version ".DOL_VERSION." (using php mail)".$this->eol2;
  1172. $out .= "Mime-Version: 1.0".$this->eol2;
  1173. //$out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol;
  1174. $out .= "Content-Type: multipart/mixed;".$this->eol2." boundary=\"".$this->mixed_boundary."\"".$this->eol2;
  1175. $out .= "Content-Transfer-Encoding: 8bit".$this->eol2; // TODO Seems to be ignored. Header is 7bit once received.
  1176. dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out);
  1177. return $out;
  1178. }
  1179. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1180. /**
  1181. * Create header MIME (mode = 'mail')
  1182. *
  1183. * @param array $filename_list Array of filenames
  1184. * @param array $mimefilename_list Array of mime types
  1185. * @return string mime headers
  1186. */
  1187. public function write_mimeheaders($filename_list, $mimefilename_list)
  1188. {
  1189. // phpcs:enable
  1190. $mimedone = 0;
  1191. $out = "";
  1192. if (is_array($filename_list)) {
  1193. $filename_list_size = count($filename_list);
  1194. for ($i = 0; $i < $filename_list_size; $i++) {
  1195. if ($filename_list[$i]) {
  1196. if ($mimefilename_list[$i]) {
  1197. $filename_list[$i] = $mimefilename_list[$i];
  1198. }
  1199. $out .= "X-attachments: $filename_list[$i]".$this->eol2;
  1200. }
  1201. }
  1202. }
  1203. dol_syslog("CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
  1204. return $out;
  1205. }
  1206. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1207. /**
  1208. * Return email content (mode = 'mail')
  1209. *
  1210. * @param string $msgtext Message string
  1211. * @return string String content
  1212. */
  1213. public function write_body($msgtext)
  1214. {
  1215. // phpcs:enable
  1216. global $conf;
  1217. $out = '';
  1218. $out .= "--".$this->mixed_boundary.$this->eol;
  1219. if ($this->atleastoneimage) {
  1220. $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
  1221. $out .= $this->eol;
  1222. $out .= "--".$this->alternative_boundary.$this->eol;
  1223. }
  1224. // Make RFC821 Compliant, replace bare linefeeds
  1225. $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $msgtext); // PCRE modifier /s means new lines are common chars
  1226. if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
  1227. $strContent = preg_replace("/\r\n/si", "\n", $strContent); // PCRE modifier /s means new lines are common chars
  1228. }
  1229. $strContentAltText = '';
  1230. if ($this->msgishtml) {
  1231. // Similar code to forge a text from html is also in smtps.class.php
  1232. $strContentAltText = preg_replace("/<br\s*[^>]*>/", " ", $strContent);
  1233. // TODO We could replace <img ...> with [Filename.ext] like Gmail do.
  1234. $strContentAltText = html_entity_decode(strip_tags($strContentAltText)); // Remove any HTML tags
  1235. $strContentAltText = trim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ? "\r\n" : "\n"));
  1236. // Check if html header already in message, if not complete the message
  1237. $strContent = $this->checkIfHTML($strContent);
  1238. }
  1239. // Make RFC2045 Compliant, split lines
  1240. //$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems ko if not used on a base64 content
  1241. // TODO Encode main content into base64 and use the chunk_split, or quoted-printable
  1242. $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.
  1243. if ($this->msgishtml) {
  1244. if ($this->atleastoneimage) {
  1245. $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  1246. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
  1247. $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol; // Add plain text message
  1248. $out .= "--".$this->alternative_boundary.$this->eol;
  1249. $out .= "Content-Type: multipart/related;".$this->eol." boundary=\"".$this->related_boundary."\"".$this->eol;
  1250. $out .= $this->eol;
  1251. $out .= "--".$this->related_boundary.$this->eol;
  1252. }
  1253. if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) { // Add plain text message part before html part
  1254. $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
  1255. $out .= $this->eol;
  1256. $out .= "--".$this->alternative_boundary.$this->eol;
  1257. $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  1258. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
  1259. $out .= $this->eol.$strContentAltText.$this->eol;
  1260. $out .= "--".$this->alternative_boundary.$this->eol;
  1261. }
  1262. $out .= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
  1263. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol; // TODO Use base64
  1264. $out .= $this->eol.$strContent.$this->eol;
  1265. if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) { // Add plain text message part after html part
  1266. $out .= "--".$this->alternative_boundary."--".$this->eol;
  1267. }
  1268. } else {
  1269. $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
  1270. //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
  1271. $out .= $this->eol.$strContent.$this->eol;
  1272. }
  1273. $out .= $this->eol;
  1274. // Encode images
  1275. if ($this->atleastoneimage) {
  1276. $out .= $this->write_images($this->images_encoded);
  1277. // always end related and end alternative after inline images
  1278. $out .= "--".$this->related_boundary."--".$this->eol;
  1279. $out .= $this->eol."--".$this->alternative_boundary."--".$this->eol;
  1280. $out .= $this->eol;
  1281. }
  1282. return $out;
  1283. }
  1284. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1285. /**
  1286. * Attach file to email (mode = 'mail')
  1287. *
  1288. * @param array $filename_list Tableau
  1289. * @param array $mimetype_list Tableau
  1290. * @param array $mimefilename_list Tableau
  1291. * @param array $cidlist Array of CID if file must be completed with CID code
  1292. * @return string String with files encoded
  1293. */
  1294. private function write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
  1295. {
  1296. // phpcs:enable
  1297. $out = '';
  1298. $filename_list_size = count($filename_list);
  1299. for ($i = 0; $i < $filename_list_size; $i++) {
  1300. if ($filename_list[$i]) {
  1301. dol_syslog("CMailFile::write_files: i=$i");
  1302. $encoded = $this->_encode_file($filename_list[$i]);
  1303. if ($encoded >= 0) {
  1304. if ($mimefilename_list[$i]) {
  1305. $filename_list[$i] = $mimefilename_list[$i];
  1306. }
  1307. if (!$mimetype_list[$i]) {
  1308. $mimetype_list[$i] = "application/octet-stream";
  1309. }
  1310. $out .= "--".$this->mixed_boundary.$this->eol;
  1311. $out .= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
  1312. $out .= "Content-Type: ".$mimetype_list[$i]."; name=\"".$filename_list[$i]."\"".$this->eol;
  1313. $out .= "Content-Transfer-Encoding: base64".$this->eol;
  1314. $out .= "Content-Description: ".$filename_list[$i].$this->eol;
  1315. if (!empty($cidlist) && is_array($cidlist) && $cidlist[$i]) {
  1316. $out .= "X-Attachment-Id: ".$cidlist[$i].$this->eol;
  1317. $out .= "Content-ID: <".$cidlist[$i].'>'.$this->eol;
  1318. }
  1319. $out .= $this->eol;
  1320. $out .= $encoded;
  1321. $out .= $this->eol;
  1322. //$out.= $this->eol;
  1323. } else {
  1324. return $encoded;
  1325. }
  1326. }
  1327. }
  1328. return $out;
  1329. }
  1330. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1331. /**
  1332. * Attach an image to email (mode = 'mail')
  1333. *
  1334. * @param array $images_list Array of array image
  1335. * @return string Chaine images encodees
  1336. */
  1337. public function write_images($images_list)
  1338. {
  1339. // phpcs:enable
  1340. $out = '';
  1341. if (is_array($images_list)) {
  1342. foreach ($images_list as $img) {
  1343. dol_syslog("CMailFile::write_images: ".$img["name"]);
  1344. $out .= "--".$this->related_boundary.$this->eol; // always related for an inline image
  1345. $out .= "Content-Type: ".$img["content_type"]."; name=\"".$img["name"]."\"".$this->eol;
  1346. $out .= "Content-Transfer-Encoding: base64".$this->eol;
  1347. $out .= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
  1348. $out .= "Content-ID: <".$img["cid"].">".$this->eol;
  1349. $out .= $this->eol;
  1350. $out .= $img["image_encoded"];
  1351. $out .= $this->eol;
  1352. }
  1353. }
  1354. return $out;
  1355. }
  1356. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1357. /**
  1358. * Try to create a socket connection
  1359. *
  1360. * @param string $host Add ssl:// for SSL/TLS.
  1361. * @param int $port Example: 25, 465
  1362. * @return int Socket id if ok, 0 if KO
  1363. */
  1364. public function check_server_port($host, $port)
  1365. {
  1366. // phpcs:enable
  1367. global $conf;
  1368. $_retVal = 0;
  1369. $timeout = 5; // Timeout in seconds
  1370. if (function_exists('fsockopen')) {
  1371. $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
  1372. $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
  1373. $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
  1374. $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
  1375. $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE';
  1376. $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
  1377. $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
  1378. $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
  1379. $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
  1380. if (!empty($this->sendcontext)) {
  1381. $smtpContextKey = strtoupper($this->sendcontext);
  1382. $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
  1383. if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
  1384. $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
  1385. $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
  1386. $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
  1387. $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
  1388. $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
  1389. $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
  1390. $keyfortls = 'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
  1391. $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
  1392. $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
  1393. }
  1394. }
  1395. // If we use SSL/TLS
  1396. if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
  1397. $host = 'ssl://'.$host;
  1398. }
  1399. // tls smtp start with no encryption
  1400. //if (!empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS) && function_exists('openssl_open')) $host='tls://'.$host;
  1401. dol_syslog("Try socket connection to host=".$host." port=".$port);
  1402. //See if we can connect to the SMTP server
  1403. $errno = 0; $errstr = '';
  1404. if ($socket = @fsockopen(
  1405. $host, // Host to test, IP or domain. Add ssl:// for SSL/TLS.
  1406. $port, // which Port number to use
  1407. $errno, // actual system level error
  1408. $errstr, // and any text that goes with the error
  1409. $timeout // timeout for reading/writing data over the socket
  1410. )) {
  1411. // Windows still does not have support for this timeout function
  1412. if (function_exists('stream_set_timeout')) {
  1413. stream_set_timeout($socket, $timeout, 0);
  1414. }
  1415. dol_syslog("Now we wait for answer 220");
  1416. // Check response from Server
  1417. if ($_retVal = $this->server_parse($socket, "220")) {
  1418. $_retVal = $socket;
  1419. }
  1420. } else {
  1421. $this->error = utf8_check('Error '.$errno.' - '.$errstr) ? 'Error '.$errno.' - '.$errstr : utf8_encode('Error '.$errno.' - '.$errstr);
  1422. }
  1423. }
  1424. return $_retVal;
  1425. }
  1426. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1427. /**
  1428. * This function has been modified as provided by SirSir to allow multiline responses when
  1429. * using SMTP Extensions.
  1430. *
  1431. * @param resource $socket Socket
  1432. * @param string $response Response string
  1433. * @return boolean true if success
  1434. */
  1435. public function server_parse($socket, $response)
  1436. {
  1437. // phpcs:enable
  1438. $_retVal = true; // Indicates if Object was created or not
  1439. $server_response = '';
  1440. while (substr($server_response, 3, 1) != ' ') {
  1441. if (!($server_response = fgets($socket, 256))) {
  1442. $this->error = "Couldn't get mail server response codes";
  1443. return false;
  1444. }
  1445. }
  1446. if (!(substr($server_response, 0, 3) == $response)) {
  1447. $this->error = "Ran into problems sending Mail.\r\nResponse: $server_response";
  1448. $_retVal = false;
  1449. }
  1450. return $_retVal;
  1451. }
  1452. /**
  1453. * Seearch images into html message and init array this->images_encoded if found
  1454. *
  1455. * @param string $images_dir Location of physical images files. For example $dolibarr_main_data_root.'/medias'
  1456. * @return int >0 if OK, <0 if KO
  1457. */
  1458. private function findHtmlImages($images_dir)
  1459. {
  1460. // Build the array of image extensions
  1461. $extensions = array_keys($this->image_types);
  1462. // We search (into mail body this->html), if we find some strings like "... file=xxx.img"
  1463. // For example when:
  1464. // <img alt="" src="/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/picture.jpg" style="height:356px; width:1040px" />
  1465. $matches = array();
  1466. preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
  1467. if (!empty($matches)) {
  1468. $i = 0;
  1469. // We are interested in $matches[1] only (the second set of parenthesis into regex)
  1470. foreach ($matches[1] as $full) {
  1471. $regs = array();
  1472. if (preg_match('/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) { // If xxx is 'file=aaa'
  1473. $img = $regs[1];
  1474. if (file_exists($images_dir.'/'.$img)) {
  1475. // Image path in src
  1476. $src = preg_quote($full, '/');
  1477. // Image full path
  1478. $this->html_images[$i]["fullpath"] = $images_dir.'/'.$img;
  1479. // Image name
  1480. $this->html_images[$i]["name"] = $img;
  1481. // Content type
  1482. $regext = array();
  1483. if (preg_match('/^.+\.(\w{3,4})$/', $img, $regext)) {
  1484. $ext = strtolower($regext[1]);
  1485. $this->html_images[$i]["content_type"] = $this->image_types[$ext];
  1486. }
  1487. // cid
  1488. $this->html_images[$i]["cid"] = dol_hash(uniqid(time()), 3); // Force md5 hash (does not contains special chars)
  1489. // type
  1490. $this->html_images[$i]["type"] = 'cidfromurl';
  1491. $this->html = preg_replace("/src=\"$src\"|src='$src'/i", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
  1492. }
  1493. $i++;
  1494. }
  1495. }
  1496. if (!empty($this->html_images)) {
  1497. $inline = array();
  1498. $i = 0;
  1499. foreach ($this->html_images as $img) {
  1500. $fullpath = $images_dir.'/'.$img["name"];
  1501. // If duplicate images are embedded, they may show up as attachments, so remove them.
  1502. if (!in_array($fullpath, $inline)) {
  1503. // Read image file
  1504. if ($image = file_get_contents($fullpath)) {
  1505. // On garde que le nom de l'image
  1506. $regs = array();
  1507. preg_match('/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img["name"], $regs);
  1508. $imgName = $regs[1];
  1509. $this->images_encoded[$i]['name'] = $imgName;
  1510. $this->images_encoded[$i]['fullpath'] = $fullpath;
  1511. $this->images_encoded[$i]['content_type'] = $img["content_type"];
  1512. $this->images_encoded[$i]['cid'] = $img["cid"];
  1513. // Encodage de l'image
  1514. $this->images_encoded[$i]["image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
  1515. $inline[] = $fullpath;
  1516. }
  1517. }
  1518. $i++;
  1519. }
  1520. } else {
  1521. return -1;
  1522. }
  1523. return 1;
  1524. } else {
  1525. return 0;
  1526. }
  1527. }
  1528. /**
  1529. * Seearch images with data:image format into html message
  1530. *
  1531. * @param string $images_dir Location of where to store physicaly images files. For example $dolibarr_main_data_root.'/medias'
  1532. * @return int >0 if OK, <0 if KO
  1533. */
  1534. private function findHtmlImagesIsSrcData($images_dir)
  1535. {
  1536. global $conf;
  1537. // Build the array of image extensions
  1538. $extensions = array_keys($this->image_types);
  1539. if ($images_dir && !dol_is_dir($images_dir)) {
  1540. dol_mkdir($images_dir, DOL_DATA_ROOT);
  1541. }
  1542. // Uncomment this for debug
  1543. /*
  1544. global $dolibarr_main_data_root;
  1545. $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
  1546. $fp = fopen($outputfile, "w+");
  1547. fwrite($fp, $this->html);
  1548. fclose($fp);
  1549. */
  1550. // We search (into mail body this->html), if we find some strings like "... file=xxx.img"
  1551. // For example when:
  1552. // <img alt="" src="/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/picture.jpg" style="height:356px; width:1040px" />
  1553. $matches = array();
  1554. preg_match_all('/src="data:image\/('.implode('|', $extensions).');base64,([^"]+)"/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
  1555. if (!empty($matches) && !empty($matches[1])) {
  1556. if (empty($images_dir)) {
  1557. // No temp directory provided, so we are not able to support convertion of data:image into physical images.
  1558. $this->error = 'NoTempDirProvidedInCMailConstructorSoCantConvertDataImgOnDisk';
  1559. return -1;
  1560. }
  1561. $i = 0;
  1562. foreach ($matches[1] as $key => $ext) {
  1563. // We save the image to send in disk
  1564. $filecontent = $matches[2][$key];
  1565. $cid = 'cid000'.dol_hash($this->html, 'md5');
  1566. $destfiletmp = $images_dir.'/'.$cid.'.'.$ext;
  1567. $fhandle = @fopen($destfiletmp, 'w');
  1568. if ($fhandle) {
  1569. $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent));
  1570. fclose($fhandle);
  1571. @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK));
  1572. } else {
  1573. $this->errors[] = "Failed to open file '".$destfiletmp."' for write";
  1574. return -1;
  1575. }
  1576. if (file_exists($destfiletmp)) {
  1577. // Image full path
  1578. $this->html_images[$i]["fullpath"] = $destfiletmp;
  1579. // Image name
  1580. $this->html_images[$i]["name"] = basename($destfiletmp);
  1581. // Content type
  1582. $this->html_images[$i]["content_type"] = $this->image_types[strtolower($ext)];
  1583. // cid
  1584. $this->html_images[$i]["cid"] = $cid;
  1585. // type
  1586. $this->html_images[$i]["type"] = 'cidfromdata';
  1587. $this->html = str_replace('src="data:image/'.$ext.';base64,'.$filecontent.'"', 'src="cid:'.$this->html_images[$i]["cid"].'"', $this->html);
  1588. }
  1589. $i++;
  1590. }
  1591. return 1;
  1592. } else {
  1593. return 0;
  1594. }
  1595. }
  1596. /**
  1597. * Return a formatted address string for SMTP protocol
  1598. *
  1599. * @param string $address Example: 'John Doe <john@doe.com>, Alan Smith <alan@smith.com>' or 'john@doe.com, alan@smith.com'
  1600. * @param int $format 0=auto, 1=emails with <>, 2=emails without <>, 3=auto + label between ", 4 label or email, 5 mailto link
  1601. * @param int $encode 0=No encode name, 1=Encode name to RFC2822
  1602. * @param int $maxnumberofemail 0=No limit. Otherwise, maximum number of emails returned ($address may contains several email separated with ','). Add '...' if there is more.
  1603. * @return string If format 0: '<john@doe.com>' or 'John Doe <john@doe.com>' or '=?UTF-8?B?Sm9obiBEb2U=?= <john@doe.com>'
  1604. * If format 1: '<john@doe.com>'
  1605. * If format 2: 'john@doe.com'
  1606. * If format 3: '<john@doe.com>' or '"John Doe" <john@doe.com>' or '"=?UTF-8?B?Sm9obiBEb2U=?=" <john@doe.com>'
  1607. * If format 4: 'John Doe' or 'john@doe.com' if no label exists
  1608. * If format 5: <a href="mailto:john@doe.com">John Doe</a> or <a href="mailto:john@doe.com">john@doe.com</a> if no label exists
  1609. * @see getArrayAddress()
  1610. */
  1611. public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
  1612. {
  1613. global $conf;
  1614. $ret = '';
  1615. $arrayaddress = explode(',', $address);
  1616. // Boucle sur chaque composant de l'adresse
  1617. $i = 0;
  1618. foreach ($arrayaddress as $val) {
  1619. $regs = array();
  1620. if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
  1621. $name = trim($regs[1]);
  1622. $email = trim($regs[2]);
  1623. } else {
  1624. $name = '';
  1625. $email = trim($val);
  1626. }
  1627. if ($email) {
  1628. $i++;
  1629. $newemail = '';
  1630. if ($format == 5) {
  1631. $newemail = $name ? $name : $email;
  1632. $newemail = '<a href="mailto:'.$email.'">'.$newemail.'</a>';
  1633. }
  1634. if ($format == 4) {
  1635. $newemail = $name ? $name : $email;
  1636. }
  1637. if ($format == 2) {
  1638. $newemail = $email;
  1639. }
  1640. if ($format == 1 || $format == 3) {
  1641. $newemail = '<'.$email.'>';
  1642. }
  1643. if ($format == 0 || $format == 3) {
  1644. if (!empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) {
  1645. $newemail = '<'.$email.'>';
  1646. } elseif (!$name) {
  1647. $newemail = '<'.$email.'>';
  1648. } else {
  1649. $newemail = ($format == 3 ? '"' : '').($encode ?self::encodetorfc2822($name) : $name).($format == 3 ? '"' : '').' <'.$email.'>';
  1650. }
  1651. }
  1652. $ret = ($ret ? $ret.',' : '').$newemail;
  1653. // Stop if we have too much records
  1654. if ($maxnumberofemail && $i >= $maxnumberofemail) {
  1655. if (count($arrayaddress) > $maxnumberofemail) {
  1656. $ret .= '...';
  1657. }
  1658. break;
  1659. }
  1660. }
  1661. }
  1662. return $ret;
  1663. }
  1664. /**
  1665. * Return a formatted array of address string for SMTP protocol
  1666. *
  1667. * @param string $address Example: 'John Doe <john@doe.com>, Alan Smith <alan@smith.com>' or 'john@doe.com, alan@smith.com'
  1668. * @return array array of email => name
  1669. * @see getValidAddress()
  1670. */
  1671. public static function getArrayAddress($address)
  1672. {
  1673. global $conf;
  1674. $ret = array();
  1675. $arrayaddress = explode(',', $address);
  1676. // Boucle sur chaque composant de l'adresse
  1677. foreach ($arrayaddress as $val) {
  1678. if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
  1679. $name = trim($regs[1]);
  1680. $email = trim($regs[2]);
  1681. } else {
  1682. $name = null;
  1683. $email = trim($val);
  1684. }
  1685. $ret[$email] = empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL) ? $name : null;
  1686. }
  1687. return $ret;
  1688. }
  1689. }