CMailFile.class.php 79 KB

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