CMailFile.class.php 77 KB

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